Skip to content

Commit

Permalink
New Add/Remove prefix option (#69)
Browse files Browse the repository at this point in the history
* New Add/Remove prefix option

Some libraries like [the gorm ORM](https://github.com/go-gorm/gorm) or
the [gaum SQL query builder](https://github.com/shiftleftsecurity/gaum)
use a prefix in the tag name (ie `gorm:"column:struct_field"`,
gaum:"field_name=struct_field") to help ther Scanner/Valuer interact
with the sql library in go.
This PR adds the ability to add and remove said prefixes.
I used these two libraries as samples in the tests files to help
understand the rationale behind said feature to people reading without
context, hopefully this doesn't break any project convention.

* Add Readme section for prefix

* Replaced prefix with format

* Update main.go

Accepting fatih's suggestion on flag doc

Co-authored-by: Fatih Arslan <fatih@github.com>

* Update README.md

Accepting fatih's suggestion on `format` flag

Co-authored-by: Fatih Arslan <fatih@github.com>

* Update README.md

Accepting fatih's suggestion on readme secion title

Co-authored-by: Fatih Arslan <fatih@github.com>

* Rename formatting var to valueFormat

* Rename $value to $field for clarity

* Update readme too

Co-authored-by: Fatih Arslan <fatih@github.com>
  • Loading branch information
perrito666 and Fatih Arslan authored Oct 22, 2020
1 parent bdb461e commit 128b480
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ type Server struct {
}
```

### Formatting tag values

By default a struct tag's value is transformed from a struct's field and used directly. As an example for the field `Server string`, we generate a tag in the form: `json:"server"` (assuming `-add-tags=json` is used).

However, some third party libraries use tags in a different way and might require to them to have a particular formatting, such as is the case of prefixing them (`field_name=<your_value>`). The `--format` flag allows you to specify a custom format for the tag value to be applied.

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

```go
package main

type Server struct {
Name string `gaum:"field_name=name"`
Port int `gaum:"field_name=port"`
EnableLogs bool `gaum:"field_name=enableLogs"`
BaseDomain string `gaum:"field_name=baseDomain"`
}
```

### Transformations

We currently support the following transformations:
Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type config struct {

transform string
sort bool
valueFormat string
clear bool
clearOption bool
}
Expand Down Expand Up @@ -113,6 +114,10 @@ func realMain() error {
flagSort = flag.Bool("sort", false,
"Sort sorts the tags in increasing order according to the key name")

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

// option flags
flagRemoveOptions = flag.String("remove-options", "",
"Remove the comma separated list of options from the given keys, "+
Expand Down Expand Up @@ -146,6 +151,7 @@ func realMain() error {
clearOption: *flagClearOptions,
transform: *flagTransform,
sort: *flagSort,
valueFormat: *flagFormatting,
override: *flagOverride,
skipUnexportedFields: *flagSkipPrivateFields,
}
Expand Down Expand Up @@ -399,6 +405,10 @@ func (c *config) addTags(fieldName string, tags *structtag.Tags) (*structtag.Tag
unknown = true
}

if c.valueFormat != "" {
name = strings.ReplaceAll(c.valueFormat, "$field", name)
}

for _, key := range c.add {
splitted = strings.SplitN(key, ":", 2)
if len(splitted) >= 2 {
Expand Down
20 changes: 20 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ func TestRewrite(t *testing.T) {
transform: "snakecase",
},
},
{
file: "struct_format",
cfg: &config{
add: []string{"gaum"},
output: "source",
structName: "foo",
transform: "snakecase",
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_remove",
cfg: &config{
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/struct_format.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.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo

type foo struct {
bar string
t bool
}
6 changes: 6 additions & 0 deletions test-fixtures/struct_format_existing.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.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"`
}

0 comments on commit 128b480

Please sign in to comment.