v0.0.0-...-0a721d5 Latest Latest

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 16 Imported by: 2



Package derive is the code generator library used by all plugins.



This section is empty.


This section is empty.


func GetStructFields

func GetStructFields(s *types.Struct) []*types.Var

func ImportPaths

func ImportPaths(args []string) []string

ImportPaths returns the import paths to use for the given command line.

func IsComparable

func IsComparable(tt types.Type) bool

func IsError

func IsError(t types.Type) bool

IsError returns whether a type implements the Error interface.

func RenameBlankIdentifier

func RenameBlankIdentifier(sig *types.Signature) *types.Signature

RenameBlankIdentifier returns a signature where all blank parameter names are renamed.

func RenameBlankIdentifierWith

func RenameBlankIdentifierWith(sig *types.Signature, prefix string) *types.Signature

RenameBlankIdentifierWith returns a signature where all blank parameter names are renamed. The given prefix is used to rename.

func Zero

func Zero(typ types.Type) string

Zero returns the zero value as a string, for a given type.


type Dependency

type Dependency interface {
	GetFuncName(typs ...types.Type) string

Dependency is used by other plugins to generate more functions.

type Field

type Field struct {
	Type types.Type
	// contains filtered or unexported fields

Field describes a struct field.

func (*Field) DebugName

func (f *Field) DebugName() string

DebugName simply returns the field name, without the receiver or any unsafe magic.

func (*Field) Name

func (f *Field) Name(recv string, unsafePkg Import) string

Name returns the field name, given the receiver and the unsafe import, if needed.

func (*Field) Private

func (f *Field) Private() bool

Private whether the field is private

type Generator

type Generator interface {
	Add(name string, typs []types.Type) (string, error)
	Generate(typs []types.Type) error

Generator generates code for input types.

type Import

type Import func() string

Import is a closure that returns the import alias and only adds the import to the generated code if the function is actually called.

type Named

type Named struct {
	Fields  []*Field
	Reflect bool

Named describes a named struct with a list of fields.

func Fields

func Fields(typesMap TypesMap, typ *types.Struct, external bool) *Named

Fields returns a new Named object containing a list of Fields for a given input struct.

type Plugin

type Plugin interface {
	GetPrefix() string
	Name() string
	New(typesMap TypesMap, p Printer, deps map[string]Dependency) Generator

Plugin is used to create a Generator.

func NewPlugin

func NewPlugin(name, prefix string, newFunc func(typesMap TypesMap, p Printer, deps map[string]Dependency) Generator) Plugin

NewPlugin is used by a plugin library to create a plugin, that can be added to the Plugins list. For example:

func NewPlugin() derive.Plugin {
  return derive.NewPlugin("all", "deriveAll", New)

type Plugins

type Plugins interface {
	Load(paths []string) (Program, error)

Plugins is a collection of plugins, that given a list of paths becomes a Program.

func NewPlugins

func NewPlugins(ps []Plugin, autoname bool, dedup bool) Plugins

NewPlugins returns a collection of plugins that is ready to generate code.

type Printer

type Printer interface {
	P(format string, a ...interface{})
	WriteTo(w io.Writer) (int64, error)

	NewImport(name, path string) Import
	HasContent() bool

Printer is used to print the generated code to a file.

type Program

type Program interface {
	Generate() error

Program is ready to generate code for a whole program.

type TypesMap

type TypesMap interface {
	SetFuncName(name string, typs ...types.Type) (newName string, err error)
	GetFuncName(typs ...types.Type) string
	Generating(typs ...types.Type)
	ToGenerate() [][]types.Type
	Prefix() string
	TypeString(typ types.Type) string
	FieldStrings(fields []*types.Var) ([]string, error)
	IsExternal(typ *types.Named) bool
	Done() bool

TypesMap is a map of input types to function names. It also keeps track of which functions have been generated.

Jump to

Keyboard shortcuts

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