Skip to content

Latest commit

 

History

History
113 lines (85 loc) · 3.96 KB

README.md

File metadata and controls

113 lines (85 loc) · 3.96 KB

constconv

Generate Go code using user-defined templates according to constant definitions.

Installation

go install github.com/uechoco/constconv

Running constconv

Flags

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

Examples

The golden_test.go file includes some examples. Or see testdata/ directory.

Template Variables

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 details

  • $.Result.PkgName: The specified package name of the constant. For example, sets os if the argument -type=os.FileMode given.
  • $.Result.TypeName: The specified type name of the constant. For example, sets FileMode if the argument -type=os.FileMode given.
  • $.Result.RepTypeName: The specified representation of the type name. For example, sets os.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.

Value details

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.

Import details

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.

Template functions

Some template functions are pre-defined.

string case converters

These functions uses github.com/stoewer/go-strcase.

  • SnakeCase : Convert a string to lower_sanke_case. using strcase.SnakeCase .
  • KebabCase : Convert a string to lower-kebab-case. using strcase.KebabCase .
  • LowerCamelCase: Convert a string to lowerCamelCase. using strcase.LowerCamelCase .
  • UpperSnakeCase : Convert a string to UPPER_SNAKE_CASE. using strcase.UpperSnakeCase .
  • UpperKebabCase : Convert a string to UPPER-KEBAB-CASE. using strcase.UpperKebabCase .
  • UpperCamelCase: Convert a string to UpperCamelCase. using strcase.UpperCamelCase .

"strings" package functions

Some "strings" package's functions are available.

  • Title
  • ToLower
  • ToUpper
  • TrimSpace
  • TrimPrefix
  • TrimSuffix
  • Trim
  • TrimLeft
  • TrimRight

others

  • Quote: strconv.Quote
  • Unquote: Almost the same as strconv.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.