stdlib

package
v0.34.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package stdlib contains standard library functions exposed to River configs.

Index

Constants

This section is empty.

Variables

View Source
var Identifiers = map[string]interface{}{

	"constants": constants,

	"env": os.Getenv,

	"nonsensitive": func(secret rivertypes.Secret) string {
		return string(secret)
	},

	"concat": value.RawFunction(func(funcValue value.Value, args ...value.Value) (value.Value, error) {
		if len(args) == 0 {
			return value.Array(), nil
		}

		// finalSize is the final size of the resulting concatenated array. We type
		// check our arguments while computing what finalSize will be.
		var finalSize int
		for i, arg := range args {
			if arg.Type() != value.TypeArray {
				return value.Null, value.ArgError{
					Function: funcValue,
					Argument: arg,
					Index:    i,
					Inner: value.TypeError{
						Value:    arg,
						Expected: value.TypeArray,
					},
				}
			}

			finalSize += arg.Len()
		}

		if len(args) == 1 {
			return args[0], nil
		}

		raw := make([]value.Value, 0, finalSize)
		for _, arg := range args {
			for i := 0; i < arg.Len(); i++ {
				raw = append(raw, arg.Index(i))
			}
		}

		return value.Array(raw...), nil
	}),

	"json_decode": func(in string) (interface{}, error) {
		var res interface{}
		err := json.Unmarshal([]byte(in), &res)
		if err != nil {
			return nil, err
		}
		return res, nil
	},

	"coalesce": value.RawFunction(func(funcValue value.Value, args ...value.Value) (value.Value, error) {
		if len(args) == 0 {
			return value.Null, nil
		}

		for _, arg := range args {
			if arg.Type() == value.TypeNull {
				continue
			}

			if !arg.Reflect().IsZero() {
				if argType := value.RiverType(arg.Reflect().Type()); (argType == value.TypeArray || argType == value.TypeObject) && arg.Len() == 0 {
					continue
				}

				return arg, nil
			}
		}

		return args[len(args)-1], nil
	}),
}

Identifiers holds a list of stdlib identifiers by name. All interface{} values are River-compatible values.

Function identifiers are Go functions with exactly one non-error return value, with an optionally supported error return value as the second return value.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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