bind

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Bind

Bind binds variables values into string expression in fast and safety way. When the variable pattern is invalid, it return an error.

e.g.: should be {price} but written as {price } (with space) will return an error.

Usage

Bind
    s := "{price} - ({price} * {discount-percentage})"
    v, err := bind.Bind(s,
        "price", 100,
        "discount-percentage", 0.1,
    )
    if err != nil {
        panic(err)
    }

    fmt.Println(v) // "100 - (100 * 0.1)"
SetIdent

Using custom identifier.

    bind.SetIdent(&bind.Ident{
        Prefix: ":", 
        Suffix: "",
    })

    s := ":price - (:price * :discount_percentage)"
    v, err := bind.Bind(s,
        "price", 100,
        "discount_percentage", 0.1,
    )
    if err != nil {
        panic(err)
    }

    fmt.Println(v) // "100 - (100 * 0.1)"

Documentation

Overview

Package bind is an helper to bind variable values into the string expression.

Index

Constants

This section is empty.

Variables

View Source
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

func Bind(s string, keyvals ...interface{}) (string, error)

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 Format

func Format(v interface{}) string

Format formats given v type into string.

func SetFormatter

func SetFormatter(formatter Formatter)

SetIdent sets custom keyvals formatter to std. See bind.Formatter for details.

func SetIdent

func SetIdent(ident *Ident)

SetIdent sets custom variable identifier to std. See bind.Ident{} 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{}.

func (*Binder) Bind

func (b *Binder) Bind(s string, keyvals ...interface{}) (string, error)

Bind binds keyvals values into s, key should be a string and val can be any. If keyvals is nil, s will be returned.

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>"

func DefaultFormater

func DefaultFormater() Formatter

DefaultFormater returns format

type Ident

type Ident struct {
	Prefix string // Prefix is mandatory
	Suffix string // Suffix is optional
}

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

type SyntaxError struct {
	Msg   string
	Begin int
	End   int
	Value string
	Err   error
}

func (SyntaxError) Error

func (s SyntaxError) Error() string

func (SyntaxError) Unwrap

func (s SyntaxError) Unwrap() error

Jump to

Keyboard shortcuts

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