jeudi 6 février 2020

Use google.protobuf.Value in a protobuf file with go interface{} field in a struct and vice versa

I have protobuf message which looks like this :

// Rule : Includes an entity , an operation and a comparison value
message Rule {
  string entity = 1;
  string operator = 2;
  google.protobuf.Value value = 3;
}

I am using Value here because value can be of any type.

In my Golang code where I am using the generated code :

type Rule struct {
 Entity string
 Operator string
 Value interface{}
}

Now, My question is : How do I convert type represented by Kind of google.protobuf.Value (nil, number, string, bool, struct, list) to interface{} and convert the dynamic type of interface{} in runtime back to type google.protobuf.Value ?

I can really use some help, found a sorta-solution here ( https://go.googlesource.com/protobuf/+/4c057caa84dc5e1bc6bf0d9fe8af9180a6151e07/internal/value/convert.go ) but was hoping of a simpler way to go about this.





Aucun commentaire:

Enregistrer un commentaire