Skip to content

amarjeetanandsingh/tgcon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tgcon - tag as const

Generates struct field tag values as string constant.

Build Status Go Report Card

Installation

Go 1.16+

go install github.com/amarjeetanandsingh/tgcon@latest

Go version < 1.16

go get -u github.com/amarjeetanandsingh/tgcon

Why we need this tool?

Problem

We can't use string var / const in tags. Below is an illegal syntax -

type user struct {name string `json:"` + FIELD_NAME + `"`}

Keeping tags simple is good, but this becomes quite a problem when we have to access the tag value. This leads to a situation where we need to type a string literal(tag values) whenever we need to use them.

Solution

We can't use string const as tag value, but we can generate tag values as string const.

tgcon helps generate string constant for struct field tag values, and we can use the constant everywhere in the code.

  • This prevents any bug caused by typo, that escapes the build time check.
  • In case somebody changes the tag values, you just have to regenerate constants using tgcon. We can use go-generate to automate the const generation.
  • In case somebody renames a struct field, it will fail the build unless its fixed at every place(because new struct field will generate new const)

Usage

tgcon have 2 sub-commands - 1) gen 2) clean

1) tgcon gen

tgcon gen [flags]
  • It generates struct field tag values as string constant for all the eligible struct(use magic comment to filter) fields into a single file.
  • One constant file is generated per directory with file name as <package_name>_tgcon_gen.go.
  • By default, it parses only those structs which contains a magic comment, tgcon, in a comment associated to them.
  • Use --all flag to generate field tag constant for all structs irrespective of the magic comment

Options

  -a, --all                          Process all structs irrespective of magic comment
  -f, --defaultTagValFormat string   Format to generate tag value constant for fields with no tags. Default format is  Currently supports [camelcase, lispcase, pascalcase, snakecase]
  -d, --dir string                   Generate constants for the given dir (default ".")
  -h, --help                         help for gen
  -s, --onlyTagged                   Do not create constants for unTagged fields. -s means skip
  -r, --recursive                    Recursively create constants for all subdirectories too
  -t, --tags strings                 Create constants only for given comma separated list of tags. Empty means process all available tags

alt text

Generated Content

users_tgcon_gen.go

// Code generated by tgcon; DO NOT EDIT.
package users

const (

	// ---------------------  File: user.go  ---------------------
	// Struct: User
	UserAgeJson  = "age"
	UserNameJson = "name"
)

2) tgcon clean

tgcon clean [flags]
  • It reverses the action of tgcon gen command.
  • It deletes all files with _tgcon_gen.go as suffix in their name in the given directory.
  • Use -r to clean all subdirectories too.

Options

  -d, --dir string   Delete generated const file from given directory (default ".")
  -h, --help         help for clean
  -r, --recursive    Recursively delete generated const files for all subdirectories too
  -v, --verbose      Prints the generated files getting deleted.

About

tgcon generates struct field tag values as string constants.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published