Protocol Buffers 非标准类型记录(json映射)

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;
}

google.protobuf.Any

google.protobuf.ListValue

google.protobuf.UInt32Value

google.protobuf.BytesValue

option

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyc1211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值