文章目录
message
google.protobuf.Empty 空值
// A generic empty message that you can re-use to avoid defining duplicated
// empty messages in your APIs. A typical example is to use it as the request
// or the response type of an API method. For instance:
//
// service Foo {
// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
// }
//
// The JSON representation for `Empty` is empty JSON object `{}`.
message Empty {}
service EquipmentSvr{
// 请求空,返回空
rpc Test(google.protobuf.Empty)returns (google.protobuf.Empty) {}
}
google.protobuf.Value 动态类型的值
动态类型的值,可以是
null、数字、字符串、布尔值、递归结构值或值列表
syntax = "proto3";
package google.protobuf;
// `Value` represents a dynamically typed value which can be either
// null, a number, a string, a boolean, a recursive struct value, or a
// list of values. A producer of value is expected to set one of that
// variants, absence of any variant indicates an error.
//
// The JSON representation for `Value` is JSON value.
message Value {
// The kind of value.
oneof kind {
// Represents a null value.
NullValue null_value = 1;
// Represents a double value.
double number_value = 2;
// Represents a string value.
string string_value = 3;
// Represents a boolean value.
bool bool_value = 4;
// Represents a structured value.
Struct struct_value = 5;
// Represents a repeated `Value`.
ListValue list_value = 6;
}
}
结构体描述
"conditions": [
{
"field": "createtime",
"op": ">=",
"value": "2020-07-17 00:00:00"
},
{
"field": "id",
"op": "=",
"value": 100
}
]
遇到的问题
value值有可能是各种类型
如果定义成string,前端传int值。解析会报错:
json: cannot unmarshal number into Go value of type string
proto定义
message condition {
string field = 1;
string op = 2;
google.protobuf.Value value = 3;
}
使用
"encoding/json"
用 json 序列化 外层有oneof字段
"google.golang.org/protobuf/encoding/protojson"
用 protojson 序列化 外层无oneof字段
前端http
直接发正常的json,后端都可以解析
后端构造类型
import (
pb "xxx/proto"
structpb "github.com/golang/protobuf/ptypes/struct"
)
a := &pb.Condition{
Field: "createtime",
Op: "=",
Value: &structpb.Value{
Kind: &structpb.Value_StringValue{ StringValue: "2020-07-17 00:00:00"" }
}
}
b := &pb.Condition{
Field: "id",
Op: "=",
Value: &structpb.Value{
Kind: &structpb.Value_NumberValue{ NumberValue: 100 }
}
}
google.protobuf.Struct 结构体、object
syntax = "proto3";
package google.protobuf;
// `Struct` represents a structured data value, consisting of fields
// which map to dynamically typed values. In some languages, `Struct`
// might be supported by a native representation. For example, in
// scripting languages like JS a struct is represented as an
// object. The details of that representation are described together
// with the proto support for the language.
//
// The JSON representation for `Struct` is JSON object.
message Struct {
// Unordered map of dynamically typed values.
map<string, Value> fields = 1;
}