Git Product home page Git Product logo

tgcon's Introduction

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.

tgcon's People

Contributors

amarjeetanandsingh avatar endermanbugzjfc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.