Golang Tag Align
TagAlign is used to align and sort tags in golang struct. It can make the struct more readable and easy to maintain.
For example, this struct
type FooBar struct {
Foo int `json:"foo" validate:"required"`
Bar string `json:"bar" validate:"required"`
FooFoo int8 `json:"foo_foo" validate:"required"`
BarBar int `json:"bar_bar" validate:"required"`
FooBar struct {
Foo int `json:"foo" yaml:"foo" validate:"required"`
Bar222 string `json:"bar222" validate:"required" yaml:"bar"`
} `json:"foo_bar" validate:"required"`
BarFoo string `json:"bar_foo" validate:"required"`
BarFooBar string `json:"bar_foo_bar" validate:"required"`
}
can be aligned to:
type FooBar struct {
Foo int `json:"foo" validate:"required"`
Bar string `json:"bar" validate:"required"`
FooFoo int8 `json:"foo_foo" validate:"required"`
BarBar int `json:"bar_bar" validate:"required"`
FooBar struct {
Foo int `json:"foo" yaml:"foo" validate:"required"`
Bar222 string `json:"bar222" validate:"required" yaml:"bar"`
} `json:"foo_bar" validate:"required"`
BarFoo string `json:"bar_foo" validate:"required"`
BarFooBar string `json:"bar_foo_bar" validate:"required"`
}
In addition to alignment, it can also sort tags with fixed order. If we enable auto-sort with fixed order json,xml, the following code
type SortExample struct {
Foo int `json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" zip:"foo" validate:"required"`
Bar int `validate:"required" yaml:"foo" xml:"bar" binding:"required" json:"bar,omitempty" gorm:"column:bar" zip:"bar" `
FooBar int `gorm:"column:bar" validate:"required" xml:"bar" binding:"required" json:"bar,omitempty" zip:"bar" yaml:"foo"`
}
will be sorted and aligned to:
type SortExample struct {
Foo int `json:"foo,omitempty" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" yaml:"bar" zip:"foo"`
Bar int `json:"bar,omitempty" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" yaml:"foo" zip:"bar"`
FooBar int `json:"bar,omitempty" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" yaml:"foo" zip:"bar"`
}
The fixed order is json,xml, so the tags json and xml will be sorted and aligned first, and the rest tags will be sorted and aligned in the dictionary order.
Install
go install github.com/4meepo/tagalign/cmd/tagalign
Usage
# basic
tagalign -fix {package path}
# enable auto sort with fixed order
tagalign -fix -auto-sort -fixed-order "json,xml" {package path}
Reference
Golang AST Visualizer
Create New Golang CI Linter
Autofix Example
Integraing