[英]protoc-gen-go struct xxx covert to map[string]interface{}
The struct in the .pb.go file generated by .proto file has three additional fields and some other things.like this: 由.proto文件生成的文件.pb.go的结构有三个附加字段和其他一些things.like这样的:
When converting this struct to json , if one field is empty, the field will not appear in json. 当将此结构转换为json时 ,如果一个字段为空,则该字段不会出现在json中。 Now I know it can be done using jsonpb.Marshaler .
现在我知道可以使用jsonpb.Marshaler来完成。
m := jsonpb.Marshaler{EmitDefaults: true}
Now, I coverting struct to map[string]interface{} , put it in InfluxDB . 现在,我覆盖将结构 映射到map [string] interface {} ,并将其放入InfluxDB中 。 I have to convert struct to map[string]interface{}.The function NewPoint needs.
我必须将结构转换为map [string] interface {} 。NewPoint需要此功能。 like this:
像这样:
I use structs.Map(value) function in go ,The transformed map has three additional fields, and running the program causes errors,like this: 我在go中使用structs.Map(value)函数,转换后的地图具有三个附加字段,运行程序会导致错误,如下所示:
{"error":"unable to parse 'txt,severity=1 CurrentValue=\"1002\",MetricAlias=\"CPU\",XXX_sizecache=0i,XXX_unrecognized= 1552551101': missing field value"}
When I remove these three fields, the program runs OK.These three fields are automatically generated, and I have a lot of structs. 当我删除这三个字段时,程序运行正常,这三个字段是自动生成的,并且我有很多结构。 What should I do?Thank you!
我该怎么办?谢谢!
Protobuf generator adds some additional fields with names starting from XXX
that are intended for optimizations. Protobuf生成器添加了一些其他字段,其名称以
XXX
开头,用于优化。 You can't change this behavior of protoc-gen-go
. 您无法更改
protoc-gen-go
这种行为。
The problem is in the way you convert struct
to map[sting]interface{}
. 问题在于将
struct
转换为map[sting]interface{}
。 It's hard to figure out from which package exactly structs.Map
comes from. 很难确定
structs.Map
到底来自哪个包。 Seems like it goes from here: https://github.com/fatih/structs/blob/master/structs.go#L89 - this code uses reflect
to iterate through all fields of the structure and push them to map[sting]interface{}
. 好像是从这里开始的: https : //github.com/fatih/structs/blob/master/structs.go#L89-此代码使用
reflect
来遍历结构的所有字段并将它们推到map[sting]interface{}
。 You just need to write your own slightly modified version of FillMap
routine that will omit XXX
fields. 您只需要编写自己的
FillMap
例程的稍作修改的版本FillMap
省略XXX
字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.