codegen

package
v0.0.0-...-30ff297 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2018 License: MIT Imports: 10 Imported by: 0

README

Gojay code generator

This package provides a command line tool to generate gojay's marshaling and unmarshing interface implementation for custom type(s)

Get started

go install github.com/francoispqt/gojay/gojaygen

Generate code

Basic command

The basic command is straightforward and easy to use:

cd $GOPATH/src/mycoany/myproject
gojaygen -p myproject

If you just want to the output to stdout, omit the third parameter.

Using flags
  • s file/dir to path, can be a relative or absolute path
  • t root types to generate with its all dependencies (comma separated)
  • a annotation tag used to read meta data (default: json)
  • o output file (relative or absolute path)
  • p reuse object witt sync.Pool

Examples:

  • Specific type in a go package, write to a file:
gojay -s /tmp/myproj -t SomeType -o output.go
  • Specific types in a go package, with custom tag, write to a file::
gojay -s somegofile.go -a gojay -t SomeType

Generator tags

You can add tags to your structs to control:

  • the JSON key
  • skip a struct field
  • the use of omit empty methods for marshaling
  • timeFormat (java style data format)
  • timeLayout (golang time layout)
Example:
type A struct {
    Str             string  `json:"string"`
    StrOmitEmpty    string  `json:"stringOrEmpty,omitempty"`
    Skip            string  `json:"-"`
	StartTime time.Time `json:"startDate" timeFormat:"yyyy-MM-dd HH:mm:ss"`
	EndTime *time.Time `json:"endDate" timeLayout:"2006-01-02 15:04:05"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Key                string
	Init               string
	OmitEmpty          string
	TimeLayout         string
	Name               string
	Accessor           string
	Mutator            string
	Receiver           string //alias and type name
	Alias              string //object alias name
	Var                string //variable for this field
	Type               string
	RawType            string
	HelperType         string
	ComponentType      string
	RawComponentType   string
	IsPointerComponent bool

	PointerModifier     string //takes field pointer, "&" if field is not a pointer type
	DereferenceModifier string //take pointer value, i.e "*" if field has a pointer type

	ComponentPointerModifier     string //takes item pointer if needed,i.e
	ComponentDereferenceModifier string //de reference value if needed, i.e
	ComponentInitModifier        string //takes item pointer if type is not a pointer type
	ComponentInit                string //initialises component type

	DecodingMethod  string
	EncodingMethod  string
	PoolName        string //pool name associated with this field
	ResetDependency string
	Reset           string
	IsAnonymous     bool
	IsPointer       bool
	IsSlice         bool
}

Field represents a field.

func NewField

func NewField(owner *Struct, field *toolbox.FieldInfo, fieldType *toolbox.TypeInfo) (*Field, error)

NewField returns a new field

type Generator

type Generator struct {
	Pkg     string
	Code    string
	Init    string
	Imports string
	// contains filtered or unexported fields
}

func (*Generator) Generate

func (g *Generator) Generate(options *Options) error

func (*Generator) Type

func (g *Generator) Type(typeName string) *toolbox.TypeInfo

type Options

type Options struct {
	Source      string
	Dest        string
	Types       []string
	PoolObjects bool
	TagName     string
}

func NewOptionsWithFlagSet

func NewOptionsWithFlagSet(set *flag.FlagSet) *Options

NewOptionsWithFlagSet creates a new options for the supplide flagset

func (*Options) Validate

func (o *Options) Validate() error

type Struct

type Struct struct {
	*toolbox.TypeInfo

	*Generator
	Alias string
	Init  string
	Body  string
	// contains filtered or unexported fields
}

func NewStruct

func NewStruct(info *toolbox.TypeInfo, generator *Generator) *Struct

func (*Struct) Generate

func (s *Struct) Generate() (string, error)

Generate generates decoderCode + structRelease + encoderCode

Directories

Path Synopsis
test
annotated_struct
Code generated by GoJayGen.
Code generated by GoJayGen.
basic_struct
Code generated by GoJayGen.
Code generated by GoJayGen.
embedded_struct
Code generated by GoJayGen.
Code generated by GoJayGen.
pooled_struct
Code generated by GoJayGen.
Code generated by GoJayGen.

Jump to

Keyboard shortcuts

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