structtagger

command
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

StructTagger is a tool to automate the creation of constants that match the tag value of a struct. Given the name of a struct type T and that has tag Y defined for at least one of its fields Z, structtagger will create a new self-contained Go source file implementing

const TZY = "Yvalue"

The file is created in the same package and directory as the package that defines T, unless the target is an import path, in which the package of the current directory will be used. It has helpful defaults designed for use with go generate.

For example, given this snippet,

package autos

type Car struct {
	Make  string `json:"make"`
	Model string `json:"model"`
}

running this command

structtagger -type=Car -tag=json

in the same directory will create the file car_structtags.go, in package autos, containing

const (
	CarMakeJson  = "make"
	CarModelJson = "model"
)

Typically this process would be run using go generate, like this:

//go:generate structtagger -type=Car -tag=json

With no arguments, it processes the package in the current directory for the tag "json". Otherwise, the arguments must name a single directory holding a Go package, the import path of a Go package, or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_structtags.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

The -tag flag accepts a comma-separated list of struct tags so a single run can generate constants for multiple struct tags. The default tag is json.

Jump to

Keyboard shortcuts

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