doudao1950 2019-03-13 11:14
浏览 557

grpc-go中模型中的空值问题

I have two servers

  1. User Server: handle all the user CRUD operation
  2. Product Server: Handle Product CRUD operation and fetch user info from the user server via gRPC call

    type User struct {
        ID string `json:"id"`
        FirstName      string     `json:"firstName"`
        MiddleName     *string    `json:"middleName,omitempty"`
        LastName       string     `json:"lastName"`
        Email          string     `json:"email"`
        Disabled       bool       `json:"disabled"`
        LastSignedInAt *time.Time `json:"lastSignedInAt,omitempty"`
        Bio            *string    `json:"bio,omitempty"`
        BirthDate      *time.Time `json:"birthDate,omitempty"`
    }
    

    Here some fields are optionals and as I am using cockroachDB(extended postgreSQL), I kept them as a pointer so it's easy in scaning variable form query result.

And here is my proto file:

message User {
    int64 id = 1;
    string firstName = 2;
    string lastName = 3;
    string email = 5;
    bool disabled = 6;
    string lastSignedInAt = 8;
    string bio = 9;
    string birthdate = 10;
    string profession = 14;
}

Now generated model from above proto file is like this:"

type User struct {
    Id                   int64                 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
    FirstName            string                `protobuf:"bytes,2,opt,name=firstName,proto3" json:"firstName,omitempty"`
    LastName             string                `protobuf:"bytes,3,opt,name=lastName,proto3" json:"lastName,omitempty"`
    Email                string                `protobuf:"bytes,4,opt,name=email,json=email,proto3" json:"email,omitempty"`
    Disabled             bool                  `protobuf:"varint,6,opt,name=disabled,proto3" json:"disabled,omitempty"`
    LastSignedInAt       string                `protobuf:"bytes,8,opt,name=lastSignedInAt,proto3" json:"lastSignedInAt,omitempty"`
    Bio                  string                `protobuf:"bytes,9,opt,name=bio,proto3" json:"bio,omitempty"`
    Birthdate            string                `protobuf:"bytes,10,opt,name=birthdate,proto3" json:"birthdate,omitempty"`
}

Now the problem is as I am using a pointer for the optional field it will store null in case of no value but on the opposite site gRPC won't understand the null value and throw the error.

I have tried google.protobuf.StringValue value as a grpc type like this

google.protobuf.StringValue lastSignedInAt = 8;

This works but the problem is I have to write condition for each field in the handler:

if lastSignedInAt != nil {
    user.LastSignedInAt = &wrappers.StringValue{Value:*lastSignedInAt}
}

What is the best way to tackle this issue? Should I change the database model or any changes in the gRPC model?

  • 写回答

3条回答 默认 最新

  • dongna2498 2019-03-13 11:31
    关注

    You cannot set the null value instead of you you can use

    oneof Examples {
        Example1 example1 = 1;
        Example2 example2 = 2;
    }
    

    when you use oneof you have to set only one value either you can set example1 or example2 you cannot use both at same time. This will resolve your issue as compared to setting the nil value.

    Approach 2:

    And by default gRPC have all the variable has initial value ex: string: ""

    One thing you can also do don't set nil value check with the condition if your value is nil then set nothing.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)