1

The struct in the .pb.go file generated by .proto file has three additional fields and some other things.like this:

enter image description here

When converting this struct to json, if one field is empty, the field will not appear in json. Now I know it can be done using jsonpb.Marshaler.

m := jsonpb.Marshaler{EmitDefaults: true}

Now, I coverting struct to map[string]interface{}, put it in InfluxDB. I have to convert struct to map[string]interface{}.The function NewPoint needs. like this: enter image description here enter image description here

enter image description here

I use structs.Map(value) function in go ,The transformed map has three additional fields, and running the program causes errors,like this:

{"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!

Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64
yongguangl
  • 87
  • 2
  • 11

1 Answers1

0

Protobuf generator adds some additional fields with names starting from XXX that are intended for optimizations. You can't change this behavior of protoc-gen-go.

The problem is in the way you convert struct to map[sting]interface{}. It's hard to figure out from which package exactly structs.Map comes from. 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{}. You just need to write your own slightly modified version of FillMap routine that will omit XXX fields.

Vitaly Isaev
  • 5,392
  • 6
  • 45
  • 64