Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added FirstCaseToLowercase naming strategy #633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion extra/naming_strategy.go
Expand Up @@ -18,7 +18,7 @@ type namingStrategyExtension struct {

func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
for _, binding := range structDescriptor.Fields {
if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_'{
if unicode.IsLower(rune(binding.Field.Name()[0])) || binding.Field.Name()[0] == '_' {
continue
}
tag, hastag := binding.Field.Tag().Lookup("json")
Expand Down Expand Up @@ -53,3 +53,16 @@ func LowerCaseWithUnderscores(name string) string {
}
return string(newName)
}

// FirstCaseToLower one strategy to SetNamingStrategy for. It will change HelloWorld to helloWorld.
func FirstCaseToLower(name string) string {
newName := []rune{}
for i, c := range name {
if i == 0 {
newName = append(newName, unicode.ToLower(c))
} else {
newName = append(newName, c)
}
}
return string(newName)
}
19 changes: 19 additions & 0 deletions extra/naming_strategy_test.go
Expand Up @@ -64,3 +64,22 @@ func Test_set_naming_strategy_with_private_field(t *testing.T) {
should.Nil(err)
should.Equal(`{"user_name":"allen"}`, string(output))
}

func Test_first_case_to_lower(t *testing.T) {
should := require.New(t)
should.Equal("helloWorld", FirstCaseToLower("HelloWorld"))
should.Equal("hello_World", FirstCaseToLower("Hello_World"))
}

func Test_first_case_to_lower_with_first_case_already_lowercase(t *testing.T) {
should := require.New(t)
should.Equal("helloWorld", FirstCaseToLower("helloWorld"))
}

func Test_first_case_to_lower_with_first_case_be_anything(t *testing.T) {
should := require.New(t)
should.Equal("_HelloWorld", FirstCaseToLower("_HelloWorld"))
should.Equal("*HelloWorld", FirstCaseToLower("*HelloWorld"))
should.Equal("?HelloWorld", FirstCaseToLower("?HelloWorld"))
should.Equal(".HelloWorld", FirstCaseToLower(".HelloWorld"))
}