builder

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package builder exposes an API to create a River configuration file by constructing a set of tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Name  []string
	Label string
	// contains filtered or unexported fields
}

A Block encapsulates a body within a named and labeled River block. Blocks must be created by calling NewBlock, but its public struct fields may be safely modified by callers.

func NewBlock

func NewBlock(name []string, label string) *Block

NewBlock returns a new Block with the given name and label. The name/label can be updated later by modifying the Block's public fields.

func (*Block) Body

func (b *Block) Body() *Body

Body returns the Body contained within the Block.

func (*Block) Tokens

func (b *Block) Tokens() []Token

Tokens returns the File as a set of Tokens.

type Body

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

Body is a list of block and attribute statements. A Body cannot be manually created, but is retrieved from a File or Block.

func (*Body) AppendBlock

func (b *Body) AppendBlock(block *Block)

AppendBlock adds a new block inside of the Body.

func (*Body) AppendFrom

func (b *Body) AppendFrom(goValue interface{})

AppendFrom sets attributes and appends blocks defined by goValue into the Body. If any value reachable from goValue implements Tokenizer, the printed tokens will instead be retrieved by calling the RiverTokenize method.

Optional attributes and blocks set to default values are trimmed. If goValue implements Defaulter, default values are retrieved by calling SetToDefault against a copy. Otherwise, default values are the zero value of the respective Go types.

goValue must be a struct or a pointer to a struct that contains River struct tags.

func (*Body) AppendTokens

func (b *Body) AppendTokens(tokens []Token)

AppendTokens appends raw tokens to the Body.

func (*Body) Nodes

func (b *Body) Nodes() []tokenNode

func (*Body) SetAttributeTokens

func (b *Body) SetAttributeTokens(name string, tokens []Token)

SetAttributeTokens sets an attribute to the Body whose value is a set of raw tokens. If the attribute was previously set, its value tokens are updated.

Attributes will be written out in the order they were initially created.

func (*Body) SetAttributeValue

func (b *Body) SetAttributeValue(name string, goValue interface{})

SetAttributeValue sets an attribute in the Body whose value is converted from a Go value to a River value. The Go value is encoded using the normal Go to River encoding rules. If any value reachable from goValue implements Tokenizer, the printed tokens will instead be retrieved by calling the RiverTokenize method.

If the attribute was previously set, its value tokens are updated.

Attributes will be written out in the order they were initially crated.

func (*Body) SetValueOverrideHook

func (b *Body) SetValueOverrideHook(valueOverrideHook ValueOverrideHook)

SetValueOverrideHook sets a hook to override the value that will be token encoded. The hook can mutate the value to be encoded or should return it unmodified. This hook can be skipped by leaving it nil or setting it to nil.

func (*Body) Tokens

func (b *Body) Tokens() []Token

Tokens returns the File as a set of Tokens.

type Expr

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

An Expr represents a single River expression.

func NewExpr

func NewExpr() *Expr

NewExpr creates a new Expr.

func (*Expr) Bytes

func (e *Expr) Bytes() []byte

Bytes renders the File to a formatted byte slice.

func (*Expr) SetValue

func (e *Expr) SetValue(goValue interface{})

SetValue sets the Expr to a River value converted from a Go value. The Go value is encoded using the normal Go to River encoding rules. If any value reachable from goValue implements Tokenizer, the printed tokens will instead be retrieved by calling the RiverTokenize method.

func (*Expr) Tokens

func (e *Expr) Tokens() []Token

Tokens returns the Expr as a set of Tokens.

func (*Expr) WriteTo

func (e *Expr) WriteTo(w io.Writer) (int64, error)

WriteTo renders and formats the File, writing the contents to w.

type File

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

A File represents a River configuration file.

func NewFile

func NewFile() *File

NewFile creates a new File.

func (*File) Body

func (f *File) Body() *Body

Body returns the Body contents of the file.

func (*File) Bytes

func (f *File) Bytes() []byte

Bytes renders the File to a formatted byte slice.

func (*File) Tokens

func (f *File) Tokens() []Token

Tokens returns the File as a set of Tokens.

func (*File) WriteTo

func (f *File) WriteTo(w io.Writer) (int64, error)

WriteTo renders and formats the File, writing the contents to w.

type Token

type Token struct {
	Tok token.Token
	Lit string
}

A Token is a wrapper around token.Token which contains the token type alongside its literal. Use LiteralTok as the Tok field to write literal characters such as whitespace.

type Tokenizer

type Tokenizer interface {
	// RiverTokenize returns the raw set of River tokens which are used when
	// printing out the value with river/token/builder.
	RiverTokenize() []Token
}

Tokenizer is any value which can return a raw set of tokens.

type ValueOverrideHook

type ValueOverrideHook = func(val interface{}) interface{}

Jump to

Keyboard shortcuts

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