musttag

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MPL-2.0 Imports: 19 Imported by: 0

README ¶

musttag

checks pkg.go.dev goreportcard 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

The following packages are supported out of the box:

In addition, any custom package can be added to the list.

📋 Usage

musttag is already integrated into golangci-lint, and this is the recommended way to use it.

To enable the linter, add the following lines to .golangci.yml:

linters:
  enable:
    - musttag

If you'd rather prefer to use musttag standalone, you can install it via brew...

brew install tmzane/tap/musttag

...or download a prebuilt binary from the Releases page.

Then run it either directly or as a go vet tool:

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

To enable reporting a custom function, you need to add its description to .golangci.yml.

The following is an example of adding support for the hclsimple.DecodeFile function from github.com/hashicorp/hcl:

linters-settings:
  musttag:
    functions:
        # The full name of the function, including the package.
      - name: github.com/hashicorp/hcl/v2/hclsimple.DecodeFile
        # The struct tag whose presence should be ensured.
        tag: hcl
        # The position of the argument to check.
        arg-pos: 2

The same can be done via the -fn=name:tag:arg-pos flag when using musttag standalone:

musttag -fn="github.com/hashicorp/hcl/v2/hclsimple.DecodeFile:hcl:2" ./...

Documentation ¶

Overview ¶

Package musttag implements the musttag analyzer.

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func New ¶

func New(funcs ...Func) *analysis.Analyzer

New creates a new musttag analyzer. To report a custom function provide its description via Func, it will be added to the builtin ones.

Types ¶

type Func ¶

type Func struct {
	Name   string // Name is the full name of the function, including the package.
	Tag    string // Tag is the struct tag whose presence should be ensured.
	ArgPos int    // ArgPos is the position of the argument to check.
}

Func describes a function call to look for, e.g. json.Marshal.

Directories ¶

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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