2

I've been trying to add values to a ScalarMapContainer in Python. This object was autogenerated by Google's Protobuf3 library from the .proto:

map<string, string> mapName = 4;

According to the scant docs I found the ScalarMapContainer should act basically like a normal dict but doesn't.

I've tried accessing it like a normal dict. I deep copied it then called MergeFrom on the copy. I also created a new ScalarMapConatiner then called MergeFrom on it.

# normal dict
x.mapName["key"] = "value"
from google.protobuf.json_format import MessageToJson 
print(MessageToJson(x)) # key/value not present

# deep copy
copy = deepcopy(x.mapName)
copy["key"] = "value"
x.mapName.MergeFrom(copy)
print(MessageToJson(x)) # key/value not present

# new object
myMap = ScalarMapContainer()
myMap["key"] = "value"
x.mapName.MergeFrom(myMap) # crashes

I'm not sure what else to try to get this working.

Thank you.

1 Answer 1

5

Say I have a protobuffer message as:

[...]
message ResponseMap {
    map<string, int32> pairs = 1;
}
[...]

and build my entire proto using grpc_tools.protoc.

The server-side could fill the message as if it was a dict. Example:

def get_map(self, request, context):
    result = gen_pb2.ResponseMap()
    # Fill in the map
    result.pairs['a'] = 2
    result.pairs['b'] = 3
    return result

The client-side, on the other hand, could read the key-value pairs as:

response = stub.get_map([...])
for k, v in response.pairs.items():
    print(f'{k}: {v}')

items method would let you iterate over the key-value pairs as if it was a dict.

In this simple example, pairs is a ScalarMapContainer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.