Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ The `{field}` word is a special keyword that is replaced by the struct tag's val

We currently support the following transformations:

* `snakecase`: `"BaseDomain"` -> `"base_domain"`
* `camelcase`: `"BaseDomain"` -> `"baseDomain"`
* `lispcase`: `"BaseDomain"` -> `"base-domain"`
* `pascalcase`: `"BaseDomain"` -> `"BaseDomain"`
* `titlecase`: `"BaseDomain"` -> `"Base Domain"`
* `snakecase`: `"DBUserID"` -> `"db_user_id"`
* `constcase`: `"DBUserID"` -> `"DB_USER_ID"`
* `camelcase`: `"DBUserID"` -> `"dbUserID"`
* `strictcamelcase`: `"DBUserID"` -> `"dbUserId"`
* `lispcase`: `"DBUserID"` -> `"db-user-id"`
* `pascalcase`: `"DBUserID"` -> `"DBUserID"`
* `strictpascalcase`: `"DBUserID"` -> `"DbUserId"`
* `titlecase`: `"DBUserID"` -> `"DB User ID"`
* `keep`: keeps the original field name

You can also pass a static value for each fields. This is useful if you use Go
Expand Down
41 changes: 26 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ func parseConfig(args []string) (*config, error) {
flagWrite = flag.Bool("w", false, "Write results to (source) file")
flagQuiet = flag.Bool("quiet", false, "Don't print result to stdout")

flagOutput = flag.String("format", "source", "Output format."+
flagOutput = flag.String("format", "source", "Output format. "+
"By default it's the whole file. Options: [source, json]")
flagModified = flag.Bool("modified", false, "read an archive of modified files from standard input")

// processing modes
flagOffset = flag.Int("offset", 0,
"Byte offset of the cursor position inside a struct."+
"Byte offset of the cursor position inside a struct. "+
"Can be anwhere from the comment until closing bracket")
flagLine = flag.String("line", "",
"Line number of the field or a range of line. i.e: 4 or 4,8")
Expand All @@ -148,13 +148,13 @@ func parseConfig(args []string) (*config, error) {
flagClearTags = flag.Bool("clear-tags", false,
"Clear all tags")
flagAddTags = flag.String("add-tags", "",
"Adds tags for the comma separated list of keys."+
"Adds tags for the comma separated list of keys. "+
"Keys can contain a static value, i,e: json:foo")
flagOverride = flag.Bool("override", false, "Override current tags when adding tags")
flagSkipUnexportedFields = flag.Bool("skip-unexported", false, "Skip unexported fields")
flagTransform = flag.String("transform", "snakecase",
"Transform adds a transform rule when adding tags."+
" Current options: [snakecase, camelcase, lispcase, pascalcase, titlecase, keep]")
"Transform adds a transform rule when adding tags. Current options: "+
"[snakecase, constcase, [strict]camelcase, lispcase, [strict]pascalcase, titlecase, keep]")
flagSort = flag.Bool("sort", false,
"Sort sorts the tags in increasing order according to the key name")

Expand Down Expand Up @@ -383,42 +383,53 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
return tags, nil
}

StrictTitle := func(s string) string {
return strings.Title(strings.ToLower(s))
}

splitted := camelcase.Split(fieldName)
name := ""

unknown := false
switch c.transform {
switch Title, ConvertCase := strings.Title, strings.ToLower; c.transform {
case "constcase":
ConvertCase = strings.ToUpper
fallthrough
case "snakecase":
var lowerSplitted []string
var convertedSplitted []string
for _, s := range splitted {
s = strings.Trim(s, "_")
if s == "" {
continue
}
lowerSplitted = append(lowerSplitted, strings.ToLower(s))
convertedSplitted = append(convertedSplitted, ConvertCase(s))
}

name = strings.Join(lowerSplitted, "_")
name = strings.Join(convertedSplitted, "_")
case "lispcase":
var lowerSplitted []string
for _, s := range splitted {
lowerSplitted = append(lowerSplitted, strings.ToLower(s))
}

name = strings.Join(lowerSplitted, "-")
case "strictcamelcase":
Title = StrictTitle
fallthrough
case "camelcase":
var titled []string
for _, s := range splitted {
titled = append(titled, strings.Title(s))
for _, s := range splitted[1:] {
titled = append(titled, Title(s))
}

titled[0] = strings.ToLower(titled[0])

name = strings.Join(titled, "")
name = strings.ToLower(splitted[0]) + strings.Join(titled, "")
case "strictpascalcase":
Title = StrictTitle
fallthrough
case "pascalcase":
var titled []string
for _, s := range splitted {
titled = append(titled, strings.Title(s))
titled = append(titled, Title(s))
}

name = strings.Join(titled, "")
Expand Down
54 changes: 54 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,42 @@ func TestRewrite(t *testing.T) {
transform: "lispcase",
},
},
{
file: "line_constcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "constcase",
},
},
{
file: "line_constcase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "constcase",
},
},
{
file: "line_strictcamelcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "strictcamelcase",
},
},
{
file: "line_strictcamelcase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "strictcamelcase",
},
},
{
file: "line_camelcase_add",
cfg: &config{
Expand Down Expand Up @@ -393,6 +429,24 @@ func TestRewrite(t *testing.T) {
transform: "snakecase",
},
},
{
file: "line_strictpascalcase_add",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,5",
transform: "strictpascalcase",
},
},
{
file: "line_strictpascalcase_add_embedded",
cfg: &config{
add: []string{"json"},
output: "source",
line: "4,6",
transform: "strictpascalcase",
},
},
{
file: "line_pascalcase_add",
cfg: &config{
Expand Down
2 changes: 1 addition & 1 deletion test-fixtures/line_camelcase_add.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string `json:"bar"`
XMLFileID string `json:"xmlFileID"`
MyExample bool `json:"myExample"`
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_camelcase_add.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string
XMLFileID string
MyExample bool
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_camelcase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar `json:"bar"`
XMLFileID `json:"xmlFileID"`
MyExample bool `json:"myExample"`
MyAnother []string `json:"myAnother"`
}
2 changes: 1 addition & 1 deletion test-fixtures/line_camelcase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar
XMLFileID
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_constcase_add.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID string `json:"XML_FILE_ID"`
MyExample bool `json:"MY_EXAMPLE"`
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_constcase_add.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID string
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_constcase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID `json:"XML_FILE_ID"`
MyExample bool `json:"MY_EXAMPLE"`
MyAnother []string `json:"MY_ANOTHER"`
}
7 changes: 7 additions & 0 deletions test-fixtures/line_constcase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID
MyExample bool
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_pascalcase_add.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string `json:"Bar"`
XMLFile string `json:"XMLFile"`
MyExample bool `json:"MyExample"`
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_pascalcase_add.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string
XMLFile string
MyExample bool
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_pascalcase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar `json:"Bar"`
XMLFile `json:"XMLFile"`
MyExample bool `json:"MyExample"`
MyAnother []string `json:"MyAnother"`
}
2 changes: 1 addition & 1 deletion test-fixtures/line_pascalcase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar
XMLFile
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictcamelcase_add.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID string `json:"xmlFileId"`
MyExample bool `json:"myExample"`
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictcamelcase_add.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID string
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictcamelcase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID `json:"xmlFileId"`
MyExample bool `json:"myExample"`
MyAnother []string `json:"myAnother"`
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictcamelcase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFileID
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictpascalcase_add.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFile string `json:"XmlFile"`
MyExample bool `json:"MyExample"`
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictpascalcase_add.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFile string
MyExample bool
MyAnother []string
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictpascalcase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFile `json:"XmlFile"`
MyExample bool `json:"MyExample"`
MyAnother []string `json:"MyAnother"`
}
7 changes: 7 additions & 0 deletions test-fixtures/line_strictpascalcase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

type foo struct {
XMLFile
MyExample bool
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_titlecase_add.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string `json:"Bar"`
TOCFileID string `json:"TOC File ID"`
MyExample bool `json:"My Example"`
MyAnother []string `json:"My Another"`
}
2 changes: 1 addition & 1 deletion test-fixtures/line_titlecase_add.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar string
TOCFileID string
MyExample bool
MyAnother []string
}
2 changes: 1 addition & 1 deletion test-fixtures/line_titlecase_add_embedded.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar `json:"Bar"`
TOCFileID `json:"TOC File ID"`
MyExample bool `json:"My Example"`
MyAnother []string `json:"My Another"`
}
2 changes: 1 addition & 1 deletion test-fixtures/line_titlecase_add_embedded.input
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package foo

type foo struct {
bar
TOCFileID
MyExample bool
MyAnother []string
}