Skip to content

Commit fa4ab93

Browse files
committed
added first case to lowercase naming strategy
1 parent bf8b920 commit fa4ab93

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

extra/naming_strategy.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type namingStrategyExtension struct {
1818

1919
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
2020
for _, binding := range structDescriptor.Fields {
21-
if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_'{
21+
if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_' {
2222
continue
2323
}
2424
tag, hastag := binding.Field.Tag().Lookup("json")
@@ -53,3 +53,16 @@ func LowerCaseWithUnderscores(name string) string {
5353
}
5454
return string(newName)
5555
}
56+
57+
// FirstCaseToLower one strategy to SetNamingStrategy for. It will change HelloWorld to helloWorld.
58+
func FirstCaseToLower(name string) string {
59+
newName := []rune{}
60+
for i, c := range name {
61+
if i == 0 {
62+
newName = append(newName, unicode.ToLower(c))
63+
} else {
64+
newName = append(newName, c)
65+
}
66+
}
67+
return string(newName)
68+
}

extra/naming_strategy_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,22 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) {
6464
should.Nil(err)
6565
should.Equal(`{"user_name":"allen"}`, string(output))
6666
}
67+
68+
func Test_first_case_to_lower(t *testing.T) {
69+
should := require.New(t)
70+
should.Equal("helloWorld", FirstCaseToLower("HelloWorld"))
71+
should.Equal("hello_World", FirstCaseToLower("Hello_World"))
72+
}
73+
74+
func Test_first_case_to_lower_with_first_case_already_lowercase(t *testing.T) {
75+
should := require.New(t)
76+
should.Equal("helloWorld", FirstCaseToLower("helloWorld"))
77+
}
78+
79+
func Test_first_case_to_lower_with_first_case_be_anything(t *testing.T) {
80+
should := require.New(t)
81+
should.Equal("_HelloWorld", FirstCaseToLower("_HelloWorld"))
82+
should.Equal("*HelloWorld", FirstCaseToLower("*HelloWorld"))
83+
should.Equal("?HelloWorld", FirstCaseToLower("?HelloWorld"))
84+
should.Equal(".HelloWorld", FirstCaseToLower(".HelloWorld"))
85+
}

0 commit comments

Comments
 (0)