2

am using a protobuf definition file to create golang classes

file.proto

message my_message {
 <what type to use?> data = 1 [json_name = "data"]
}

generated.go

type MyMessage struct {
  data any
}

I have checked out Struct , Value and Any, though none of them actually mark the type as β€œany” in generated classes.

The closes I get is Value type which can accomodate primitives as well as structs and lists.

SpartanX1
  • 201
  • 2
  • 8

1 Answers1

1

As far as I'm aware the Go code generator doesn't have any circumstances where any/interface{} will be used for a generated field.

google.protobuf.Any is likely what you want here, but the field in the generated code will of type anypb.Any. If you absolutely require the any type be used, you'll unfortunately have to manually map to another struct.

nj_
  • 2,219
  • 1
  • 10
  • 12
  • Well I can see google.protobuf.Any gives you the advantage of referencing some other proto message type, but I don't see the need for it. I guess google.protobuf.Value would do ? – SpartanX1 Feb 15 '23 at 11:54
  • If you're only needing to hold primitive types, `google.protobuf.Struct`, `google.protobuf.ListValue`, `google.protobuf.NullValue`, then yes, `google.protobuf.Value` should be fine. – nj_ Feb 15 '23 at 12:35