gen

package
v3.56.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(modules []*il.Graph, lang Generator) error

Generate generates source for a list of modules using the given language-specific generator.

func SortedKeys

func SortedKeys(m interface{}) []string

SortedKeys returns a sorted list of keys for the given map. The map's key type must be of kind string.

Types

type Emitter

type Emitter struct {
	// The current indent level as a string.
	Indent string
	// contains filtered or unexported fields
}

Emitter is a convenience type that implements a number of common utilities used to emit source code. It implements the io.Writer interface.

func NewEmitter

func NewEmitter(w io.Writer, g HILGenerator) *Emitter

NewEmitter creates a new emitter targeting the given io.Writer that will use the given HILGenerator when generating code.

func (*Emitter) Fgen

func (e *Emitter) Fgen(w io.Writer, vs ...interface{})

Fgen generates code for a list of strings and expression trees. The former are written directly to the destination; the latter are recursively generated using the appropriate gen* functions.

func (*Emitter) Fgenf

func (e *Emitter) Fgenf(w io.Writer, format string, args ...interface{})

Fgenf generates code using a format string and its arguments. Any arguments that are BoundNode values are wrapped in a FormatFunc that calls the appropriate recursive generation function. This allows for the composition of standard format strings with expression/property code gen (e.e. `e.genf(w, ".apply(__arg0 => %v)", then)`, where `then` is an expression tree).

func (*Emitter) Gen

func (e *Emitter) Gen(vs ...interface{})

Gen is shorthand for Fgen(e, vs...).

func (*Emitter) Genf

func (e *Emitter) Genf(format string, args ...interface{})

Genf is shorthand for Fgenf(e, format, args...)

func (*Emitter) Indented

func (e *Emitter) Indented(f func())

indented bumps the current indentation level, invokes the given function, and then resets the indentation level to its prior value.

func (*Emitter) Print

func (e *Emitter) Print(a ...interface{})

Print prints one or more values to the generator's output stream.

func (*Emitter) Printf

func (e *Emitter) Printf(format string, a ...interface{})

Printf prints a formatted message to the generator's output stream.

func (*Emitter) Println

func (e *Emitter) Println(a ...interface{})

Println prints one or more values to the generator's output stream, followed by a newline.

func (*Emitter) Write

func (e *Emitter) Write(b []byte) (int, error)

Write writes the given bytes to the emitter's destination.

type FormatFunc

type FormatFunc func(f fmt.State, c rune)

FormatFunc is a function type that implements the fmt.Formatter interface. This can be used to conveniently implement this interface for types defined in other packages.

func (FormatFunc) Format

func (p FormatFunc) Format(f fmt.State, c rune)

Format invokes the FormatFunc's underlying function.

type Generator

type Generator interface {
	// GeneratePreamble generates any preamble required by the language. The complete list of modules that will be
	// generated is passed as an input.
	GeneratePreamble(gs []*il.Graph) error
	// BeginModule does any work specific to the generation of a new module definition.
	BeginModule(g *il.Graph) error
	// EndModule does any work specific to the end of code generation for a module definition.
	EndModule(g *il.Graph) error
	// GenerateProvider generates a single provider block in the context of the current module definition.
	GenerateProvider(p *il.ProviderNode) error
	// GenerateVariables generates variable definitions in the context of the current module definition.
	GenerateVariables(vs []*il.VariableNode) error
	// GenerateModule generates a single module instantiation in the context of the current module definition.
	GenerateModule(m *il.ModuleNode) error
	// GenerateLocal generates a single local value definition in the context of the current module definition.
	GenerateLocal(l *il.LocalNode) error
	// GenerateResource generates a single resource instantiation in the context of the current module definition.
	GenerateResource(r *il.ResourceNode) error
	// GenerateOutputs generates the list of outputs in the context of the current module definition.
	GenerateOutputs(os []*il.OutputNode) error
}

Generator defines the interface that a language-specific code generator must expose in order to generate code for a set of Terraform modules.

type HILGenerator

type HILGenerator interface {
	// GenArithmetic generates code for the indicated arithmetic node to the given writer.
	GenArithmetic(w io.Writer, v *il.BoundArithmetic)
	// GenCall generates code for the indicated call node to the given writer.
	GenCall(w io.Writer, v *il.BoundCall)
	// GenConditional generates code for the indicated conditional node to the given writer.
	GenConditional(w io.Writer, v *il.BoundConditional)
	// GenError generates code for the indicated error node to the given writer.
	GenError(w io.Writer, v *il.BoundError)
	// GenIndex generates code for the indicated index node to the given writer.
	GenIndex(w io.Writer, v *il.BoundIndex)
	// GenListProperty generates code for the indicated list property to the given writer.
	GenListProperty(w io.Writer, v *il.BoundListProperty)
	// GenLiteral generates code for the indicated literal node to the given writer.
	GenLiteral(w io.Writer, v *il.BoundLiteral)
	// GenMapProperty generates code for the indicated map property to the given writer.
	GenMapProperty(w io.Writer, v *il.BoundMapProperty)
	// GenOutput generates code for the indicated output node to the given writer.
	GenOutput(w io.Writer, v *il.BoundOutput)
	// GenPropertyValue generates code for the indicated property value node to the given writer.
	GenPropertyValue(w io.Writer, v *il.BoundPropertyValue)
	// GenVariableAccess generates code for the indicated variable access node to the given writer.
	GenVariableAccess(w io.Writer, v *il.BoundVariableAccess)
}

HILGenerator is an interface that

Jump to

Keyboard shortcuts

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