-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add --camel to convert JSON keys to camelCase.
- Loading branch information
Showing
6 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package sdkclient | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
func marshalJSON(v interface{}, camelCase bool) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
if !camelCase { | ||
return json.Marshal(v) | ||
} | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch b[0] { | ||
case '{': | ||
m := map[string]any{} | ||
if err := json.Unmarshal(b, &m); err != nil { | ||
return nil, err | ||
} | ||
walkMap(m, toCamelCase) | ||
return json.Marshal(m) | ||
case '[': | ||
a := []any{} | ||
if err := json.Unmarshal(b, &a); err != nil { | ||
return nil, err | ||
} | ||
walkArray(a, toCamelCase) | ||
return json.Marshal(a) | ||
default: | ||
return b, nil | ||
} | ||
} | ||
|
||
func toCamelCase(s string) string { | ||
if len(s) == 0 { | ||
return s | ||
} | ||
return strings.ToLower(s[:1]) + s[1:] | ||
} | ||
|
||
func walkMap(m map[string]interface{}, fn func(string) string) { | ||
for key, value := range m { | ||
delete(m, key) | ||
newKey := key | ||
if fn != nil { | ||
newKey = fn(key) | ||
} | ||
if value != nil { | ||
m[newKey] = value | ||
} | ||
switch value := value.(type) { | ||
case map[string]any: | ||
walkMap(value, fn) | ||
case []interface{}: | ||
walkArray(value, fn) | ||
default: | ||
} | ||
} | ||
} | ||
|
||
func walkArray(a []interface{}, fn func(string) string) { | ||
for _, value := range a { | ||
switch value := value.(type) { | ||
case map[string]interface{}: | ||
walkMap(value, fn) | ||
case []interface{}: | ||
walkArray(value, fn) | ||
default: | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package sdkclient_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdkclient "github.com/fujiwara/awslim" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
var jsonTestCases = []struct { | ||
Name string | ||
Src any | ||
Expect string | ||
CamelCase bool | ||
}{ | ||
{ | ||
Name: "no-camel", | ||
Src: struct{ FooBar string }{FooBar: "baz"}, | ||
Expect: `{"FooBar":"baz"}`, | ||
CamelCase: false, | ||
}, | ||
{ | ||
Name: "simple", | ||
Src: struct{ FooBar string }{FooBar: "baz"}, | ||
Expect: `{"fooBar":"baz"}`, | ||
CamelCase: true, | ||
}, | ||
{ | ||
Name: "nested", | ||
Src: struct{ FooBar struct{ BazQux string } }{FooBar: struct{ BazQux string }{BazQux: "quux"}}, | ||
Expect: `{"fooBar":{"bazQux":"quux"}}`, | ||
CamelCase: true, | ||
}, | ||
{ | ||
Name: "array", | ||
Src: []any{struct{ FooBar string }{FooBar: "baz"}, 0, 1, true, nil}, | ||
Expect: `[{"fooBar":"baz"},0,1,true,null]`, | ||
CamelCase: true, | ||
}, | ||
{ | ||
Name: "string", | ||
Src: "FooBar", | ||
Expect: `"FooBar"`, | ||
CamelCase: true, | ||
}, | ||
{ | ||
Name: "number", | ||
Src: 42, | ||
Expect: `42`, | ||
CamelCase: true, | ||
}, | ||
{ | ||
Name: "true", | ||
Src: true, | ||
Expect: `true`, | ||
CamelCase: true, | ||
}, | ||
} | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
for _, tc := range jsonTestCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
b, err := sdkclient.MarshalJSON(tc.Src, tc.CamelCase) | ||
if err != nil { | ||
t.Fatalf("failed to unmarshal: %v", err) | ||
} | ||
if diff := cmp.Diff(tc.Expect, string(b)); diff != "" { | ||
t.Errorf("expect match (-got +want):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters