When trying to get a structure with string and uint32 types through a gRPC request, it is all shifted into one key

huangapple go评论242阅读模式
英文:

When trying to get a structure with string and uint32 types through a gRPC request, it is all shifted into one key

问题

I'm trying to get a structure from a database. I use gorm and gRPC for this. Previously, I used only string data types, and there were no problems.
Here is my struct that was previously for gorm:

  1. type Book struct {
  2. BookID string `gorm:"primarykey;autoIncrement"`
  3. Name string
  4. Year string
  5. Edition string
  6. Authors []*Author `gorm:"many2many:book_author"`
  7. }

The former struct generated by file.proto:

  1. type Book struct {
  2. state protoimpl.MessageState
  3. sizeCache protoimpl.SizeCache
  4. unknownFields protoimpl.UnknownFields
  5. Bookid string `protobuf:"bytes,1,opt,name=bookid,proto3" json:"bookid,omitempty"`
  6. Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
  7. Year string `protobuf:"bytes,3,opt,name=year,proto3" json:"year,omitempty"`
  8. Edition string `protobuf:"bytes,4,opt,name=edition,proto3" json:"edition,omitempty"`
  9. }

And a function on the server side that handles all this:

  1. func (*server) UpdateBook(ctx context.Context, req *pb.UpdateBookRequest) (*pb.UpdateBookResponse, error) {
  2. fmt.Println("Update Book")
  3. var book Book
  4. reqBook := req.GetBook()
  5. res := DB.Model(&book).Where("book_id=?", reqBook.Bookid).Updates(
  6. Book{Name: reqBook.Name, Year: reqBook.Year, Edition: reqBook.Edition})
  7. if res.RowsAffected == 0 {
  8. return nil, errors.New("Books not found")
  9. }
  10. return &pb.UpdateBookResponse{
  11. Book: &pb.Book{
  12. Bookid: book.BookID,
  13. Name: book.Name,
  14. Year: book.Year,
  15. Edition: book.Edition,
  16. },
  17. }, nil
  18. }

As a result, when sending a gRPC request to getBooks(), I got what I needed:

  1. {
  2. "books": [
  3. {
  4. "bookid": "",
  5. "name": "Логические ошибки",
  6. "year": "2017",
  7. "edition": "1"
  8. },
  9. {
  10. "bookid": "",
  11. "name": "LINUX API исчерпывающее руководство",
  12. "year": "2017",
  13. "edition": "1"
  14. },
  15. {
  16. "bookid": "",
  17. "name": "Сказки громовых",
  18. "year": "1999",
  19. "edition": "1"
  20. },
  21. {
  22. "bookid": "",
  23. "name": "Алгоритмы. Руководство по разработке",
  24. "year": "2022",
  25. "edition": "3"
  26. },
  27. ]
  28. }

Then, for gorm to work correctly, I needed to change the bookid data type from string to uint32, file.proto was also regenerated.

Current gorm struct:

  1. type Book struct {
  2. BookID uint32 `gorm:"primarykey;autoIncrement"`
  3. Name string
  4. Year string
  5. Edition string
  6. Authors []*Author `gorm:"many2many:book_author"`
  7. }

Current struct generated by file.proto:

  1. type Book struct {
  2. state protoimpl.MessageState
  3. sizeCache protoimpl.SizeCache
  4. unknownFields protoimpl.UnknownFields
  5. Bookid uint32 `protobuf:"varint,1,opt,name=bookid,proto3" json:"bookid,omitempty"`
  6. Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
  7. Year string `protobuf:"bytes,3,opt,name=year,proto3" json:"year,omitempty"`
  8. Edition string `protobuf:"bytes,4,opt,name=edition,proto3" json:"edition,omitempty"`
  9. }

But now when sending a gRPC request, I get all the values in one line of the key:

  1. {
  2. "books": [
  3. {
  4. "bookid": "",
  5. "name": "",
  6. "year": "",
  7. "edition": "еские ошибки2017\"1\nH\b;LINUX API исчерпывающее руководство2017\"1\n*\bСказки громовых1999\"1\nQ\bDАлгоритмы. Руководство по разработке2022\"3\n\"8Роман Светы2003\"1"
  8. }
  9. ]
  10. }

The reason for this behavior of the function is not clear to me, and if you send everything in JSON format, then the result comes correct (client-side code is written for JSON).

I tried to bring everything back, but because of this, gorm did not work properly.

I would like to get the same desired result as before, but at the same time do not change data types because correct operation depends on them gorm, but it seems my knowledge is not enough.

I also tried to configure the encoding of gorm for correct operation:

  1. dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  2. dbUser,
  3. password,
  4. host,
  5. port,
  6. dbName,
  7. )
  8. DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
  9. DB.AutoMigrate(Book{})
英文:

I'm trying to get a structure from a database. I use gorm and gRPC for this. Previously, I used only string data types. and there were no problems.
Here is my struct that was previously for gorm:

  1. type Book struct {
  2. BookID string `gorm:"primarykey;autoIncrement"`
  3. Name string
  4. Year string
  5. Edition string
  6. Authors []*Author `gorm:"many2many:book_author"`
  7. }

former struct generated by file.proto:

  1. type Book struct {
  2. state protoimpl.MessageState
  3. sizeCache protoimpl.SizeCache
  4. unknownFields protoimpl.UnknownFields
  5. Bookid string `protobuf:"bytes,1,opt,name=bookid,proto3" json:"bookid,omitempty"`
  6. Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
  7. Year string `protobuf:"bytes,3,opt,name=year,proto3" json:"year,omitempty"`
  8. Edition string `protobuf:"bytes,4,opt,name=edition,proto3" json:"edition,omitempty"`
  9. }

and a function on the server side that handles all this:

  1. func (*server) UpdateBook(ctx context.Context, req *pb.UpdateBookRequest) (*pb.UpdateBookResponse, error) {
  2. fmt.Println("Update Book")
  3. var book Book
  4. reqBook := req.GetBook()
  5. res := DB.Model(&book).Where("book_id=?", reqBook.Bookid).Updates(
  6. Book{Name: reqBook.Name, Year: reqBook.Year, Edition: reqBook.Edition})
  7. if res.RowsAffected == 0 {
  8. return nil, errors.New("Books not found")
  9. }
  10. return &pb.UpdateBookResponse{
  11. Book: &pb.Book{
  12. Bookid: book.BookID,
  13. Name: book.Name,
  14. Year: book.Year,
  15. Edition: book.Edition,
  16. },
  17. }, nil
  18. }

as a result, when sending a gRPC request to getBooks(), I got what I needed:

  1. {
  2. "books": [
  3. {
  4. "bookid": "",
  5. "name": "Логические ошибки",
  6. "year": "2017",
  7. "edition": "1"
  8. },
  9. {
  10. "bookid": "",
  11. "name": "LINUX API исчерпывающее руководство",
  12. "year": "2017",
  13. "edition": "1"
  14. },
  15. {
  16. "bookid": "",
  17. "name": "Сказки громовых",
  18. "year": "1999",
  19. "edition": "1"
  20. },
  21. {
  22. "bookid": "",
  23. "name": "Алгоритмы. Руководство по разработке",
  24. "year": "2022",
  25. "edition": "3"
  26. },

Then, for gorm to work correctly, I needed to change the bookid data type from string to uint32, file.proto was also regenerated.
Current gorm struct:

  1. type Book struct {
  2. BookID uint32 `gorm:"primarykey;autoIncrement"`
  3. Name string
  4. Year string
  5. Edition string
  6. Authors []*Author `gorm:"many2many:book_author"`
  7. }

Current struct generated by file.proto:

  1. type Book struct {
  2. state protoimpl.MessageState
  3. sizeCache protoimpl.SizeCache
  4. unknownFields protoimpl.UnknownFields
  5. Bookid uint32 `protobuf:"varint,1,opt,name=bookid,proto3" json:"bookid,omitempty"`
  6. Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
  7. Year string `protobuf:"bytes,3,opt,name=year,proto3" json:"year,omitempty"`
  8. Edition string `protobuf:"bytes,4,opt,name=edition,proto3" json:"edition,omitempty"`
  9. }

But now when sending a gRPC request, I get all the values in one line of the key:

  1. {
  2. "books": [
  3. {
  4. "bookid": "",
  5. "name": "",
  6. "year": "",
  7. "edition": "еские ошибки2017\"1\nH\b;LINUX API исчерпывающее руководство2017\"1\n*\bСказки громовых1999\"1\nQ\bDАлгоритмы. Руководство по разработке2022\"3\n\"\bРоман Светы2003\"1"
  8. }
  9. ]
  10. }

The reason for this behavior of the function is not clear to me, and if you send everything in json format, then the result comes correct (client-side code is written for json)

I tried to bring everything back, but because of this, gorm did not work properly

I would like to get the same desired result as before, but at the same time do not change data types, because correct operation depends on them gorm, but it seems my knowledge is not enough

I also tried to configure the encoding of gorm for correct operation

  1. dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  2. dbUser,
  3. password,
  4. host,
  5. port,
  6. dbName,
  7. )
  8. DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
  9. DB.AutoMigrate(Book{})

答案1

得分: 0

抱歉,我是个愚蠢的人,忘记在失眠中将file.proto更改为新的file.proto,因此函数的意外行为发生了。

英文:

I'm sorry, I'm a fool who forgot to change file.proto to a new file.proto in the insomnia, therefore, the unexpected behavior of the function occurred
When trying to get a structure with string and uint32 types through a gRPC request, it is all shifted into one key

huangapple
  • 本文由 发表于 2023年8月10日 22:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定