Skip to content

Commit beb3fc9

Browse files
committed
readme
1 parent cc31979 commit beb3fc9

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ type FooBar struct {
4141
}
4242
```
4343

44-
45-
4644
## Usage
4745

4846
By default tagalign will only align tags, but not sort them. But alignment and [sort feature](https://github.com/4meepo/tagalign#sort-tag) can work together or separately.
@@ -55,10 +53,13 @@ By default tagalign will only align tags, but not sort them. But alignment and [
5553
* Standalone Mode
5654

5755
Install it using `GO` or download it [here](https://github.com/4meepo/tagalign/releases).
56+
5857
```bash
5958
go install github.com/4meepo/tagalign/cmd/tagalign@latest
6059
```
60+
6161
Run it in your terminal.
62+
6263
```bash
6364
# Only align tags.
6465
tagalign -fix {package path}
@@ -68,7 +69,9 @@ By default tagalign will only align tags, but not sort them. But alignment and [
6869
tagalign -fix -sort -order "json,xml" {package path}
6970
```
7071

71-
## Sort Tag
72+
## Advanced Features
73+
74+
### Sort Tag
7275

7376
In addition to alignment, it can also sort tags with fixed order. If we enable sort with fixed order `json,xml`, the following code
7477

@@ -92,6 +95,28 @@ type SortExample struct {
9295

9396
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.
9497

98+
### Strict Style
99+
100+
Sometimes, you may want to align your tags in strict style. In this style, the tags will be sorted and aligned in the dictionary order, and the tags with the same name will be aligned together. For example, the following code
101+
102+
```go
103+
type StrictStyleExample struct {
104+
Foo int ` xml:"baz" yaml:"bar" zip:"foo" binding:"required" gorm:"column:foo" validate:"required"`
105+
Bar int `validate:"required" gorm:"column:bar" yaml:"foo" xml:"bar" binding:"required" json:"bar,omitempty" `
106+
}
107+
```
108+
109+
will be aligned to
110+
111+
```go
112+
type StrictStyleExample struct {
113+
Foo int `binding:"required" gorm:"column:foo" validate:"required" xml:"baz" yaml:"bar" zip:"foo"`
114+
Bar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"foo"`
115+
}
116+
```
117+
118+
> Note: The strict style can't run without the align or sort feature enabled.
119+
95120
## References
96121
97122
[Golang AST Visualizer](http://goast.yuroyoro.net/)

cmd/tagalign/tagalign.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ func main() {
5050
options = append(options, tagalign.WithSort(orders...))
5151
}
5252
if strict {
53-
if noalign || !sort {
54-
panic("`-strict` flag must be used with `-align` and `-sort` together")
53+
if noalign {
54+
// cannot use noalign and strict together.
55+
panic("cannot use `-noalign` and `-strict` together.")
56+
}
57+
if !sort {
58+
// cannot use strict without sort.
59+
panic("cannot use `-strict` without `-sort`.")
5560
}
5661
options = append(options, tagalign.WithStrictStyle())
5762
}

testdata/strict/example.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package strict
22

33
type AlignAndSortWithOrderExample struct {
4-
Foo int `json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" zip:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
5-
Bar int `validate:"required" yaml:"foo" xml:"bar" binding:"required" json:"bar,omitempty" gorm:"column:bar" zip:"bar" ` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
6-
FooBar int `gorm:"column:bar" validate:"required" xml:"bar" binding:"required" json:"bar,omitempty" zip:"bar" yaml:"foo"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
4+
Foo int `binding:"required" gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"baz" yaml:"bar" zip:"foo"` // want `tag is not aligned, should be: json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
5+
Bar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"foo" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
6+
FooBar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"foo" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
77
}
88

99
type AlignAndSortWithOrderExample2 struct {
10-
Foo int ` xml:"baz" yaml:"bar" zip:"foo" binding:"required" gorm:"column:foo" validate:"required"` // want `tag is not aligned, should be: yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
11-
Bar int `validate:"required" gorm:"column:bar" yaml:"foo" xml:"bar" binding:"required" json:"bar,omitempty" ` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required"`
10+
Foo int `binding:"required" gorm:"column:foo" validate:"required" xml:"baz" yaml:"bar" zip:"foo"` // want `tag is not aligned, should be: yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
11+
Bar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"foo"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required"`
1212
}
1313

1414
type AlignAndSortWithOrderExample3 struct {
15-
Foo int ` zip:"foo" gorm:"column:foo"` // want `tag is not aligned, should be: gorm:"column:foo" zip:"foo"`
16-
Bar int `binding:"required" gorm:"column:bar" validate:"required" xml:"barxxxxxxxxxxxx" yaml:"foo" zip:"bar" json:"bar,omitempty" ` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"barxxxxxxxxxxxx" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
17-
FooBar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" yaml:"foo" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
15+
Foo int ` gorm:"column:foo" zip:"foo"` // want `tag is not aligned, should be: gorm:"column:foo" zip:"foo"`
16+
Bar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"barxxxxxxxxxxxx" yaml:"foo" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"barxxxxxxxxxxxx" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
17+
FooBar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" yaml:"foo" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
1818
}

0 commit comments

Comments
 (0)