Skip to content

Commit

Permalink
let's convert left key to string ^^ problem #1
Browse files Browse the repository at this point in the history
  • Loading branch information
wakeful committed Oct 16, 2018
1 parent 6f10061 commit 21a6575
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package main

import (
"fmt"
"strconv"
)

func decode(input interface{}) interface{} {
switch in := input.(type) {
case map[interface{}]interface{}:
rec := map[string]interface{}{}
for k, v := range in {
rec[k.(string)] = decode(v)

switch k.(type) {
case bool:
rec[strconv.FormatBool(k.(bool))] = decode(v)

This comment has been minimized.

Copy link
@EVODelavega

EVODelavega Oct 19, 2018

Why type assert k? switch kValue := k.(type), then use kValue as the type that it is

This comment has been minimized.

Copy link
@EVODelavega

EVODelavega Oct 19, 2018

alternatively, get rid of the switch, and leave it to fmt.Sprintf to handle all this:

rec[fmt.Sprintf("%v", k)] = decode(v)

Should be enough, let fmt handle the type assertions and/or reflection

case int:
rec[strconv.Itoa(k.(int))] = decode(v)
case float64:
rec[fmt.Sprintf("%f", k.(float64))] = decode(v)
case string:
rec[k.(string)] = decode(v)
}

}
return rec
case []interface{}:
Expand Down

0 comments on commit 21a6575

Please sign in to comment.