protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.11k stars 15.43k forks source link

Pass key-value pairs into Struct constructor in python #12809

Open TommyDew42 opened 1 year ago

TommyDew42 commented 1 year ago

What language does this apply to? python

Describe the problem you are trying to solve. When we initialise a Struct and want to set key-value pair on the struct, there are two steps:

struct = Struct()
struct.update(key1='value-1', key2=2)

Alternatively, we can also use json_format.ParseDict.

Describe the solution you'd like Hopefully, we can do the following:

struct = Struct(fields={'key1': 'value-1, 'key2: 2})
# or 
struct = Struct({'key1': 'value-1, 'key2: 2})

Describe alternatives you've considered

Additional context Add any other context or screenshots about the feature request here.

haberman commented 1 year ago

Does it work to add an extra constructor to the Struct class you pointed to? If so it could be a relatively simple change. If not it might be too complicated to implement.

TommyDew42 commented 1 year ago

yes that's the solution in my mind too 💯

haberman commented 1 year ago

We probably will not be able to prioritize this soon. Feel free to send a PR to implement it. If it works, we can review the API change internally and if we don't see any issues of concern, we can accept the PR.

TommyDew42 commented 1 year ago

Thanks. How do I run unittest locally?

haberman commented 1 year ago

Try: bazel test python/.... You may want to create a venv first to ensure that protobuf isn't already installed in your local Python env:

$ python -m venv /tmp/venv
$ source /tmp/venv/bin/activate
$ bazel test python/...