goplater

command module
v0.0.0-...-471202d Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

README

goplater

goplater is a tool for automating the creation of methods that satisfy some built-in interfaces or those defined in the templates.

Usage

goplater can be run from console in directory with target type or with help go generate:

$ goplater enum --type MyType

Or

package main

//go:generate goplater enum --type MyType
type MyType int 

const (
	MyTypeA MyType   = iota
	MyTypeB
	MyTypeC
)
$  ./goplater 
NAME:
   goplater - auto generate, dont repeat

USAGE:
   goplater [global options] command [command options] [arguments...]

VERSION:
   2.0

COMMANDS:
     enum     generate var and methods for the iota-enums
     model    generate code for structure by template
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version

Enum

Command: goplater enum

Implements predefined interfaces for a user-defined integer type with iota-constants.

Interface list:

  1. fmt.Stringer - String() string;
  2. json.Marshaler - MarshalJSON() ([]byte, error);
  3. json.Unmarshaler - UnmarshalJSON([]byte) error;
  4. driver.Valuer - Value() (Value, error);
  5. sql.Scanner - Scan(src interface{}) error;
  6. Validator - Validate() error.

Predefined variables :

  • var def<Type>ValueToName map[<Type>]string - matching a constant and its string representation;
  • var def<Type>NameToValue map[string]<Type> - matching a string representation and constant;
  • var Err<Type>Invalid error - error.

All methods and maps can be pre-determined before generation, and at run they will be omitted.

List of arguments:

Flag Type Description
type string The name of the target type or types for code generation
transform snake, kebab, space, none A rule describing the strategy for converting constant names to a string. Default: none
tprefix true, false add type name prefix into string values or not. Default: false
prefix string A prefix to be added to the output file
suffix string A suffix to be added to the output. Default: "_enums"
merge bool Merge all output into one file, if set prefix and suffix will be ignored. Default: false

Example

goplater enum --type ShirtSize,WeekDay --merge true
Model

Command: goplater model

Find the structure definition, analyze it and fill in the transposed template.

Available fields for template:

Flag Type Description
Package string Name of package with type
TypeName string Type name
TypeString string camelCased type name
Fields []Field List of structure field definitions
Field.Name string Name of filed
Field.FType string Type of field
Field.Tags map[string]string Field tags

List of arguments:

Flag Type Description
tmpl string path to the templates; required
type string list of type names; required
prefix string prefix to be added to the output file
suffix string suffix to be added to the output file

ToDo

  • Improve documentation, add tests
  • Add string types support
  • Add bitmap types support
  • Add custom template support

License

This package is based on https://github.com/campoy/jsonenums.

Documentation

Overview

JSONenums is a tool to automate the creation of methods that satisfy the fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces. Given the name of a (signed or unsigned) integer type T that has constants defined, goplater will create a new self-contained Go source file implementing

func (t T) String() string
func (t T) MarshalJSON() ([]byte, error)
func (t *T) UnmarshalJSON([]byte) error

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

JSONenums is a simple implementation of a concept and the code might not be the most performant or beautiful to read.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

goplater -type=Pill

in the same directory will create the file pill_jsonenums.go, in package painkiller, containing a definition of

func (r Pill) String() string
func (r Pill) MarshalJSON() ([]byte, error)
func (r *Pill) UnmarshalJSON([]byte) error

That method will translate the value of a Pill constant to the string representation of the respective constant name, so that the call fmt.Print(painkiller.Aspirin) will print the string "Aspirin".

Typically this process would be run using go generate, like this:

//go:generate goplater -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_jsonenums.go, where t is the lower-cased name of the first type listed. The suffix can be overridden with the -suffix flag and a prefix may be added with the -prefix flag.

Directories

Path Synopsis
generated by goplater enum --type ShirtSize,WeekDay --merge true; DO NOT EDIT
generated by goplater enum --type ShirtSize,WeekDay --merge true; DO NOT EDIT
Package parser parses Go code and keeps track of all the types defined and provides access to all the constants defined for an int type.
Package parser parses Go code and keeps track of all the types defined and provides access to all the constants defined for an int type.

Jump to

Keyboard shortcuts

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