Skip to content

๐Ÿ‘ฎโ€โ™‚๏ธ A Go linter that enforces field tags in (un)marshaled structs

License

Notifications You must be signed in to change notification settings

SVilgelm/musttag

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

musttag

ci docs report codecov

A Go linter that enforces field tags in (un)marshaled structs

๐Ÿ“Œ About

musttag checks that exported fields of a struct passed to a Marshal-like function are annotated with the relevant tag:

// BAD:
var user struct {
	Name string
}
data, err := json.Marshal(user)

// GOOD:
var user struct {
	Name string `json:"name"`
}
data, err := json.Marshal(user)

The rational from Uber Style Guide:

The serialized form of the structure is a contract between different systems. Changes to the structure of the serialized form, including field names, break this contract. Specifying field names inside tags makes the contract explicit, and it guards against accidentally breaking the contract by refactoring or renaming fields.

๐Ÿš€ Features

musttag supports these packages out of the box:

  • encoding/json
  • encoding/xml
  • gopkg.in/yaml.v3
  • github.com/BurntSushi/toml
  • github.com/mitchellh/mapstructure
  • ...and any custom one

๐Ÿ“ฆ Install

Go

go install github.com/junk1tm/musttag/cmd/musttag@latest

Brew

brew install junk1tm/tap/musttag

Manual

Download a prebuilt binary from the Releases page.

๐Ÿ“‹ Usage

As a standalone binary:

musttag ./...

Via go vet:

go vet -vettool=$(which musttag) ./...

Custom packages

The -fn=name:tag:argpos flag can be used to report functions from custom packages, where

  • name is the full name of the function, including the package
  • tag is the struct tag whose presence should be ensured
  • argpos is the position of the argument to check

For example, to support the sqlx.Get function:

musttag -fn="github.com/jmoiron/sqlx.Get:db:1" ./...

About

๐Ÿ‘ฎโ€โ™‚๏ธ A Go linter that enforces field tags in (un)marshaled structs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%