Skip to content

Commit

Permalink
Merge pull request #96 from arp242/underscoritus
Browse files Browse the repository at this point in the history
Don't double up underscores with -transform=snakecase
  • Loading branch information
fatih authored Jul 15, 2024
2 parents f700133 + 5ae6985 commit 0af24e1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
case "snakecase":
var lowerSplitted []string
for _, s := range splitted {
s = strings.Trim(s, "_")
if s == "" {
continue
}
lowerSplitted = append(lowerSplitted, strings.ToLower(s))
}

Expand Down
9 changes: 9 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func TestRewrite(t *testing.T) {
transform: "snakecase",
},
},
{
file: "struct_add_underscore",
cfg: &config{
add: []string{"json"},
output: "source",
structName: "foo",
transform: "snakecase",
},
},
{
file: "struct_add_existing",
cfg: &config{
Expand Down
5 changes: 5 additions & 0 deletions test-fixtures/struct_add_underscore.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo

type foo struct {
foo_bar string `json:"foo_bar"`
}
5 changes: 5 additions & 0 deletions test-fixtures/struct_add_underscore.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo

type foo struct {
foo_bar string
}

0 comments on commit 0af24e1

Please sign in to comment.