Skip to content

Commit 6f01c01

Browse files
authored
Merge pull request #85 from fatih/fatih-add-old-sytle
Change `$field` keyword to `{field}`
2 parents c502265 + c2c476a commit 6f01c01

7 files changed

+54
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ prefixing them (`field_name=<your_value>`). The `--template` flag allows you to
161161
specify a custom format for the tag value to be applied.
162162

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

167167
```go
@@ -175,7 +175,7 @@ type Server struct {
175175
}
176176
```
177177

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

181181
### Transformations

main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func parseConfig(args []string) (*config, error) {
156156

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

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

422422
if c.valueFormat != "" {
423-
name = strings.ReplaceAll(c.valueFormat, "$field", name)
423+
prevName := name
424+
name = strings.ReplaceAll(c.valueFormat, "{field}", name)
425+
if name == c.valueFormat {
426+
// support old style for backward compatibility
427+
name = strings.ReplaceAll(c.valueFormat, "$field", prevName)
428+
}
424429
}
425430

426431
for _, key := range c.add {

main_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,31 @@ func TestRewrite(t *testing.T) {
4848
output: "source",
4949
structName: "foo",
5050
transform: "snakecase",
51-
valueFormat: "field_name=$field",
51+
valueFormat: "field_name={field}",
5252
},
5353
},
5454
{
5555
file: "struct_format_existing",
56+
cfg: &config{
57+
add: []string{"gaum"},
58+
output: "source",
59+
structName: "foo",
60+
transform: "snakecase",
61+
valueFormat: "field_name={field}",
62+
},
63+
},
64+
{
65+
file: "struct_format_oldstyle",
66+
cfg: &config{
67+
add: []string{"gaum"},
68+
output: "source",
69+
structName: "foo",
70+
transform: "snakecase",
71+
valueFormat: "field_name=$field",
72+
},
73+
},
74+
{
75+
file: "struct_format_existing_oldstyle",
5676
cfg: &config{
5777
add: []string{"gaum"},
5878
output: "source",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package foo
2+
3+
type foo struct {
4+
bar string `gaum:"field_name=bar"`
5+
timestamp time.Time `gaum:"@timestamp"`
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package foo
2+
3+
type foo struct {
4+
bar string
5+
timestamp time.Time `gaum:"@timestamp"`
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package foo
2+
3+
type foo struct {
4+
bar string `gaum:"field_name=bar"`
5+
t bool `gaum:"field_name=t"`
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package foo
2+
3+
type foo struct {
4+
bar string
5+
t bool
6+
}

0 commit comments

Comments
 (0)