visit

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ShortTagString    = "!!str"
	ShortTagNull      = "!!null"
	ShortTagInt       = "!!int"
	ShortTagFloat     = "!!float"
	ShortTagBool      = "!!bool"
	ShortTagMap       = "!!map"
	ShortTagSeq       = "!!seq"
	ShortTagTimestamp = "!!timestamp"
	ShortTagMerge     = "!!merge"
)

YAML short tag names.

Variables

View Source
var (
	ErrMissingRequired   = errors.New("missing required field")
	ErrMissingBuiltinVar = errors.New("missing built-in var")
)

Errors for the MapVisitor

View Source
var (
	ErrInvalidFieldType = errors.New("invalid field type")
	ErrKeyCollision     = errors.New("map key appears more than once")
	ErrKeyEmpty         = errors.New("map key must not be empty")
	ErrKeyNotString     = errors.New("map key must be string")
	ErrMissingDoc       = errors.New("empty document")
	ErrTooManyDocs      = errors.New("only 1 document is allowed")
)

Generic errors related to visiting YAML nodes.

View Source
var (
	ErrUnsupportedVarSubType = errors.New("unsupported variable substitution value")
)

Errors specific to performing variable substitution on nodes.

Functions

func Bool

func Bool(node *yaml.Node) (bool, error)

Bool will try to read this YAML node as a bool.

func DecodeFirstRootNode

func DecodeFirstRootNode(reader io.Reader) (*yaml.Node, error)

DecodeFirstRootNode returns the first YAML root node from the first parsed document. If there are multiple documents then the first document is returned together with an error.

func DecodeRootNodes

func DecodeRootNodes(reader io.Reader) ([]*yaml.Node, error)

DecodeRootNodes returns the list of YAML root nodes from all documents in the parsed input.

func Document

func Document(node *yaml.Node) (*yaml.Node, error)

Document will try to read the root node of this document YAML node.

func Float64

func Float64(node *yaml.Node) (float64, error)

Float64 will try to read this YAML node as a float64.

func Int

func Int(node *yaml.Node) (int, error)

Int will try to read this YAML node as an integer.

func Map

func Map(node *yaml.Node) (map[string]*yaml.Node, errutil.Slice)

Map will try to read this YAML node as a map of nodes with string keys.

func NewNodeWithValue

func NewNodeWithValue(node *yaml.Node, val any) (*yaml.Node, error)

NewNodeWithValue creates a new YAML node with the correct kind and tag depending on the reflected type.

func PrettyNodeTypeName

func PrettyNodeTypeName(node *yaml.Node) string

PrettyNodeTypeName will return a human-readable type name of a node.

func Sequence

func Sequence(node *yaml.Node) ([]*yaml.Node, error)

Sequence will try to read this YAML node as a sequence of nodes (array).

func String

func String(node *yaml.Node) (string, error)

String will try to read this YAML node as a string.

func Uint

func Uint(node *yaml.Node) (uint, error)

Uint will try to read this YAML node as an integer.

func VarSubNodeRec

func VarSubNodeRec(node *yaml.Node, source varsub.Source) (*yaml.Node, error)

VarSubNodeRec will perform variable substitution recursively on a YAML node tree.

func VarSubStringNode

func VarSubStringNode(str StringNode, source varsub.Source) (*yaml.Node, error)

VarSubStringNode will perform variable substitution on a string node and return a new node with the substituted value.

func VerifyKind

func VerifyKind(node *yaml.Node, wantStr string, wantKind yaml.Kind) error

VerifyKind checks if the given node has the wanted kind. The wantStr is only used as a pretty string of the wanted type in the potential error message.

func VerifyKindAndTag

func VerifyKindAndTag(node *yaml.Node, wantStr string, wantKind yaml.Kind, wantTag string) error

VerifyKindAndTag checks if the given node has the wanted kind and tag. The wantStr is only used as a pretty string of the wanted type in the potential error message.

func VerifyTag

func VerifyTag(node *yaml.Node, wantStr string, wantTag string) error

VerifyTag checks if the given node has the wanted tag. The wantStr is only used as a pretty string of the wanted type in the potential error message.

Types

type MapItem

type MapItem struct {
	Key   StringNode
	Value *yaml.Node
}

MapItem is a key-value pair item from a YAML map node.

func MapSlice

func MapSlice(node *yaml.Node) ([]MapItem, errutil.Slice)

MapSlice will try to read this YAML node as a map of nodes with string keys, returned as a slice of key-value pairs, keeping the original order of the nodes from the YAML file.

type MapVisitor

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

MapVisitor is a utility type that can visit nodes from a map of nodes by key, allowing simpler parsing of a map node with many different field types.

func NewMapVisitor

func NewMapVisitor(parent *yaml.Node, nodes map[string]*yaml.Node, source varsub.Source) MapVisitor

NewMapVisitor creates a new type that can visit nodes from a map of nodes by key, allowing simpler parsing of a map node with many different field types.

func (MapVisitor) AddErrorFor

func (p MapVisitor) AddErrorFor(key string, errSlice *errutil.Slice, err error)

AddErrorFor will add an error to the slice of errors with errutil.Pos and errutil.Scoped errors wrapped around it, using the node's position and key name respectively.

func (MapVisitor) HasNode

func (p MapVisitor) HasNode(key string) bool

HasNode returns a boolean if the node is defined. A YAML node with the value null will still return true.

func (MapVisitor) LookupBoolFromVarSub

func (p MapVisitor) LookupBoolFromVarSub(varLookup string, target *bool) error

LookupBoolFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned on type error. If the variable is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) LookupIntFromVarSub

func (p MapVisitor) LookupIntFromVarSub(varLookup string, target *int) error

LookupIntFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned on type error. If the variable is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) LookupNumberFromVarSub

func (p MapVisitor) LookupNumberFromVarSub(varLookup string, target *float64) error

LookupNumberFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned on type error. If the variable is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) LookupStringFromVarSub

func (p MapVisitor) LookupStringFromVarSub(varLookup string, target *string) error

LookupStringFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned on type error. If the variable is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) LookupUintFromVarSub

func (p MapVisitor) LookupUintFromVarSub(varLookup string, target *uint) error

LookupUintFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned on type error. If the variable is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) ParentPos

func (p MapVisitor) ParentPos() Pos

ParentPos returns the position of the map node's parent.

func (MapVisitor) ReadNodesPos

func (p MapVisitor) ReadNodesPos() map[string]Pos

ReadNodesPos returns a map of the positions of all the nodes that have been visited so far.

func (MapVisitor) RequireBoolFromVarSub

func (p MapVisitor) RequireBoolFromVarSub(varLookup string, target *bool) error

RequireBoolFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned if the looked up variable is not found or on type errors.

func (MapVisitor) RequireIntFromVarSub

func (p MapVisitor) RequireIntFromVarSub(varLookup string, target *int) error

RequireIntFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned if the looked up variable is not found or on type errors.

func (MapVisitor) RequireNumberFromVarSub

func (p MapVisitor) RequireNumberFromVarSub(varLookup string, target *float64) error

RequireNumberFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned if the looked up variable is not found or on type errors.

func (MapVisitor) RequireStringFromVarSub

func (p MapVisitor) RequireStringFromVarSub(varLookup string, target *string) error

RequireStringFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned if the looked up variable is not found or on type errors.

func (MapVisitor) RequireUintFromVarSub

func (p MapVisitor) RequireUintFromVarSub(varLookup string, target *uint) error

RequireUintFromVarSub looks up a value from the predefined varsub.Source and writes the value to the pointer. An error is returned if the looked up variable is not found or on type errors.

func (MapVisitor) ValidateRequiredSlice

func (p MapVisitor) ValidateRequiredSlice(key string) error

ValidateRequiredSlice will return an error if the node at the given key is not found or is an empty slice.

func (MapVisitor) ValidateRequiredString

func (p MapVisitor) ValidateRequiredString(key string) error

ValidateRequiredString will return an error if the node at the given key is not found or is an empty string.

func (MapVisitor) VisitBool

func (p MapVisitor) VisitBool(key string, target *bool) error

VisitBool reads a node by string key and writes the parsed bool value to the pointer. An error is returned on parse error. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitInt

func (p MapVisitor) VisitInt(key string, target *int) error

VisitInt reads a node by string key and writes the parsed int value to the pointer. An error is returned on parse error. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitNumber

func (p MapVisitor) VisitNumber(key string, target *float64) error

VisitNumber reads a node by string key and writes the parsed float64 value to the pointer. An error is returned on parse error. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitString

func (p MapVisitor) VisitString(key string, target *string) error

VisitString reads a node by string key and writes the string value to the pointer. An error is returned on type error. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitStringSlice

func (p MapVisitor) VisitStringSlice(key string, target *[]string) errutil.Slice

VisitStringSlice reads a node by string key and writes the string values to the pointer. A slice of error contains any type errors. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitStringStringMap

func (p MapVisitor) VisitStringStringMap(key string, target *map[string]string) errutil.Slice

VisitStringStringMap reads a node by string key and writes the string key-value pairs to the pointer. A slice of error contains any type errors. If the node is not present, then nil is returned and the pointer is untouched.

func (MapVisitor) VisitStringWithVarSub

func (p MapVisitor) VisitStringWithVarSub(nodeKey, varLookup string, target *string) error

VisitStringWithVarSub will try to find a variable from the predefined varsub.Source and use that as a default if the node at the given key is not present. An error is returned on type errors.

func (MapVisitor) VisitUint

func (p MapVisitor) VisitUint(key string, target *uint) error

VisitUint reads a node by string key and writes the parsed int value to the pointer. An error is returned on parse error. If the node is not present, then nil is returned and the pointer is untouched.

type Pos

type Pos struct {
	Line   int
	Column int
}

Pos represents a position. Used to declare where an object was defined in the .wharf-ci.yml file. The first line and column starts at 1. The zero value is used to represent an undefined position.

func NewPosFromNode

func NewPosFromNode(node *yaml.Node) Pos

NewPosFromNode creates a new Pos using a YAML node's Line and Column.

func (Pos) String

func (p Pos) String() string

String implements fmt.Stringer

type StringNode

type StringNode struct {
	Node  *yaml.Node
	Value string
}

StringNode is a node with known string type, with both the string value and original node available.

type VarSubNode

type VarSubNode struct {
	Node *yaml.Node
}

VarSubNode is a custom varsub variable type that envelops a YAML node. Mostly only used internally inside the wharfyml package.

func (VarSubNode) String

func (v VarSubNode) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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