Skip to content

Commit

Permalink
Merge pull request #37 from nitinmohan87/36-allow_uint_as_map_keys
Browse files Browse the repository at this point in the history
Allow uint as map key types
  • Loading branch information
casualjim committed Aug 19, 2019
2 parents de649ff + 87a7e4c commit c3d0f78
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
47 changes: 33 additions & 14 deletions yaml.go
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions yaml_test.go
Expand Up @@ -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:
Expand Down

0 comments on commit c3d0f78

Please sign in to comment.