diff --git a/README.md b/README.md index 2e1fc42..8703ee6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index e852147..43dda12 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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") @@ -383,22 +383,29 @@ 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 { @@ -406,19 +413,23 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag } 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, "") diff --git a/main_test.go b/main_test.go index c83a786..a16d688 100644 --- a/main_test.go +++ b/main_test.go @@ -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{ @@ -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{ diff --git a/test-fixtures/line_camelcase_add.golden b/test-fixtures/line_camelcase_add.golden index ba45bc9..0a02d3d 100644 --- a/test-fixtures/line_camelcase_add.golden +++ b/test-fixtures/line_camelcase_add.golden @@ -1,7 +1,7 @@ package foo type foo struct { - bar string `json:"bar"` + XMLFileID string `json:"xmlFileID"` MyExample bool `json:"myExample"` MyAnother []string } diff --git a/test-fixtures/line_camelcase_add.input b/test-fixtures/line_camelcase_add.input index 77c7967..a9d13a3 100644 --- a/test-fixtures/line_camelcase_add.input +++ b/test-fixtures/line_camelcase_add.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar string + XMLFileID string MyExample bool MyAnother []string } diff --git a/test-fixtures/line_camelcase_add_embedded.golden b/test-fixtures/line_camelcase_add_embedded.golden index 09b3a50..a4d3d30 100644 --- a/test-fixtures/line_camelcase_add_embedded.golden +++ b/test-fixtures/line_camelcase_add_embedded.golden @@ -1,7 +1,7 @@ package foo type foo struct { - bar `json:"bar"` + XMLFileID `json:"xmlFileID"` MyExample bool `json:"myExample"` MyAnother []string `json:"myAnother"` } diff --git a/test-fixtures/line_camelcase_add_embedded.input b/test-fixtures/line_camelcase_add_embedded.input index a9e3704..ff8aa0a 100644 --- a/test-fixtures/line_camelcase_add_embedded.input +++ b/test-fixtures/line_camelcase_add_embedded.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar + XMLFileID MyExample bool MyAnother []string } diff --git a/test-fixtures/line_constcase_add.golden b/test-fixtures/line_constcase_add.golden new file mode 100644 index 0000000..c90df10 --- /dev/null +++ b/test-fixtures/line_constcase_add.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID string `json:"XML_FILE_ID"` + MyExample bool `json:"MY_EXAMPLE"` + MyAnother []string +} diff --git a/test-fixtures/line_constcase_add.input b/test-fixtures/line_constcase_add.input new file mode 100644 index 0000000..a9d13a3 --- /dev/null +++ b/test-fixtures/line_constcase_add.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID string + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_constcase_add_embedded.golden b/test-fixtures/line_constcase_add_embedded.golden new file mode 100644 index 0000000..112d14e --- /dev/null +++ b/test-fixtures/line_constcase_add_embedded.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID `json:"XML_FILE_ID"` + MyExample bool `json:"MY_EXAMPLE"` + MyAnother []string `json:"MY_ANOTHER"` +} diff --git a/test-fixtures/line_constcase_add_embedded.input b/test-fixtures/line_constcase_add_embedded.input new file mode 100644 index 0000000..ff8aa0a --- /dev/null +++ b/test-fixtures/line_constcase_add_embedded.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_pascalcase_add.golden b/test-fixtures/line_pascalcase_add.golden index 7563be0..98e36d0 100644 --- a/test-fixtures/line_pascalcase_add.golden +++ b/test-fixtures/line_pascalcase_add.golden @@ -1,7 +1,7 @@ package foo type foo struct { - bar string `json:"Bar"` + XMLFile string `json:"XMLFile"` MyExample bool `json:"MyExample"` MyAnother []string } diff --git a/test-fixtures/line_pascalcase_add.input b/test-fixtures/line_pascalcase_add.input index 77c7967..e038faa 100644 --- a/test-fixtures/line_pascalcase_add.input +++ b/test-fixtures/line_pascalcase_add.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar string + XMLFile string MyExample bool MyAnother []string } diff --git a/test-fixtures/line_pascalcase_add_embedded.golden b/test-fixtures/line_pascalcase_add_embedded.golden index bd5c5c6..c76d8b6 100644 --- a/test-fixtures/line_pascalcase_add_embedded.golden +++ b/test-fixtures/line_pascalcase_add_embedded.golden @@ -1,7 +1,7 @@ package foo type foo struct { - bar `json:"Bar"` + XMLFile `json:"XMLFile"` MyExample bool `json:"MyExample"` MyAnother []string `json:"MyAnother"` } diff --git a/test-fixtures/line_pascalcase_add_embedded.input b/test-fixtures/line_pascalcase_add_embedded.input index a9e3704..129d303 100644 --- a/test-fixtures/line_pascalcase_add_embedded.input +++ b/test-fixtures/line_pascalcase_add_embedded.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar + XMLFile MyExample bool MyAnother []string } diff --git a/test-fixtures/line_strictcamelcase_add.golden b/test-fixtures/line_strictcamelcase_add.golden new file mode 100644 index 0000000..93ed165 --- /dev/null +++ b/test-fixtures/line_strictcamelcase_add.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID string `json:"xmlFileId"` + MyExample bool `json:"myExample"` + MyAnother []string +} diff --git a/test-fixtures/line_strictcamelcase_add.input b/test-fixtures/line_strictcamelcase_add.input new file mode 100644 index 0000000..a9d13a3 --- /dev/null +++ b/test-fixtures/line_strictcamelcase_add.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID string + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_strictcamelcase_add_embedded.golden b/test-fixtures/line_strictcamelcase_add_embedded.golden new file mode 100644 index 0000000..f814aa4 --- /dev/null +++ b/test-fixtures/line_strictcamelcase_add_embedded.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID `json:"xmlFileId"` + MyExample bool `json:"myExample"` + MyAnother []string `json:"myAnother"` +} diff --git a/test-fixtures/line_strictcamelcase_add_embedded.input b/test-fixtures/line_strictcamelcase_add_embedded.input new file mode 100644 index 0000000..ff8aa0a --- /dev/null +++ b/test-fixtures/line_strictcamelcase_add_embedded.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFileID + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_strictpascalcase_add.golden b/test-fixtures/line_strictpascalcase_add.golden new file mode 100644 index 0000000..223281f --- /dev/null +++ b/test-fixtures/line_strictpascalcase_add.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFile string `json:"XmlFile"` + MyExample bool `json:"MyExample"` + MyAnother []string +} diff --git a/test-fixtures/line_strictpascalcase_add.input b/test-fixtures/line_strictpascalcase_add.input new file mode 100644 index 0000000..e038faa --- /dev/null +++ b/test-fixtures/line_strictpascalcase_add.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFile string + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_strictpascalcase_add_embedded.golden b/test-fixtures/line_strictpascalcase_add_embedded.golden new file mode 100644 index 0000000..23a93cb --- /dev/null +++ b/test-fixtures/line_strictpascalcase_add_embedded.golden @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFile `json:"XmlFile"` + MyExample bool `json:"MyExample"` + MyAnother []string `json:"MyAnother"` +} diff --git a/test-fixtures/line_strictpascalcase_add_embedded.input b/test-fixtures/line_strictpascalcase_add_embedded.input new file mode 100644 index 0000000..129d303 --- /dev/null +++ b/test-fixtures/line_strictpascalcase_add_embedded.input @@ -0,0 +1,7 @@ +package foo + +type foo struct { + XMLFile + MyExample bool + MyAnother []string +} diff --git a/test-fixtures/line_titlecase_add.golden b/test-fixtures/line_titlecase_add.golden index 81a521a..9098bb5 100644 --- a/test-fixtures/line_titlecase_add.golden +++ b/test-fixtures/line_titlecase_add.golden @@ -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"` } diff --git a/test-fixtures/line_titlecase_add.input b/test-fixtures/line_titlecase_add.input index 77c7967..c8b64a7 100644 --- a/test-fixtures/line_titlecase_add.input +++ b/test-fixtures/line_titlecase_add.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar string + TOCFileID string MyExample bool MyAnother []string } diff --git a/test-fixtures/line_titlecase_add_embedded.golden b/test-fixtures/line_titlecase_add_embedded.golden index 9cc0990..7bf2dff 100644 --- a/test-fixtures/line_titlecase_add_embedded.golden +++ b/test-fixtures/line_titlecase_add_embedded.golden @@ -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"` } diff --git a/test-fixtures/line_titlecase_add_embedded.input b/test-fixtures/line_titlecase_add_embedded.input index a9e3704..8a41a2f 100644 --- a/test-fixtures/line_titlecase_add_embedded.input +++ b/test-fixtures/line_titlecase_add_embedded.input @@ -1,7 +1,7 @@ package foo type foo struct { - bar + TOCFileID MyExample bool MyAnother []string }