Saqib Ali
Saqib Ali

Reputation: 12605

How to model map<string, map<string, int>> in ProtoBuffers 3

I'm using Go to implement an API endpoint that should return data that looks like this:

{
    "object1s": [
        {
            "object2": {
                "key1": {
                    "key3": 1,
                    "key4": 2,
                    "key5": 3
                },
                "key2": {
                    "key3": 4,
                    "key4": 5,
                    "key5": 6
                }
            }
        },
        {
            "object2": {
                "key1": {
                    "key3": 7,
                    "key4": 8,
                    "key5": 9
                },
                "key2": {
                    "key3": 10,
                    "key4": 11,
                    "key5": 12
                }
            }
        }
    ]
}

How can I model this with proto3?

I have this:

message SubObject {
  map<string, map<string, int32>> object2 = 1;
}

message ResponseMessage {
  repeated SubObject object1s = 1;
}

But I believe the syntax map<string, map<string, int>> is invalid. So what is the correct way to describe SubObject?

Upvotes: 0

Views: 637

Answers (1)

Masudur Rahman
Masudur Rahman

Reputation: 1693

Your desired way isn't supported yet.
Right now, only way to do this, is to create a message type to hold the inner map field.

message InnerObject {
    map<string, int32> object3 = 1;
}

message SubObject {
    map<string, InnerObject> object2 = 1;
}

message ResponseMessage {
    repeated SubObject object1s = 1;
}

So, you have to modify your return data as follows,

{
    "object1s": [
        {
            "object2": {
                "key1": {
                    "object3": {
                        "key3": 1,
                        "key4": 2
                    }
                }
            }
        }
    ]
}

Reference : Issue#4596

Upvotes: 1

Related Questions