Documentation ¶
Overview ¶
Package bind is an helper to bind variable values into the string expression.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyValsLengthIsOdd occurs when given keyvals is not matched [key, val] structure. ErrKeyValsLengthIsOdd = errors.New("keyvals's length is odd") // ErrKeyvalsIsEmptyOrNil occurs when keyvals is empty or nil ErrKeyvalsIsEmptyOrNil = errors.New("keyvals is empty or nil") // ErrKeyIsNotAString occurs when given key in keyvals contains non-string type. ErrKeyIsNotAString = errors.New("key in keyvals is not a string") // ErrMalformedVariablePattern occurs when s is malformed or is not valid ErrMalformedVariablePattern = errors.New("malformed variable pattern") // ErrEmptyPrefix occurs when prefix is empty "" while it's a mandatory to bind the variables. ErrEmptyPrefix = errors.New("empty prefix") )
Functions ¶
func Bind ¶
Bind binds given keyvals values into the given s. Key in keyvals should be a string that consist of alphanumeric [a-z, A-Z, 0-9] and symbol ['-', '_'] only.
- e.g. price after discount calculation expression:
s: "{price} - ({price} * {discount-percentage})"
keyvals: ["price", 100, "discount-percentage", 0.1]
resulting value: "100 - (100 * 0.1)"
Note: If s is a really big string (len(s) > 60k for example) consider creating your own binder using strings.Replacer, see [bind_benchmark_test.go] file.
Otherwise, use this for faster process with low memory footprint and low memory alloc.
func SetFormatter ¶
func SetFormatter(formatter Formatter)
SetIdent sets custom keyvals formatter to std. See bind.Formatter for details.
Types ¶
type Binder ¶
type Binder struct { Ident *Ident // variable identifier on string expression Formatter Formatter // keyvals values formatter. }
Binder binds variable values into string expression, it finds the variable name using specified identifier bind.Ident{}.
type Formatter ¶
type Formatter func(v interface{}) string
Formatter formats keyvals values into string values. Key will never be quoted, only the Value will be quoted.
e.g.
- "price" -> "price"
- 100 -> "100"
- 2.1 -> "2.1"
- struct{}{} -> "{}"
- nil -> "<nil>"
type Ident ¶
Ident is variable name identifier. Prefix is mandatory when Suffix is optional.
e.g.
- "{price}" : the "{" is the prefix identifier and "}" is the suffix identifier of variable named price.
- ":price:" : the ":" is the prefix identifier and ":" is the suffix identifier of variable named price.
- ":price" : the ":" is the prefix identifier and "" is the suffix identifier of variable named price.
func DefaultIdent ¶
func DefaultIdent() *Ident
type SyntaxError ¶
func (SyntaxError) Error ¶
func (s SyntaxError) Error() string
func (SyntaxError) Unwrap ¶
func (s SyntaxError) Unwrap() error