gg_fnvars

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Function Variables

Expression engine for variables to solve at runtime.

This is useful when you need a dynamic value in template engines or text expressions.

Example: "This is a RND number with 3 digits: <var>rnd|number|3</var>"

func TestSolveAll(t *testing.T) {
	statements := []string{
		"this is a random string: (<var>rnd:alphanumeric:6</var>)",
		"this is a random number: (<var>rnd:numeric:6</var>)",
		"this is a random string LOWERCASE: (<var>rnd|alphanumeric|6|lower</var>)",
		"this is a random string UPPERCASE: (<var>rnd|alphanumeric|6|upper</var>)",
		"this is a random GUID UPPERCASE: (<var>rnd|guid|upper</var>)",
		"this is a random Num Between: (<var>rnd|between|0-10</var>)",
		"this is a random ID: (<var>rnd|id</var>)",
		"this is a DateTime: (<var>date|yyyy-MM-dd HH:mm|upper</var>)",
		"this is a DateTime ISO86001: (<var>date|iso|upper</var>)",
		"this is a DateTime Unix: (<var>date|unix</var>)",
		"this is a DateTime Ruby: (<var>date|ruby</var>)",
	}
	engine := gg.FnVars.NewEngine()
	for i, statement := range statements {
		text, err := engine.SolveText(statement)
		if nil != err {
			t.Error(err)
			t.FailNow()
		}
		fmt.Println(i, statement, text)
	}

}

Expressions Syntax

Function Variables are cosed in <var> tags.

Example: <var>rnd</var>

Each Function Variable can have some "options". Options are "optional" data that allow a Function Variable to handle parametrization.

For example, if we need a 3 digits number we can write a Function Variable like this: <var>rnd|numeric|3</var>.

Here is Function Variable explanation:

  • rnd: Function Variable name
  • numeric: Option telling the engine to extract a number
  • 3: Option telling the engine that the number should have 3 digits

Documentation

Index

Constants

View Source
const (
	ToolCtx = "ctx"
)
View Source
const ToolDate = "date"
View Source
const (
	ToolRnd = "rnd"
)
View Source
const ToolUser = "user"

Variables

This section is empty.

Functions

func GetTokens

func GetTokens(text string) []string

func ParseTools

func ParseTools(text string, registered map[string]FnVarTool) map[string]FnVarTool

ParseTools a text and extract a map of all tools supported

func SplitToken

func SplitToken(token string) []string

func Transform

func Transform(text string, transform string) string

Types

type FnVarTool

type FnVarTool interface {
	Name() string
	Solve(token string, context ...interface{}) (interface{}, error)
}

func GetTool

func GetTool(token string, registered map[string]FnVarTool) FnVarTool

GetTool retrieve a registered tool parsing token (i.e. $rnd:alphanumeric:6)

type FnVarToolCtx

type FnVarToolCtx struct{}

FnVarToolCtx ctx|var1|4, ctx|var2

func (*FnVarToolCtx) Name

func (instance *FnVarToolCtx) Name() string

func (*FnVarToolCtx) Solve

func (instance *FnVarToolCtx) Solve(token string, context ...interface{}) (interface{}, error)

type FnVarToolDate

type FnVarToolDate struct{}

FnVarToolDate date|yyyy-MM-dd|upper

func (*FnVarToolDate) Name

func (instance *FnVarToolDate) Name() string

func (*FnVarToolDate) Solve

func (instance *FnVarToolDate) Solve(token string, context ...interface{}) (interface{}, error)

type FnVarToolRnd

type FnVarToolRnd struct{}

FnVarToolRnd rnd|chars|4|upper

func (*FnVarToolRnd) Name

func (instance *FnVarToolRnd) Name() string

func (*FnVarToolRnd) Solve

func (instance *FnVarToolRnd) Solve(token string, context ...interface{}) (interface{}, error)

type FnVarToolUser

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

FnVarToolUser person|name,mobile,mail|3

func (*FnVarToolUser) GetDataRoot

func (instance *FnVarToolUser) GetDataRoot() string

func (*FnVarToolUser) Name

func (instance *FnVarToolUser) Name() string

func (*FnVarToolUser) SetDataRoot

func (instance *FnVarToolUser) SetDataRoot(value string)

func (*FnVarToolUser) Solve

func (instance *FnVarToolUser) Solve(token string, context ...interface{}) (interface{}, error)

type FnVarsEngine

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

func (*FnVarsEngine) GetByName

func (instance *FnVarsEngine) GetByName(toolName string) FnVarTool

func (*FnVarsEngine) Register

func (instance *FnVarsEngine) Register(tool FnVarTool)

func (*FnVarsEngine) Solve

func (instance *FnVarsEngine) Solve(input interface{}, context ...interface{}) (interface{}, error)

func (*FnVarsEngine) SolveArray

func (instance *FnVarsEngine) SolveArray(a []string, context ...interface{}) ([]string, error)

func (*FnVarsEngine) SolveMap

func (instance *FnVarsEngine) SolveMap(m map[string]string, context ...interface{}) (map[string]string, error)

func (*FnVarsEngine) SolveText

func (instance *FnVarsEngine) SolveText(text string, context ...interface{}) (string, error)

type FnVarsHelper

type FnVarsHelper struct {
}
var FnVars *FnVarsHelper

func (*FnVarsHelper) NewEngine

func (instance *FnVarsHelper) NewEngine() *FnVarsEngine

Jump to

Keyboard shortcuts

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