Generate Go code using user-defined templates according to constant definitions.
go install github.com/uechoco/constconv
Usage of constconv:
constconv -type T -template F [optional flags] [directory]
constconv -type T -template F [optional flags] files... # Must be a single package
-data string
semicolon-separated list of extra template data. each data coupled by equal sign; e.g. "typename=Foo;prefix=Bar"
-output string
output file name; default srcdir/<snake-cased-type>_constconv.go
-tags string
comma-separated list of build tags to apply
-template string
template file path which is relative from the srcdir; must be set
-type string
comma-separated list of type names; must be set
The golden_test.go
file includes some examples. Or see testdata/
directory.
These template variables are available.
$.DoNotEdit
: A DO-NOT-EDIT comment. Such as the below:// DO NOT EDIT.; Code generated by "constconv -type=constant.Kind -template=const_kind.tmpl testdata/const_kind.go
$.Extra
: Extra template data given by-data
argument. For example, if the argument-data=package=dayofweek;typename=DayOfWeek
given, the below template variables are available:{{ $.Extra.package }} // dayofweek {{ $.Extra.typename }} // DayOfWeek
$.BasePackageName
: The package name to which the given file belongs.$.Result
: The result of parsing the type. This is the first element of$.ResultList
.$.ResultList
: The list of result.
$.Result.PkgName
: The specified package name of the constant. For example, setsos
if the argument-type=os.FileMode
given.$.Result.TypeName
: The specified type name of the constant. For example, setsFileMode
if the argument-type=os.FileMode
given.$.Result.RepTypeName
: The specified representation of the type name. For example, setsos.FileMode
if the argument-type=os.FileMode
given.$.Result.Values
: The slice of constant definitions.$.Result.Imports
: The slice of import definitions that defined at parsed package.
When the $.Result.Values
expanded as {{ range $Value := $.Result.Values }}
:
$Value.Name
: The constant name.$Value.Str
: The string representation given by the "go/constant" package.$Value.ExactStr
: The exact string representation given by the "go/constant" package.$Value.Kind
: The kind of constant given by the "go/constant" package.
When the $.Result.Imports
expanded as {{ range $Import := $.Result.Imports }}
:
$Import.Name
: The import name.$Import.Path
: The import path.$Import.Comment
: The import comment.$Import.Doc
: The import doc.
Some template functions are pre-defined.
These functions uses github.com/stoewer/go-strcase.
SnakeCase
: Convert a string to lower_sanke_case. usingstrcase.SnakeCase
.KebabCase
: Convert a string to lower-kebab-case. usingstrcase.KebabCase
.LowerCamelCase
: Convert a string to lowerCamelCase. usingstrcase.LowerCamelCase
.UpperSnakeCase
: Convert a string to UPPER_SNAKE_CASE. usingstrcase.UpperSnakeCase
.UpperKebabCase
: Convert a string to UPPER-KEBAB-CASE. usingstrcase.UpperKebabCase
.UpperCamelCase
: Convert a string to UpperCamelCase. usingstrcase.UpperCamelCase
.
Some "strings" package's functions are available.
Title
ToLower
ToUpper
TrimSpace
TrimPrefix
TrimSuffix
Trim
TrimLeft
TrimRight
Quote
:strconv.Quote
Unquote
: Almost the same asstrconv.Unquote
, but returns the input string if the error occurred.DropDot
: Strip "." characters from a string.DropUnderscore
: Strip "_" characters from a string.DropHyphen
: Strip "-" characters from a string.