gen-struct-fields

command module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 15 Imported by: 0

README

Gen-struct-fields

Gen-struct-fields is a tool to generate the names of the structure fields in various formats in Go code.

The generated code can be used, for example, to simplify work with the database and etc.

Install

Gen-struct-fields can be installed as any other go command:

go get github.com/abramlab/gen-struct-fields@v0.1.2

After that, the gen-struct-fields executable will be in "$GOPATH/bin" folder, and you can use it with go generate

How to use

gen-struct-fields -struct=User -custom_name=user

or with go generate:

//go:generate gen-struct-fields -struct=User -custom_name=user

By default, for each structure will be generated a separate file, with name: <struct_name>_fields.go.

You can use output tag to specified custom output file name, then all data will be generated in it.

gen-struct-fields -struct=User -output=custom_generated_file.go

For multiple generation, separate tag struct value with coma, for example:

gen-struct-fields -struct=User,Car,Plane -tag=custom_tag -custom_name=user,,plane

will generate fields for User,Car,Plane structs with custom names for User and Plane.

If you want to specify custom tag name(by default it is field), use "-tag":

gen-struct-fields -struct=User -custom_name=user -tag=custom_tag

Simple example

Initial data:

//go:generate gen-struct-fields -struct=User -tag=custom_tag -custom_name=user

type User struct {
	Name     string `custom_tag:"name"`
	Login    string `custom_tag:"username"`
	Password string `custom_tag:"-"`
	AuthType int    `custom_tag:"auth_type"`
}

Output(generated) data:

const UserName = "user"

var UserFields = struct {
	Name     string
	Login    string
	AuthType string
}{
	Name:     "name",
	Login:    "username",
	AuthType: "auth_type",
}

var UserFieldsArray = []string{
	UserFields.Name,
	UserFields.Login,
	UserFields.AuthType,
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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