generator

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2018 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTagKey = "json"

DefaultTagKey is the default tag key to use when parsing stuct fields.

Variables

This section is empty.

Functions

func CanUnmarshal

func CanUnmarshal(t types.Type) bool

CanUnmarshal returns if can be unmarshaled

func IsGeneratedByNJSON

func IsGeneratedByNJSON(f *ast.File) bool

IsGeneratedByNJSON checks if a file begins with the njson generated header comment.

func ParseFieldTag

func ParseFieldTag(tag, key string) (name string, omitempty, ok bool)

ParseFieldTag parses a field tag to get a json name and omitempty info.

Types

type Generator

type Generator struct {
	*meta.Package
	// contains filtered or unexported fields
}

Generator is a source code generator for njson unmarshal methods.

func New

func New(path string, targetPkg string, options ...Option) (*Generator, error)

New creates a new Generator for a package named targetPkg and parses the specified path.

func (*Generator) AllStructs

func (g *Generator) AllStructs() (all []string)

AllStructs returns all structs from the package.

func (*Generator) AppendMethod

func (o *Generator) AppendMethod() string

func (*Generator) Appender

func (g *Generator) Appender(typName string) (c meta.Code)

func (*Generator) BasicUnmarshaler

func (g *Generator) BasicUnmarshaler(t types.Type, b *types.Basic) (code meta.Code)

BasicUnmarshaler generates the code block to unmarshal a basic type.

func (*Generator) DumpTo

func (g *Generator) DumpTo(w io.Writer) error

DumpTo writes the generated file without checking and formatting.

func (*Generator) EnsurePath

func (g *Generator) EnsurePath(path meta.FieldPath) (code meta.Code)

EnsurePath generates a code block to ensure the path to an embedded pointer to struct has no nils.

func (*Generator) EnsureReversePath

func (g *Generator) EnsureReversePath(path meta.FieldPath, code meta.Code) meta.Code

func (*Generator) Filename

func (g *Generator) Filename() (name string)

Filename returns the filename to write output.

func (*Generator) Header

func (g *Generator) Header() string

Header returns the header code for the generated file.

func (*Generator) Import

func (g *Generator) Import(imports ...*types.Package)

Import adds packages to import in the generated file

func (*Generator) InterfaceUnmarshaler

func (g *Generator) InterfaceUnmarshaler(t types.Type, b *types.Interface) (code meta.Code)

InterfaceUnmarshaler generates the code block to unmarshal an empty interface.

func (*Generator) JSONFieldName

func (o *Generator) JSONFieldName(name string) string

func (*Generator) JSONUnmarshaler

func (g *Generator) JSONUnmarshaler(t types.Type) (code meta.Code)

JSONUnmarshaler generates code to wrap the UnmarshalJSON method of a value.

func (*Generator) MapUnmarshaler

func (g *Generator) MapUnmarshaler(t types.Type, m *types.Map) (code meta.Code)

MapUnmarshaler generates the code block to unmarshal a map.

func (*Generator) MatchField

func (o *Generator) MatchField(name string) bool

func (*Generator) NodeJSONUnmarshaler

func (g *Generator) NodeJSONUnmarshaler(t types.Type) (code meta.Code)

NodeJSONUnmarshaler generates code to wrap the UnmarshalNodeJSON method of a value.

func (*Generator) OmiterType

func (g *Generator) OmiterType() (string, *types.Interface)

func (*Generator) PointerUnmarshaler

func (g *Generator) PointerUnmarshaler(T types.Type, t *types.Pointer) (code meta.Code)

PointerUnmarshaler generates the code block to unmarshal a pointer type.

func (*Generator) PrintTo

func (g *Generator) PrintTo(w io.Writer) error

PrintTo writes the generated file after checking and formatting.

func (*Generator) RawStringUnmarshaler

func (g *Generator) RawStringUnmarshaler(t types.Type) meta.Code

func (*Generator) Reset

func (g *Generator) Reset()

Reset resets the generator to start a new file.

func (*Generator) SliceUnmarshaler

func (g *Generator) SliceUnmarshaler(T types.Type, t *types.Slice) meta.Code

SliceUnmarshaler generates the code block to unmarshal a slice.

func (*Generator) StructAppender

func (g *Generator) StructAppender(fields meta.Fields) (c meta.Code)

func (*Generator) StructUnmarshaler

func (g *Generator) StructUnmarshaler(t *types.Struct) (code meta.Code)

StructUnmarshaler generates the code block to unmarshal a struct.

func (*Generator) TagKey

func (o *Generator) TagKey() string

func (*Generator) TextUnmarshaler

func (g *Generator) TextUnmarshaler(t types.Type) (code meta.Code)

TextUnmarshaler generates code to wrap the UnmarshalText method of a value.

func (*Generator) TypeAppender

func (g *Generator) TypeAppender(typ types.Type, params meta.Params) (c meta.Code)

func (*Generator) TypeOmiter

func (g *Generator) TypeOmiter(typ types.Type, block meta.Code) meta.Code

func (*Generator) TypeUnmarshaler

func (g *Generator) TypeUnmarshaler(t types.Type) (code meta.Code)

TypeUnmarshaler returns the code block for unmarshaling a type.

func (*Generator) UnmarshalMethod

func (o *Generator) UnmarshalMethod() string

func (*Generator) Unmarshaler

func (g *Generator) Unmarshaler(typeName string) (code meta.Code)

Unmarshaler generates an unmarshaler method for a type

func (*Generator) WriteAppender

func (g *Generator) WriteAppender(typName string) (err error)

func (*Generator) WriteUnmarshaler

func (g *Generator) WriteUnmarshaler(typeName string) (err error)

WriteUnmarshaler writes an unmarshaler method for a type in the generator's buffer.

type Option

type Option func(g *Generator)

Option is a generator option

func AppendMethod

func AppendMethod(methodName string) Option

AppendMethod sets the tag key to use when parsing struct fields

func ForceOmitEmpty

func ForceOmitEmpty(on bool) Option

ForceOmitEmpty forces omitempty on all fields regardless of json tag.

func Logger

func Logger(logger *log.Logger) Option

Logger sets a logger for the generator error messages.

func MatchFieldName

func MatchFieldName(rx *regexp.Regexp) Option

MatchFieldName sets a regex for matching struct field names

func OmitMethod

func OmitMethod(methodName string) Option

OmitMethod sets the tag key to use when parsing struct fields

func OnlyExported

func OnlyExported(on bool) Option

OnlyExported forces generator to ignore unexported fields

func OnlyTagged

func OnlyTagged(on bool) Option

OnlyTagged forces generator to ignore fields without a tag

func TagKey

func TagKey(key string) Option

TagKey sets the tag key to use when parsing struct fields

func TransformFieldCase

func TransformFieldCase(mode string) Option

TransformFieldCase sets a case transformation mode for field names when no tag based name is found.

func UnmarshalMethod

func UnmarshalMethod(methodName string) Option

UnmarshalMethod sets the tag key to use when parsing struct fields

Jump to

Keyboard shortcuts

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