diff --git a/codegen/type_converter.go b/codegen/type_converter.go index 84cbf00d5..cac918415 100644 --- a/codegen/type_converter.go +++ b/codegen/type_converter.go @@ -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) @@ -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, ) } @@ -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("}") diff --git a/codegen/type_converter_test.go b/codegen/type_converter_test.go index dd83d2a25..a0088815d 100644 --- a/codegen/type_converter_test.go +++ b/codegen/type_converter_test.go @@ -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 uuidMap + } + + struct Bar { + 1: required map 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 uuidMap + } + + struct Bar { + 1: required map 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",