derive

package
v0.0.0-...-ee86543 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package derive contains some utilities and helpers to build deriving macros.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCycleDetected reports that code cannot be derived due to infinite cycle.
	ErrCycleDetected = errors.New("cycle detected")
	// ErrInvalidType reports that type check failed.
	ErrInvalidType = errors.New("got invalid type")
)
View Source
var ErrExpected = errors.New("expected expression")

ErrExpected reports that expression has invalid type or kind.

Functions

func Callback

func Callback(m Macro) macro.HandlerFunc

Callback creates new macro handler from derive macro.

Types

type Array

type Array struct {
	Size int64
	Elem types.Type
}

Array is a Go slice or array type.

type ArrayDerive

type ArrayDerive interface {
	Array(d *Derive, field Field, arr Array) (*ast.BlockStmt, error)
}

ArrayDerive defines code generation for arrays and slices.

type Derive

type Derive struct {
	macro.Context
	Macro
	Interpolator Interpolator
	// contains filtered or unexported fields
}

Derive is a deriving state.

func NewDerive

func NewDerive(m Macro) *Derive

NewDerive creates new Derive.

func (*Derive) Derive

Derive derives code for given type declaration.

func (*Derive) Dispatch

Dispatch derives code for given field.

func (*Derive) IsCurrent

func (d *Derive) IsCurrent(typ types.Type) bool

IsCurrent checks that given type is current deriving type. Useful for resolving cycles.

func (*Derive) IsDelayed

func (d *Derive) IsDelayed(typ *types.TypeName) bool

IsDelayed checks that given typename is marked to derive code using current macro. Useful for resolving cycles.

func (*Derive) With

func (d *Derive) With(ctx macro.Context)

With uses given context.

type Field

type Field struct {
	Named    bool
	TypeName *types.TypeName
	Tag      Tag
	Selector ast.Expr
}

Field is a struct field representation.

type Interpolator

type Interpolator struct {
	// contains filtered or unexported fields
}

Interpolator interpolates and evaluates struct tags.

func NewInterpolator

func NewInterpolator(derive *Derive, values map[string]string) Interpolator

NewInterpolator creates new Interpolator.

func (Interpolator) Expr

func (i Interpolator) Expr(s string) (ast.Expr, error)

Expr interpolates given string and tries to parse it.

func (Interpolator) ExprExpectInfo

func (i Interpolator) ExprExpectInfo(s string, info types.BasicInfo) (ast.Expr, error)

ExprExpectInfo interpolates given string, tries to parse it and checks basic type into.

func (Interpolator) ExprExpectKind

func (i Interpolator) ExprExpectKind(s string, kind types.BasicKind) (ast.Expr, error)

ExprExpectKind interpolates given string, tries to parse it and checks basic type kind.

func (Interpolator) Interpolate

func (i Interpolator) Interpolate(s string) string

Interpolate interpolates given string.

type Macro

type Macro interface {
	Protocol() Protocol
	Name() string
	Target() *types.Interface
}

Macro is an abstraction for deriving macro.

func CreateMacro

func CreateMacro(name string, target *types.Interface, p Protocol) Macro

CreateMacro creates derive macro from given name, target interface and Protocol.

type Map

type Map struct {
	Key   types.Type
	Value types.Type
}

Map is a Go map type.

type MapDerive

type MapDerive interface {
	Map(d *Derive, field Field, m Map) (*ast.BlockStmt, error)
}

MapDerive defines code generation for maps.

type Pointer

type Pointer struct {
	Elem types.Type
}

Pointer is Go pointer type.

type PointerDerive

type PointerDerive interface {
	Pointer(d *Derive, field Field, p Pointer) (*ast.BlockStmt, error)
}

PointerDerive defines code generation for pointers.

type Protocol

type Protocol interface {
	CallFor(d *Derive, field Field, kind types.BasicKind) (*ast.BlockStmt, error)
	Impl(d *Derive, field Field) (*ast.BlockStmt, error)
	Callback(d *Derive, node *ast.TypeSpec) error
}

Protocol is an abstraction for deriving protocol. It defines

type Tag

type Tag string

Tag is a struct tag

func (Tag) Lookup

func (t Tag) Lookup(s string) (string, bool)

Lookup gets struct tag value by name.

Jump to

Keyboard shortcuts

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