Skip to content

Commit

Permalink
Merge pull request #332 from uber/jl/type_converter_map_typedef_fix
Browse files Browse the repository at this point in the history
fix map typedef key bug
  • Loading branch information
jakelarkn authored Feb 20, 2018
2 parents fde7746 + a29276c commit 0ce0f16
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 27 deletions.
45 changes: 18 additions & 27 deletions codegen/type_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,29 +391,13 @@ func (c *TypeConverter) genConverterForMap(
valueStruct, isStruct := toFieldType.ValueSpec.(*compile.StructSpec)
sourceIdentifier := fromIdentifier
_, isStringKey := toFieldType.KeySpec.(*compile.StringSpec)
keyType := "string"

if !isStringKey {
realType := compile.RootTypeSpec(toFieldType.KeySpec)
switch realType.(type) {
case *compile.StringSpec:
keyType, _ := c.getGoTypeName(toFieldType.KeySpec)
c.appendf(
"%s = make(map[%s]%s, len(%s))",
toIdentifier, keyType, typeName, sourceIdentifier,
)

keyID := c.makeUniqIdentifier("key")
valID := c.makeUniqIdentifier("value")
c.appendf(
"for %s, %s := range %s {",
keyID, valID, sourceIdentifier,
)
c.appendf(
"\t %s[%s(%s)] = %s(%s)",
toIdentifier, keyType, keyID, typeName, valID,
)
c.append("}")
return nil
keyType, _ = c.getGoTypeName(toFieldType.KeySpec)
default:
return errors.Errorf(
"could not convert key (%s), map is not string-keyed.", toField.Name)
Expand Down Expand Up @@ -445,16 +429,15 @@ func (c *TypeConverter) genConverterForMap(
sourceIdentifier = sourceListID
checkOverride = true
}

if isStruct {
c.appendf(
"%s = make(map[string]*%s, len(%s))",
toIdentifier, typeName, sourceIdentifier,
"%s = make(map[%s]*%s, len(%s))",
toIdentifier, keyType, typeName, sourceIdentifier,
)
} else {
c.appendf(
"%s = make(map[string]%s, len(%s))",
toIdentifier, typeName, sourceIdentifier,
"%s = make(map[%s]%s, len(%s))",
toIdentifier, keyType, typeName, sourceIdentifier,
)
}

Expand Down Expand Up @@ -526,10 +509,18 @@ func (c *TypeConverter) genConverterForMap(
c.append("\t", "}")
}
} else {
c.appendf(
"\t%s[%s] = %s(%s)",
toIdentifier, keyID, typeName, valID,
)
if keyType == "string" {
c.appendf(
"\t%s[%s] = %s(%s)",
toIdentifier, keyID, typeName, valID,
)

} else {
c.appendf(
"\t %s[%s(%s)] = %s(%s)",
toIdentifier, keyType, keyID, typeName, valID,
)
}
}

c.append("}")
Expand Down
74 changes: 74 additions & 0 deletions codegen/type_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,80 @@ func TestConvertMapOfString(t *testing.T) {
`), lines)
}

func TestConvertMapStringToStruct(t *testing.T) {
lines, err := convertTypes(
"Foo", "Bar",
`
struct MapValue {
1: required string one
2: optional string two
}
struct Foo {
1: required map<string, MapValue> uuidMap
}
struct Bar {
1: required map<string, MapValue> uuidMap
}`,
nil,
nil,
)

assert.NoError(t, err)
assertPrettyEqual(t, trim(`
out.UUIDMap = make(map[string]*structs.MapValue, len(in.UUIDMap))
for key1, value2 := range in.UUIDMap {
if value2 != nil {
out.UUIDMap[key1] = &structs.MapValue{}
out.UUIDMap[key1].One = string(in.UUIDMap[key1].One)
out.UUIDMap[key1].Two = (*string)(in.UUIDMap[key1].Two)
} else {
out.UUIDMap[key1] = nil
}
}
`), lines)
}

// Todo Test Override cases

func TestConvertMapTypeDefToStruct(t *testing.T) {
lines, err := convertTypes(
"Foo", "Bar",
`
typedef string UUID
struct MapValue {
1: required string one
2: optional string two
}
struct Foo {
1: required map<UUID, MapValue> uuidMap
}
struct Bar {
1: required map<UUID, MapValue> uuidMap
}`,
nil,
nil,
)

assert.NoError(t, err)
assertPrettyEqual(t, trim(`
out.UUIDMap = make(map[structs.UUID]*structs.MapValue, len(in.UUIDMap))
for key1, value2 := range in.UUIDMap {
if value2 != nil {
out.UUIDMap[key1] = &structs.MapValue{}
out.UUIDMap[key1].One = string(in.UUIDMap[key1].One)
out.UUIDMap[key1].Two = (*string)(in.UUIDMap[key1].Two)
} else {
out.UUIDMap[key1] = nil
}
}
`), lines)
}

func TestConvertMapOfStruct(t *testing.T) {
lines, err := convertTypes(
"Foo", "Bar",
Expand Down

0 comments on commit 0ce0f16

Please # to comment.