varsub

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

This section is empty.

Variables

View Source
var (
	// ErrRecursiveLoop means a variable substitution has some self-referring
	// variables, either directly or indirectly.
	ErrRecursiveLoop = errors.New("recursive variable loop")
)

Functions

func NewCopier

func NewCopier(source Source) filecopy.Copier

NewCopier creates a copier that applies variable substitution on each line.

func Substitute

func Substitute(value string, source Source) (any, error)

Substitute will replace all variables in the string using the variable substution source. Variables are looked up recursively.

Types

type Source

type Source interface {
	// Lookup tries to look up a value based on name and returns that value as
	// well as true on success, or false if the variable was not found.
	Lookup(name string) (Var, bool)

	// ListVars will return a slice of all variables that this varsub Source
	// provides.
	ListVars() []Var
}

Source is a variable substitution source.

func NewOSEnvSource

func NewOSEnvSource(prefix string) Source

NewOSEnvSource creates a new Source that uses your OS environment variables with a prefix as variables.

To use all environment variables, you can specify an empty string as prefix.

type SourceMap

type SourceMap map[string]Val

SourceMap is a variable substitution source based on a map where it uses the underlying map as the variable source.

func (SourceMap) ListVars

func (s SourceMap) ListVars() []Var

ListVars will return a slice of all variables that this varsub Source provides.

func (SourceMap) Lookup

func (s SourceMap) Lookup(name string) (Var, bool)

Lookup tries to look up a value based on name and returns that value as well as true on success, or false if the variable was not found.

type SourceSlice

type SourceSlice []Source

SourceSlice is a slice of variable substution sources that act as a source itself by returning the first successful lookup.

func (SourceSlice) ListVars

func (s SourceSlice) ListVars() []Var

ListVars will return a slice of all variables that this varsub Source provides.

func (SourceSlice) Lookup

func (s SourceSlice) Lookup(name string) (Var, bool)

Lookup tries to look up a value based on name and returns that value as well as true on success, or false if the variable was not found.

type SourceVar

type SourceVar Var

SourceVar is a single Var that also acts as a Source.

func (SourceVar) ListVars

func (s SourceVar) ListVars() []Var

ListVars will return a slice of only the one variable that this varsub Source provides.

func (SourceVar) Lookup

func (s SourceVar) Lookup(name string) (Var, bool)

Lookup compares the name and returns the variables value as well as true on success, or false if the name does not match.

type Val

type Val struct {
	Value  any
	Source string
}

Val is a slimmed down varsub.Var, without the Key, as the SourceMap will populate the Key field automatically based on the map keys.

func (Val) String

func (v Val) String() string

String implements the fmt.Stringer interface.

type Var

type Var struct {
	Key         string
	Value       any
	SourceLabel string
}

Var is a single varsub variable, with it's Key (name), Value, and optionally also a Source that declares where this variable comes from.

func (Var) GoString

func (v Var) GoString() string

GoString implements the fmt.GoStringer interface.

func (Var) String

func (v Var) String() string

String implements the fmt.Stringer interface.

type VarMatch

type VarMatch struct {
	// Name is the name of the variable inside the match.
	Name string
	// FullMatch is the entire match, including the variable syntax of ${}.
	FullMatch string
	// IsVar is true if this was a match on a variable, or false if it was
	// just a match on the delimiting string
	IsVar bool
}

VarMatch is a single variable match.

func Split

func Split(value string) []VarMatch

Split up a string on its variable and non-variable matches.

Jump to

Keyboard shortcuts

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