Skip to content

Commit

Permalink
Merge pull request #85 from fatih/fatih-add-old-sytle
Browse files Browse the repository at this point in the history
Change `$field` keyword to `{field}`
  • Loading branch information
fatih authored Aug 25, 2021
2 parents c502265 + c2c476a commit 6f01c01
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ prefixing them (`field_name=<your_value>`). The `--template` flag allows you to
specify a custom format for the tag value to be applied.

```
$ gomodifytags -file demo.go -struct Server -add-tags gaum -template "field_name=$field"
$ gomodifytags -file demo.go -struct Server -add-tags gaum -template "field_name={field}"
```

```go
Expand All @@ -175,7 +175,7 @@ type Server struct {
}
```

The `$field` is a special keyword that is replaced by the struct tag's value
The `{field}` word is a special keyword that is replaced by the struct tag's value
**after** the [transformation](https://github.com/fatih/gomodifytags#transformations).

### Transformations
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func parseConfig(args []string) (*config, error) {

// formatting
flagFormatting = flag.String("template", "",
"Format the given tag's value. i.e: \"column:$field\", \"field_name=$field\"")
"Format the given tag's value. i.e: \"column:{field}\", \"field_name={field}\"")

// option flags
flagRemoveOptions = flag.String("remove-options", "",
Expand Down Expand Up @@ -420,7 +420,12 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
}

if c.valueFormat != "" {
name = strings.ReplaceAll(c.valueFormat, "$field", name)
prevName := name
name = strings.ReplaceAll(c.valueFormat, "{field}", name)
if name == c.valueFormat {
// support old style for backward compatibility
name = strings.ReplaceAll(c.valueFormat, "$field", prevName)
}
}

for _, key := range c.add {
Expand Down
22 changes: 21 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,31 @@ func TestRewrite(t *testing.T) {
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name=$field",
valueFormat: "field_name={field}",
},
},
{
file: "struct_format_existing",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name={field}",
},
},
{
file: "struct_format_oldstyle",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
valueFormat: "field_name=$field",
},
},
{
file: "struct_format_existing_oldstyle",
cfg: &config{
add: []string{"gaum"},
output: "source",
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/struct_format_existing_oldstyle.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo

type foo struct {
bar string `gaum:"field_name=bar"`
timestamp time.Time `gaum:"@timestamp"`
}
6 changes: 6 additions & 0 deletions test-fixtures/struct_format_existing_oldstyle.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo

type foo struct {
bar string
timestamp time.Time `gaum:"@timestamp"`
}
6 changes: 6 additions & 0 deletions test-fixtures/struct_format_oldstyle.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo

type foo struct {
bar string `gaum:"field_name=bar"`
t bool `gaum:"field_name=t"`
}
6 changes: 6 additions & 0 deletions test-fixtures/struct_format_oldstyle.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo

type foo struct {
bar string
t bool
}

0 comments on commit 6f01c01

Please sign in to comment.