omitlint

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 12 Imported by: 0

README

omitlint

Test Status Go Report Card License

Checks for impossible JSON omitempty options, indicating fields that will never be omitted when encoding.

For example:

// Options are ActionRequest optional items.
type Options struct {
	Values []string `json:"values,omitempty"
}

// ActionRequest submits an action to the scheduler.
type ActionRequest struct {
	Action  string  `json:"action"`
	Options Options `json:"options,omitempty"
}

In this example, options will always appear in the encoded JSON, even if Value is empty or nil. This is because Options is a concrete struct type and not a pointer. encoding/json does not do any sort of field checking for struct types, and a concrete struct type will always have a zero value.

In order to correct, this you need to make it a pointer type:

// ActionRequest submits an action to the scheduler.
type ActionRequest struct {
	Action   string  `json:"action"`
	Options *Options `json:"options,omitempty"
}

This can be easy to overlook, especially when dealing with types that come from elsewhere. This linter will take care of this check for you by outputting messages in the form of:

/project/api/action_request.go:10:2: field "Options" is marked "omitempty", but cannot not be omitted.

Installation

Download omitlint from the releases or get the latest version from source with:

go get andy.dev/omitlint/cmd/omitlint

Usage

Shell

Check everything:

omitlint ./...

Documentation

Overview

Package omitlint defines an Analyzer that checks for fields with a json 'omitempty' option, but which have no meaningful zero value, meaning they will never be omitted.

Index

Constants

This section is empty.

Variables

View Source
var Version = "omitlint snapshot"

Functions

func NewAnalyzer

func NewAnalyzer() *analysis.Analyzer

NewAnalyzer returns a new omitlint analyzer.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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