Documentation ¶
Overview ¶
The typewriter package provides a framework for type-driven code generation. It implements the core functionality of gen.
This package is primarily of interest to those who wish to extend gen with third-party functionality.
More docs are available at https://clipperhouse.github.io/gen/typewriters.
Index ¶
- Variables
- func Register(tw Interface) error
- type App
- type Config
- type Constraint
- type ImportSpec
- type ImportSpecSet
- func (set ImportSpecSet) Add(i ImportSpec) bool
- func (set ImportSpecSet) Cardinality() int
- func (set *ImportSpecSet) Clear()
- func (set ImportSpecSet) Clone() ImportSpecSet
- func (set ImportSpecSet) Contains(i ImportSpec) bool
- func (set ImportSpecSet) ContainsAll(i ...ImportSpec) bool
- func (set ImportSpecSet) Difference(other ImportSpecSet) ImportSpecSet
- func (set ImportSpecSet) Equal(other ImportSpecSet) bool
- func (set ImportSpecSet) Intersect(other ImportSpecSet) ImportSpecSet
- func (set ImportSpecSet) IsSubset(other ImportSpecSet) bool
- func (set ImportSpecSet) IsSuperset(other ImportSpecSet) bool
- func (set ImportSpecSet) Iter() <-chan ImportSpec
- func (set ImportSpecSet) Remove(i ImportSpec)
- func (set ImportSpecSet) SymmetricDifference(other ImportSpecSet) ImportSpecSet
- func (set ImportSpecSet) ToSlice() []ImportSpec
- func (set ImportSpecSet) Union(other ImportSpecSet) ImportSpecSet
- type Interface
- type Package
- type Pointer
- type Tag
- type TagSlice
- type TagValue
- type Template
- type TemplateSlice
- type Type
- type TypeCheckError
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = &Config{}
Functions ¶
func Register ¶
Register allows template packages to make themselves known to a 'parent' package, usually in the init() func. Comparable to the approach taken by stdlib's image package for registration of image types (eg image/png). Your program will do something like:
import ( "github.com/clipperhouse/typewriter" _ "github.com/clipperhouse/slice" )
Types ¶
type App ¶
type App struct { // All typewriter.Package found in the current directory. Packages []*Package // All typewriter.Interface's registered on init. TypeWriters []Interface Directive string }
App is the high-level construct for package-level code generation. Typical usage is along the lines of:
app, err := typewriter.NewApp() err := app.WriteAll()
+test foo:"Bar" baz:"qux[struct{}],thing"
func NewApp ¶
NewApp parses the current directory, enumerating registered TypeWriters and collecting Types and their related information.
func NewAppFiltered ¶
NewAppFiltered parses the current directory, collecting Types and their related information. Pass a filter to limit which files are operated on.
type Constraint ¶
type Constraint struct { // A numeric type is one that supports arithmetic operations. Numeric bool // A comparable type is one that supports the == operator. Map keys must be comparable, for example. Comparable bool // An ordered type is one where greater-than and less-than are supported Ordered bool }
Constraint describes type requirements.
func (Constraint) TryType ¶
func (c Constraint) TryType(t Type) error
type ImportSpec ¶
type ImportSpec struct {
Name, Path string
}
ImportSpec describes the name and path of an import. The name is often omitted.
+gen set
type ImportSpecSet ¶
type ImportSpecSet map[ImportSpec]struct{}
The primary type that represents a set
func NewImportSpecSet ¶
func NewImportSpecSet(a ...ImportSpec) ImportSpecSet
Creates and returns a reference to an empty set.
func (ImportSpecSet) Add ¶
func (set ImportSpecSet) Add(i ImportSpec) bool
Adds an item to the current set if it doesn't already exist in the set.
func (ImportSpecSet) Cardinality ¶
func (set ImportSpecSet) Cardinality() int
Cardinality returns how many items are currently in the set.
func (*ImportSpecSet) Clear ¶
func (set *ImportSpecSet) Clear()
Clears the entire set to be the empty set.
func (ImportSpecSet) Clone ¶
func (set ImportSpecSet) Clone() ImportSpecSet
Returns a clone of the set. Does NOT clone the underlying elements.
func (ImportSpecSet) Contains ¶
func (set ImportSpecSet) Contains(i ImportSpec) bool
Determines if a given item is already in the set.
func (ImportSpecSet) ContainsAll ¶
func (set ImportSpecSet) ContainsAll(i ...ImportSpec) bool
Determines if the given items are all in the set
func (ImportSpecSet) Difference ¶
func (set ImportSpecSet) Difference(other ImportSpecSet) ImportSpecSet
Returns a new set with items in the current set but not in the other set
func (ImportSpecSet) Equal ¶
func (set ImportSpecSet) Equal(other ImportSpecSet) bool
Equal determines if two sets are equal to each other. If they both are the same size and have the same items they are considered equal. Order of items is not relevent for sets to be equal.
func (ImportSpecSet) Intersect ¶
func (set ImportSpecSet) Intersect(other ImportSpecSet) ImportSpecSet
Returns a new set with items that exist only in both sets.
func (ImportSpecSet) IsSubset ¶
func (set ImportSpecSet) IsSubset(other ImportSpecSet) bool
Determines if every item in the other set is in this set.
func (ImportSpecSet) IsSuperset ¶
func (set ImportSpecSet) IsSuperset(other ImportSpecSet) bool
Determines if every item of this set is in the other set.
func (ImportSpecSet) Iter ¶
func (set ImportSpecSet) Iter() <-chan ImportSpec
Iter() returns a channel of type ImportSpec that you can range over.
func (ImportSpecSet) Remove ¶
func (set ImportSpecSet) Remove(i ImportSpec)
Allows the removal of a single item in the set.
func (ImportSpecSet) SymmetricDifference ¶
func (set ImportSpecSet) SymmetricDifference(other ImportSpecSet) ImportSpecSet
Returns a new set with items in the current set or the other set but not in both.
func (ImportSpecSet) ToSlice ¶
func (set ImportSpecSet) ToSlice() []ImportSpec
ToSlice returns the elements of the current set as a slice
func (ImportSpecSet) Union ¶
func (set ImportSpecSet) Union(other ImportSpecSet) ImportSpecSet
Returns a new set with all items in both sets.
type Interface ¶
type Interface interface { Name() string // Imports is a slice of imports required for the type; each will be written into the imports declaration. Imports(t Type) []ImportSpec // Write writes to the body of the generated code, following package declaration and imports. Write(w io.Writer, t Type) error }
Interface is the interface to be implemented for code generation via gen
type Package ¶
func NewPackage ¶
type Pointer ¶
type Pointer bool
Pointer exists as a type to allow simple use as bool or as String, which returns *
type TagSlice ¶
type TagSlice []Tag
TagSlice is a slice of type Tag. Use it where you would use []Tag.
type Template ¶
type Template struct {
Name, Text string
FuncMap map[string]interface{}
TypeConstraint Constraint
// Indicates both the number of required type parameters, and the constraints of each (if any)
TypeParameterConstraints []Constraint
}
Template includes the text of a template as well as requirements for the types to which it can be applied. +gen * slice:"Where"
type TemplateSlice ¶
type TemplateSlice []*Template
TemplateSlice is a slice of type *Template. Use it where you would use []*Template.
func (TemplateSlice) ByTag ¶
ByTag attempts to locate a template which meets type constraints, and parses it.
func (TemplateSlice) ByTagValue ¶
ByTagValue attempts to locate a template which meets type constraints, and parses it.
func (TemplateSlice) Funcs ¶
func (ts TemplateSlice) Funcs(FuncMap map[string]interface{})
Funcs assigns non standard functions used in the template
func (TemplateSlice) Where ¶
func (rcv TemplateSlice) Where(fn func(*Template) bool) (result TemplateSlice)
Where returns a new TemplateSlice whose elements return true for func. See: http://clipperhouse.github.io/gen/#Where
type Type ¶
type Type struct { Pointer Pointer Name string Tags TagSlice types.Type // contains filtered or unexported fields }
type TypeCheckError ¶
type TypeCheckError struct {
// contains filtered or unexported fields
}
func (*TypeCheckError) Error ¶
func (t *TypeCheckError) Error() string