简体   繁体   中英

Is there a way to implement map<string, map<string, list<string>>> in protobuf 3?

I tried this but have scope issues.

message DataCollectionMessage {

    message subData
    {
        message SubDataList {
            repeated string data = 1;
        }
        map<string, subData> parameters = 1;
    }
    map<string,SubDataList> parameters =1;
}

Here SubDataList and subData have unresolved references.

There is just one minor issue with the protobuf in the question: the innermost map uses subData and the outer map uses SubDataList , but it should be the other way round:

message DataCollectionMessage {
    message SubData {
        message SubDataList {
            repeated string data = 1;
        }
        map<string, SubDataList> parameters = 1;
    }
    map<string, SubData> parameters = 1;
}

(I've also capitalized SubData for consistency.)

The generated Java code will have the following classes (snipped and reordered for clarity):

public static final class DataCollectionMessage {

  public Map<String, DataCollectionMessage.SubData> getParametersMap() { ... }

  public static final class SubData {

    public Map<String, DataCollectionMessage.SubData.SubDataList> getParametersMap() { ... }

    public static final class SubDataList {
      public ProtocolStringList getDataList() { ... }
    }
  }
}

Note that SubDataList has a ProtocolStringList , which is like List<String> .

If you get different results, post the protobuf file that you are using and the relevant parts of the generated Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM