poet

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2016 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package poet provides an API for generating Go constructs in a type-safe and reusable way.

Index

Constants

View Source
const UnqualifiedPrefix = "_unqualified"

UnqualifiedPrefix The prefix for type aliases that will be interpreted as unqualified

Variables

View Source
var (
	// String A TypeReference for string
	String = TypeReferenceFromInstance("")
	// Bool A TypeReference for bool
	Bool = TypeReferenceFromInstance(false)
	// Int A TypeReference for int
	Int = TypeReferenceFromInstance(0)
	// Int8 A TypeReference for int8
	Int8 = TypeReferenceFromInstance(int8(0))
	// Int16 A TypeReference for int16
	Int16 = TypeReferenceFromInstance(int16(0))
	// Int32 A TypeReference for int32
	Int32 = TypeReferenceFromInstance(int32(0))
	// Int64 A TypeReference for int64
	Int64 = TypeReferenceFromInstance(int64(0))
	// Uint A TypeReference for uint
	Uint = TypeReferenceFromInstance(uint(0))
	// Uint8 A TypeReference for uint8
	Uint8 = TypeReferenceFromInstance(uint8(0))
	// Uint16 A TypeReference for uint16
	Uint16 = TypeReferenceFromInstance(uint16(0))
	// Uint32 A TypeReference for uint32
	Uint32 = TypeReferenceFromInstance(uint32(0))
	// Uint64 A TypeReference for uint64
	Uint64 = TypeReferenceFromInstance(uint64(0))
	// Uintptr A TypeReference for uintptr
	Uintptr = TypeReferenceFromInstance(uintptr(0))
	// Float32 A TypeReference for float32
	Float32 = TypeReferenceFromInstance(float32(0))
	// Float64 A TypeReference for float64
	Float64 = TypeReferenceFromInstance(float64(0))
	// Complex64 A TypeReference for complex64
	Complex64 = TypeReferenceFromInstance(complex64(0))
	// Complex128 A TypeReference for complex128
	Complex128 = TypeReferenceFromInstance(complex128(0))
	// Byte A TypeReference for byte
	Byte = TypeReferenceFromInstanceWithCustomName(uint8(0), "byte")
	// Rune A TypeReference for rune
	Rune = TypeReferenceFromInstanceWithCustomName(int32(0), "rune")
)

Functions

This section is empty.

Types

type CodeBlock

type CodeBlock interface {
	String() string
	GetImports() []Import
}

CodeBlock represent a block of code that can be included in a File

type FileSpec

type FileSpec struct {
	Comment                string
	Package                string      // Package that the file belongs to
	InitializationPackages []Import    // InitializationPackages include any imports that need to be included for their side effects
	Init                   *FuncSpec   // Init is a single function to be outputted before all CodeBlocks
	CodeBlocks             []CodeBlock // CodeBlocks are appeneded and when outputted separated by a newline
}

FileSpec represents a .go source file

func NewFileSpec

func NewFileSpec(pkg string) *FileSpec

NewFileSpec constructs a new FileSpec with the given package name

func (*FileSpec) CodeBlock

func (f *FileSpec) CodeBlock(blk CodeBlock) *FileSpec

CodeBlock adds a code block to the file

func (*FileSpec) FileComment

func (f *FileSpec) FileComment(comment string) *FileSpec

FileComment sets the file's comment to the given input string.

func (*FileSpec) GlobalConstant

func (f *FileSpec) GlobalConstant(name string, typ TypeReference, format string, args ...interface{}) *FileSpec

GlobalConstant adds a global constant to the file with the given name, type reference, format string for the value of the constant, and arguments for the format string.

func (*FileSpec) GlobalVariable

func (f *FileSpec) GlobalVariable(name string, typ TypeReference, format string, args ...interface{}) *FileSpec

GlobalVariable adds a global variable to the file with the given name, type reference, format string for the value of the variable, and arguments for the format string.

func (*FileSpec) InitFunction

func (f *FileSpec) InitFunction(blk *FuncSpec) *FileSpec

InitFunction assign an init function

func (*FileSpec) InitializationPackage

func (f *FileSpec) InitializationPackage(imp Import) *FileSpec

InitializationPackage appends an initialization package for its side effects

func (*FileSpec) String

func (f *FileSpec) String() string

String produces the final go file string

func (*FileSpec) VariableGrouping

func (f *FileSpec) VariableGrouping() *VariableGrouping

VariableGrouping adds a variable grouping to the file, which can have variables appended to it that will be formatted in groups of variables and constants.

type FuncSpec

type FuncSpec struct {
	Name             string
	Comment          string
	Parameters       []IdentifierParameter
	ResultParameters []IdentifierParameter
	Statements       []statement
}

FuncSpec represents information needed to write a function

func NewFuncSpec

func NewFuncSpec(name string) *FuncSpec

NewFuncSpec returns a FuncSpec with the given name

func (*FuncSpec) BlockEnd

func (f *FuncSpec) BlockEnd() *FuncSpec

BlockEnd is a convenient method to append a statement that marks the end of a block of code.

func (*FuncSpec) BlockStart

func (f *FuncSpec) BlockStart(format string, args ...interface{}) *FuncSpec

BlockStart is a convenient method to append a statement that marks the start of a block of code.

func (*FuncSpec) FunctionComment

func (f *FuncSpec) FunctionComment(comment string) *FuncSpec

FunctionComment adds a comment to the function

func (*FuncSpec) GetImports

func (f *FuncSpec) GetImports() []Import

GetImports returns a slice of imports that this function needs, including parameters, result parameters, and statements within the function

func (*FuncSpec) Parameter

func (f *FuncSpec) Parameter(name string, spec TypeReference) *FuncSpec

Parameter is a convenient method to append a parameter to the function

func (*FuncSpec) ResultParameter

func (f *FuncSpec) ResultParameter(name string, spec TypeReference) *FuncSpec

ResultParameter is a convenient method to append a result parameter to the function

func (*FuncSpec) Signature

func (f *FuncSpec) Signature() (_ string, arguments []interface{})

Signature returns a format string and slice of arguments for the function's signature, not including the starting "func" or opening curly brace

func (*FuncSpec) Statement

func (f *FuncSpec) Statement(format string, args ...interface{}) *FuncSpec

Statement is a convenient method to append a statement to the function

func (*FuncSpec) String

func (f *FuncSpec) String() string

String returns a string representation of the function

func (*FuncSpec) VariadicParameter

func (f *FuncSpec) VariadicParameter(name string, spec TypeReference) *FuncSpec

VariadicParameter is a convenient method to append a parameter to the function

type Identifier

type Identifier struct {
	Name string        // Name of the instance of a variable (e.g. in "var a int", a)
	Type TypeReference // Type of the variable
}

Identifier represent an instance of a variable

type IdentifierField

type IdentifierField struct {
	Identifier
	Tag string // Tag is a
}

IdentifierField represent a field in a struct

type IdentifierParameter

type IdentifierParameter struct {
	Identifier
	Variadic bool // Variadic specifies whether the parameter is a variadic
}

IdentifierParameter represent a parameter in a function/method

type Import

type Import interface {
	GetPackage() string
	GetAlias() string
}

Import represent an indivdual import

type ImportSpec

type ImportSpec struct {
	Package   string
	Alias     string
	Qualified bool
}

ImportSpec implements Import to represent an imported go package

func (*ImportSpec) GetAlias

func (i *ImportSpec) GetAlias() string

GetAlias returns the alias associated with the package

func (*ImportSpec) GetPackage

func (i *ImportSpec) GetPackage() string

GetPackage returns the package

type InterfaceSpec

type InterfaceSpec struct {
	Name               string
	Comment            string
	EmbeddedInterfaces []TypeReference
	Methods            []*FuncSpec
}

InterfaceSpec represents an interface

func NewInterfaceSpec

func NewInterfaceSpec(name string) *InterfaceSpec

NewInterfaceSpec constructs a new interface with the given name

func (*InterfaceSpec) EmbedInterface

func (i *InterfaceSpec) EmbedInterface(interfaceType TypeReference) *InterfaceSpec

EmbedInterface specifies an interface to embed in the interface

func (*InterfaceSpec) GetImports

func (i *InterfaceSpec) GetImports() []Import

GetImports returns Import's used by the interface

func (*InterfaceSpec) Method

func (i *InterfaceSpec) Method(spec *FuncSpec) *InterfaceSpec

Method adds a new method to the interface

func (*InterfaceSpec) String

func (i *InterfaceSpec) String() string

String outputs the interface declaration

type MethodSpec

type MethodSpec struct {
	FuncSpec
	ReceiverName string
	Receiver     TypeReference
}

MethodSpec represents a method, with a receiver name and type.

func NewMethodSpec

func NewMethodSpec(name, receiverName string, receiver TypeReference) *MethodSpec

NewMethodSpec creates a new method with the given method name, receiverName, and receiver type.

func (*MethodSpec) String

func (m *MethodSpec) String() string

type StructSpec

type StructSpec struct {
	Name    string
	Comment string
	Fields  []IdentifierField
	Methods []*MethodSpec
}

StructSpec represents a struct

func NewStructSpec

func NewStructSpec(name string) *StructSpec

NewStructSpec creates a new struct with the given type name

func (*StructSpec) AttachMethod

func (s *StructSpec) AttachMethod(m *MethodSpec) *StructSpec

AttachMethod attaches a MethodSpec to this struct, such that a call to String() on this struct will output attached methods next to this struct. This is useful for having a method placed next to the struct it belongs to.

func (*StructSpec) Field

func (s *StructSpec) Field(name string, typeRef TypeReference) *StructSpec

Field adds a field to this struct.

func (*StructSpec) FieldWithTag

func (s *StructSpec) FieldWithTag(name string, typeRef TypeReference, tag string) *StructSpec

FieldWithTag adds a field to this struct with a tag on the field.

func (*StructSpec) GetImports

func (s *StructSpec) GetImports() []Import

GetImports returns a slice of imports needed by this struct

func (*StructSpec) GetName

func (s *StructSpec) GetName() string

GetName returns the name of this struct's type

func (*StructSpec) Method

func (s *StructSpec) Method(name, receiverName string, receiverIsPtr bool) *MethodSpec

Method creates a new method spec with this struct as the receiver.

func (*StructSpec) MethodFromFunction

func (s *StructSpec) MethodFromFunction(receiverName string, receiverIsPtr bool, funcSpec *FuncSpec) *MethodSpec

MethodFromFunction creates a method from a FuncSpec and adds this struct as the receiver.

func (*StructSpec) String

func (s *StructSpec) String() string

func (*StructSpec) StructComment

func (s *StructSpec) StructComment(comment string) *StructSpec

StructComment adds a comment to this struct.

type TypeReference

type TypeReference interface {
	GetImports() []Import
	GetName() string
}

TypeReference represent a specific reference (either an interface, struct or a global)

func TypeReferenceFromInstance

func TypeReferenceFromInstance(t interface{}) TypeReference

TypeReferenceFromInstance creates a TypeReference from an instance of a variable

func TypeReferenceFromInstanceWithAlias

func TypeReferenceFromInstanceWithAlias(t interface{}, alias string) TypeReference

TypeReferenceFromInstanceWithAlias creates a TypeReference from an instance of a variable with the given package alias

func TypeReferenceFromInstanceWithCustomName

func TypeReferenceFromInstanceWithCustomName(t interface{}, name string) TypeReference

TypeReferenceFromInstanceWithCustomName creates a TypeReference from an instance of a variable with the given custom name, for use of a type alias's name rather than the underlying reflect type.

type Variable

type Variable struct {
	Identifier
	Constant bool
	Format   string
	Args     []interface{}
}

Variable represents a variable, with name, type, and value.

func (*Variable) GetDeclaration

func (v *Variable) GetDeclaration() string

GetDeclaration returns the name and type of this variable, for example: 'foo string'.

func (*Variable) GetImports

func (v *Variable) GetImports() []Import

GetImports returns a slice of imports that this variable uses.

func (*Variable) String

func (v *Variable) String() string

type VariableGrouping

type VariableGrouping struct {
	Variables []*Variable
}

VariableGrouping represents a collection of variables and/or constants that will be separated into groups on output.

func (*VariableGrouping) Constant

func (g *VariableGrouping) Constant(name string, typ TypeReference, format string, args ...interface{}) *VariableGrouping

Constant adds a new constant to this variable grouping.

func (*VariableGrouping) GetImports

func (g *VariableGrouping) GetImports() []Import

GetImports returns a slice of imports that this variable grouping uses.

func (*VariableGrouping) String

func (g *VariableGrouping) String() string

func (*VariableGrouping) Variable

func (g *VariableGrouping) Variable(name string, typ TypeReference, format string, args ...interface{}) *VariableGrouping

Variable adds a new variable to this variable grouping.

Jump to

Keyboard shortcuts

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