File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ type namingStrategyExtension struct {
18
18
19
19
func (extension * namingStrategyExtension ) UpdateStructDescriptor (structDescriptor * jsoniter.StructDescriptor ) {
20
20
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 ] == '_' {
22
22
continue
23
23
}
24
24
tag , hastag := binding .Field .Tag ().Lookup ("json" )
@@ -53,3 +53,16 @@ func LowerCaseWithUnderscores(name string) string {
53
53
}
54
54
return string (newName )
55
55
}
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
+ }
Original file line number Diff line number Diff line change @@ -64,3 +64,22 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) {
64
64
should .Nil (err )
65
65
should .Equal (`{"user_name":"allen"}` , string (output ))
66
66
}
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
+ }
You can’t perform that action at this time.
0 commit comments