Help us shape the future of Go! Take the 2026 Go Developer Survey

tagalign

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 10 Imported by: 10

README

Go Tag Align

GitHub go.mod Go version codecov GoDoc Go Report Card

TagAlign is used to align and sort tags in Go struct. It can make the struct more readable and easier 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 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

By default tagalign will only align tags, but not sort them. But alignment and sort can work together or separately.

If you don't want to align tags, you can use -noalign to disable alignment.

You can use -sort to enable sort and -order to set the fixed order of tags.

# Only align tags.
tagalign -fix {package path}
# Only sort tags with fixed order.
tagalign -fix -noalign -sort -order "json,xml" {package path}
# Align and sort together.
tagalign -fix -sort -order "json,xml" {package path}

TODO: integrate with golangci-lint

Reference

Golang AST Visualizer

Create New Golang CI Linter

Autofix Example

Integrating

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(options ...Option) *analysis.Analyzer

Types

type Helper

type Helper struct {
	// contains filtered or unexported fields
}

func (*Helper) Issues

func (w *Helper) Issues() []Issue

Issues returns all issues found by the analyzer. It is used to integrate with golangci-lint.

func (*Helper) Process added in v1.2.0

func (w *Helper) Process(pass *analysis.Pass)

type InlineFix

type InlineFix struct {
	StartCol  int // zero-based
	Length    int
	NewString string
}

type Issue

type Issue struct {
	Pos       token.Position
	Message   string
	InlineFix InlineFix
}

Issue is used to integrate with golangci-lint's inline auto fix.

func Run

func Run(pass *analysis.Pass, options ...Option) []Issue

type Mode

type Mode int
const (
	StandaloneMode Mode = iota
	GolangciLintMode
)

type Option

type Option func(*Helper)

func WithAlign added in v1.2.0

func WithAlign(enabled bool) Option

WithAlign configure whether enable tags align. Align is enabled by default.

func WithMode

func WithMode(mode Mode) Option

WithMode specify the mode of tagalign.

func WithSort added in v1.2.0

func WithSort(fixedOrder ...string) Option

WithSort enable tags sort. fixedOrder specify the order of tags, the other tags will be sorted by name. Sory is disabled by default.

Directories

Path Synopsis
cmd
tagalign command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL