Search code examples
node.jsprotocol-buffersgrpcgrpc-node

How does proto3 set the value of the map type


How to set the value of map type in static code? I have a map structure like this.

message HelloRequest {
  Maps   maps = 1;
}
message Maps {
  map<string, AudioChannelCountMapBitrateOptions> formatMapChannelCount = 1;
}

message AudioChannelCountMapBitrateOptions{
  map<string, StringVec> bitrateMap = 1;
}

message StringVec{
  repeated string strings = 1;
}

After I generate the pb file, how do I use it? StringVec provides a set method and a get method,so the strings field can be get and set like this.

const strVec = new messages.StringVec();
strVec.setStringsList(['1', '2']);
console.log(strVec.getStringsList());

But Maps and AudioChannelCountMapBitrateOptions only provide the get method, like getFormatmapchannelcountMap,getBitratemapMap. How do I set the value of this map structure so that I can get a complete map data structure. the map data structure like this? right?

formatMapChannelCount : {
  bitrateMap : ['1','2','3']
}

If you feel my description is not clear, please ask me questions.


Solution

  • https://developers.google.com/protocol-buffers/docs/reference/javascript-generated#map

    The official document has instructions, but I actually only found it now.