kevinweil/elephant-bird — pull request #412 by brirams (2014-08-08 04:25:01+00:00)
Title: Adding support for Map, Sets and Lists to ThriftToDynamicProto

Lists were easy -- just added a repeated field to the top level message for each List field. Sets are along the same line but there is no way to guarantee uniqueness when converting a thrift struct. Instead, we're handling them in the same way as lists and relying on the preconditions set by the thrift container to maintain uniqueness.

Maps were a bit trickier since protobufs doesn't have native support for them. To that end, we create a new Message type that has a key and value field who's types are based on the type parameters of the incoming thrift map. We then add that as a repeated field to the enclosing struct. It ends up looking something like the following for a Map:

[key: 1 value: "ENUM_VAL_!", key: 3 value: "ENUM_VAL_2", key: 4 value: "ENUM_VAL_3"]

The brunt of the change comes in refactoring the thriftToProtoSchema method to handle the new types. I pulled out commonly used bits of code into methods that I could then reference when building the necessary Message type used for maps.

The second big piece comes in doConvert, where we need to check for whether a container is of a base type (i32, string, etc) or not since the former can be set on the Message.Builder straightaway and the latter requires conversion of the enclosing structs.