diff --git a/yaml.go b/yaml.go index 435e294..ec96914 100644 --- a/yaml.go +++ b/yaml.go @@ -143,19 +143,43 @@ func (s *JSONMapItem) UnmarshalEasyJSON(in *jlexer.Lexer) { } func transformData(input interface{}) (out interface{}, err error) { + format := func(t interface{}) (string, error) { + switch k := t.(type) { + case string: + return k, nil + case uint: + return strconv.FormatUint(uint64(k), 10), nil + case uint8: + return strconv.FormatUint(uint64(k), 10), nil + case uint16: + return strconv.FormatUint(uint64(k), 10), nil + case uint32: + return strconv.FormatUint(uint64(k), 10), nil + case uint64: + return strconv.FormatUint(k, 10), nil + case int: + return strconv.Itoa(k), nil + case int8: + return strconv.FormatInt(int64(k), 10), nil + case int16: + return strconv.FormatInt(int64(k), 10), nil + case int32: + return strconv.FormatInt(int64(k), 10), nil + case int64: + return strconv.FormatInt(k, 10), nil + default: + return "", fmt.Errorf("unexpected map key type, got: %T", k) + } + } + switch in := input.(type) { case yaml.MapSlice: o := make(JSONMapSlice, len(in)) for i, mi := range in { var nmi JSONMapItem - switch k := mi.Key.(type) { - case string: - nmi.Key = k - case int: - nmi.Key = strconv.Itoa(k) - default: - return nil, fmt.Errorf("types don't match expect map key string or int got: %T", mi.Key) + if nmi.Key, err = format(mi.Key); err != nil { + return nil, err } v, ert := transformData(mi.Value) @@ -170,13 +194,8 @@ func transformData(input interface{}) (out interface{}, err error) { o := make(JSONMapSlice, 0, len(in)) for ke, va := range in { var nmi JSONMapItem - switch k := ke.(type) { - case string: - nmi.Key = k - case int: - nmi.Key = strconv.Itoa(k) - default: - return nil, fmt.Errorf("types don't match expect map key string or int got: %T", ke) + if nmi.Key, err = format(ke); err != nil { + return nil, err } v, ert := transformData(va) diff --git a/yaml_test.go b/yaml_test.go index 784de02..f2ac967 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -183,6 +183,38 @@ func TestWithYKey(t *testing.T) { } } +func TestMapKeyTypes(t *testing.T) { + d := yaml.MapSlice{ + yaml.MapItem{Key: 12345, Value: "int"}, + yaml.MapItem{Key: int8(1), Value: "int8"}, + yaml.MapItem{Key: int16(12345), Value: "int16"}, + yaml.MapItem{Key: int32(12345678), Value: "int32"}, + yaml.MapItem{Key: int64(12345678910), Value: "int64"}, + yaml.MapItem{Key: uint(12345), Value: "uint"}, + yaml.MapItem{Key: uint8(1), Value: "uint8"}, + yaml.MapItem{Key: uint16(12345), Value: "uint16"}, + yaml.MapItem{Key: uint32(12345678), Value: "uint32"}, + yaml.MapItem{Key: uint64(12345678910), Value: "uint64"}, + } + _, err := YAMLToJSON(d) + assert.NoError(t, err) + + dm := map[interface{}]interface{}{ + 12345: "int", + int8(1): "int8", + int16(12345): "int16", + int32(12345678): "int32", + int64(12345678910): "int64", + uint(12345): "uint", + uint8(1): "uint8", + uint16(12345): "uint16", + uint32(12345678): "uint32", + uint64(12345678910): "uint64", + } + _, err = YAMLToJSON(dm) + assert.NoError(t, err) +} + const withQuotedYKey = `consumes: - application/json definitions: