musttag

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 15 Imported by: 0

README ¶

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" ./...

Documentation ¶

Overview ¶

Package musttag implements the musttag analyzer.

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func New ¶ added in v0.4.0

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 ¶ added in v0.4.0

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