Skip to content

Commit

Permalink
trim spaces between tags when filed is single.
Browse files Browse the repository at this point in the history
  • Loading branch information
4meepo committed Jul 27, 2023
1 parent 26b0a07 commit aec487a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 2 additions & 7 deletions tagalign.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,9 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
sortBy(w.fixedTagOrder, tags)
}

if reflect.DeepEqual(originalTags, tags.Tags()) {
// if tags order not changed, do nothing
continue
}

newTagValue := fmt.Sprintf("`%s`", tags.String())
if field.Tag.Value == newTagValue {
// nothing changed
if reflect.DeepEqual(originalTags, tags.Tags()) && field.Tag.Value == newTagValue {
// if tags order not changed, do nothing
continue
}

Expand Down
8 changes: 8 additions & 0 deletions tagalign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ func Test_strictStyle(t *testing.T) {
assert.NoError(t, err)
analysistest.Run(t, sort, a)
}

func Test_alignSingleField(t *testing.T) {
// only align
a := NewAnalyzer()
unsort, err := filepath.Abs("testdata/single_field")
assert.NoError(t, err)
analysistest.Run(t, unsort, a)
}
10 changes: 10 additions & 0 deletions testdata/single_field/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package singlefield

type FooBar struct {
Foo int `json:"foo" validate:"required"`
Bar string `json:"bar" validate:"required"`

FooFoo int8 `json:"foo_foo" validate:"required"` // want `json:"foo_foo" validate:"required"`

BarBar int `json:"bar_bar" validate:"required"`
}
4 changes: 2 additions & 2 deletions testdata/sortorder/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type SortWithOrderExample struct {
FooBar int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" `
// aligned but not sorted, should be reported
BarFoo int `xml:"bar" yaml:"foo" json:"bar,omitempty" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"`
// not aligned but sorted, should not be reported
FooBarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"`
// not aligned but sorted, should trim spaces between tags
FooBarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"`
}

0 comments on commit aec487a

Please sign in to comment.