Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
4meepo committed Jul 26, 2023
1 parent cc31979 commit beb3fc9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ type FooBar struct {
}
```



## Usage

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.
Expand All @@ -55,10 +53,13 @@ By default tagalign will only align tags, but not sort them. But alignment and [
* Standalone Mode

Install it using `GO` or download it [here](https://github.com/4meepo/tagalign/releases).

```bash
go install github.com/4meepo/tagalign/cmd/tagalign@latest
```

Run it in your terminal.

```bash
# Only align tags.
tagalign -fix {package path}
Expand All @@ -68,7 +69,9 @@ By default tagalign will only align tags, but not sort them. But alignment and [
tagalign -fix -sort -order "json,xml" {package path}
```

## Sort Tag
## Advanced Features

### Sort Tag

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

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

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.

### Strict Style

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

```go
type StrictStyleExample struct {
Foo int ` xml:"baz" yaml:"bar" zip:"foo" binding:"required" gorm:"column:foo" validate:"required"`
Bar int `validate:"required" gorm:"column:bar" yaml:"foo" xml:"bar" binding:"required" json:"bar,omitempty" `
}
```

will be aligned to

```go
type StrictStyleExample struct {
Foo int `binding:"required" gorm:"column:foo" validate:"required" xml:"baz" yaml:"bar" zip:"foo"`
Bar int `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"foo"`
}
```

> Note: The strict style can't run without the align or sort feature enabled.
## References
[Golang AST Visualizer](http://goast.yuroyoro.net/)
Expand Down
9 changes: 7 additions & 2 deletions cmd/tagalign/tagalign.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ func main() {
options = append(options, tagalign.WithSort(orders...))
}
if strict {
if noalign || !sort {
panic("`-strict` flag must be used with `-align` and `-sort` together")
if noalign {
// cannot use noalign and strict together.
panic("cannot use `-noalign` and `-strict` together.")
}
if !sort {
// cannot use strict without sort.
panic("cannot use `-strict` without `-sort`.")
}
options = append(options, tagalign.WithStrictStyle())
}
Expand Down
16 changes: 8 additions & 8 deletions testdata/strict/example.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package strict

type AlignAndSortWithOrderExample struct {
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"`
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"`
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"`
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"`
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"`
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"`
}

type AlignAndSortWithOrderExample2 struct {
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"`
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"`
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"`
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"`
}

type AlignAndSortWithOrderExample3 struct {
Foo int ` zip:"foo" gorm:"column:foo"` // want `tag is not aligned, should be: gorm:"column:foo" zip:"foo"`
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"`
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"`
Foo int ` gorm:"column:foo" zip:"foo"` // want `tag is not aligned, should be: gorm:"column:foo" zip:"foo"`
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"`
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"`
}

0 comments on commit beb3fc9

Please sign in to comment.