Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix map typedef key bug #332

Merged
merged 1 commit into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the bug we hit. in the Map generation Typedef string alias (e.g. UUID) Key did have proper handling for Struct value

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