google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.31k stars 209 forks source link

Support for `struct.proto` well-known-type #513

Open negz opened 1 year ago

negz commented 1 year ago

It's possible to use google/protobuf.struct.proto today, but pretty awkward. For example:

struct = proto.file("google/protobuf/struct.proto")

msg.struct_field = struct.Struct{
  fields={
    "widgets": struct.Value(number_value=42)
  }
}

I'd prefer to be able to treat a Struct as if it were a Python dictionary. More like this:

msg.struct_field = {"widgets": 42}
msg.struct_field["watchers"] = 8

That said, I'm not sure whether it makes sense for the proto Starlark module to have native awareness of the Struct type, or whether a new protostruct module should be introduced? Perhaps something like this:

msg.struct_field = protostruct.from_dict({"widgets": 42})
negz commented 1 year ago

It seems like perhaps Python generated code just supports it "natively", per https://protobuf.dev/reference/python/python-generated/#struct. Perhaps Starlark should do the same?