builtins

package
v0.0.0-...-e9b43fb Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package builtins contains function definitions and implementation for built-in mapping functions.

Index

Constants

This section is empty.

Variables

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

	"$Div": Div,
	"$Mod": Mod,
	"$Mul": Mul,
	"$Sub": Sub,
	"$Sum": Sum,

	"$Flatten":        Flatten,
	"$ListCat":        ListCat,
	"$ListLen":        ListLen,
	"$ListOf":         ListOf,
	"$SortAndTakeTop": SortAndTakeTop,
	"$Range":          Range,
	"$UnionBy":        UnionBy,
	"$Unique":         Unique,
	"$UnnestArrays":   UnnestArrays,

	"$CurrentTime":          CurrentTime,
	"$MultiFormatParseTime": MultiFormatParseTime,
	"$ParseTime":            ParseTime,
	"$ParseUnixTime":        ParseUnixTime,
	"$ReformatTime":         ReformatTime,
	"$SplitTime":            SplitTime,

	"$Hash":      Hash,
	"$IntHash":   IntHash,
	"$IsNil":     IsNil,
	"$IsNotNil":  IsNotNil,
	"$MergeJSON": MergeJSON,
	"$UUID":      UUID,
	"$Type":      Type,

	"$DebugString": DebugString,
	"$Void":        Void,

	"$And":  And,
	"$Eq":   Eq,
	"$Gt":   Gt,
	"$GtEq": GtEq,
	"$Lt":   Lt,
	"$LtEq": LtEq,
	"$NEq":  NEq,
	"$Not":  Not,
	"$Or":   Or,

	"$MatchesRegex": MatchesRegex,
	"$ParseFloat":   ParseFloat,
	"$ParseInt":     ParseInt,
	"$SubStr":       SubStr,
	"$StrCat":       StrCat,
	"$StrFmt":       StrFmt,
	"$StrJoin":      StrJoin,
	"$StrSplit":     StrSplit,
	"$ToLower":      ToLower,
	"$ToUpper":      ToUpper,
	"$Trim":         Trim,
}

When adding a built-in, remember to add it to the map below with its name as the key.

View Source
var Now = time.Now

Now is exported to allow for mocking in tests.

Functions

func And

func And(args ...jsonutil.JSONToken) (jsonutil.JSONBool, error)

And is a logical AND of all given arguments.

func CurrentTime

func CurrentTime(format jsonutil.JSONStr, tz jsonutil.JSONStr) (jsonutil.JSONStr, error)

CurrentTime returns the current time based on the Go func time.Now (https://golang.org/pkg/time/#Now). The function accepts a time format layout (https://golang.org/pkg/time/#Time.Format) and an IANA formatted time zone string (https://www.iana.org/time-zones). A string representing the current time is returned. A default layout of '2006-01-02 03:04:05'and a default time zone of 'UTC' will be used if not provided.

func DebugString

func DebugString(t jsonutil.JSONToken) (jsonutil.JSONStr, error)

DebugString converts the JSON element to a string representation by recursively converting objects to strings.

func Div

Div divides the first argument by the second.

func Eq

func Eq(args ...jsonutil.JSONToken) (jsonutil.JSONBool, error)

Eq returns true iff all given arguments are equal.

func Flatten

func Flatten(array jsonutil.JSONArr) (jsonutil.JSONArr, error)

Flatten turns a nested array of arrays (of any depth) into a single array. Item ordering is preserved, depth first.

func Gt

Gt returns true iff the first argument is greater than the second.

func GtEq

func GtEq(left jsonutil.JSONNum, right jsonutil.JSONNum) (jsonutil.JSONBool, error)

GtEq returns true iff the first argument is greater than or equal to the second.

func Hash

Hash converts the given item into a hash. Key order is not considered (array item order is). This is not cryptographically secure, and is not to be used for secure hashing.

func IntHash

func IntHash(obj jsonutil.JSONToken) (jsonutil.JSONNum, error)

IntHash converts the given item into a integer hash. Key order is not considered (array item order is). This is not cryptographically secure, and is not to be used for secure hashing.

func IsNil

func IsNil(object jsonutil.JSONToken) (jsonutil.JSONBool, error)

IsNil returns true iff the given object is nil or empty.

func IsNotNil

func IsNotNil(object jsonutil.JSONToken) (jsonutil.JSONBool, error)

IsNotNil returns true iff the given object is not nil or empty.

func ListCat

func ListCat(args ...jsonutil.JSONArr) (jsonutil.JSONArr, error)

ListCat concatenates all given arrays into one array.

func ListLen

func ListLen(in jsonutil.JSONArr) (jsonutil.JSONNum, error)

ListLen finds the length of the array.

func ListOf

func ListOf(args ...jsonutil.JSONToken) (jsonutil.JSONArr, error)

ListOf creates a list of the given tokens.

func Lt

Lt returns true iff the first argument is less than the second.

func LtEq

func LtEq(left jsonutil.JSONNum, right jsonutil.JSONNum) (jsonutil.JSONBool, error)

LtEq returns true iff the first argument is less than or equal to the second.

func MatchesRegex

func MatchesRegex(str jsonutil.JSONStr, regex jsonutil.JSONStr) (jsonutil.JSONBool, error)

MatchesRegex returns true iff the string matches the regex pattern.

func MergeJSON

func MergeJSON(arr jsonutil.JSONArr, overwriteArrays jsonutil.JSONBool) (jsonutil.JSONToken, error)

MergeJSON merges the elements in the JSONArr into one JSON object by repeatedly calling the merge function. The merge function overwrites single fields and concatenates array fields (unless overwriteArrays is true, in which case arrays are overwritten).

func Mod

Mod returns the remainder of dividing the first argument by the second.

func Mul

func Mul(operands ...jsonutil.JSONNum) (jsonutil.JSONNum, error)

Mul multiplies together all given arguments. Returns 0 if nothing given.

func MultiFormatParseTime

func MultiFormatParseTime(format jsonutil.JSONArr, date jsonutil.JSONStr) (jsonutil.JSONStr, error)

MultiFormatParseTime converts the time in the specified formats to RFC3339 (https://www.ietf.org/rfc/rfc3339.txt) format. It tries the formats in order and returns an error if none of the formats match. The function accepts a go time format layout (https://golang.org/pkg/time/#Time.Format) or Python time format layout (defined in timeTokenMap)

func NEq

func NEq(args ...jsonutil.JSONToken) (jsonutil.JSONBool, error)

NEq returns true iff all given arguments are different.

func Not

func Not(object jsonutil.JSONToken) (jsonutil.JSONBool, error)

Not returns true iff the given value is false.

func Or

func Or(args ...jsonutil.JSONToken) (jsonutil.JSONBool, error)

Or is a logical OR of all given arguments.

func ParseFloat

func ParseFloat(str jsonutil.JSONStr) (jsonutil.JSONNum, error)

ParseFloat parses a string into a float.

func ParseInt

func ParseInt(str jsonutil.JSONStr) (jsonutil.JSONNum, error)

ParseInt parses a string into an int.

func ParseTime

func ParseTime(format jsonutil.JSONStr, date jsonutil.JSONStr) (jsonutil.JSONStr, error)

ParseTime converts the time in the specified format to RFC3339 (https://www.ietf.org/rfc/rfc3339.txt) format. The function accepts a go time format layout (https://golang.org/pkg/time/#Time.Format) or Python time format layout (defined in timeTokenMap)

func ParseUnixTime

func ParseUnixTime(unit jsonutil.JSONStr, ts jsonutil.JSONNum, format jsonutil.JSONStr, tz jsonutil.JSONStr) (jsonutil.JSONStr, error)

ParseUnixTime parses a unit and a unix timestamp into the speficied format. The function accepts a go time format layout (https://golang.org/pkg/time/#Time.Format) and Python time format layout (defined in timeTokenMap)

func Range

func Range(start jsonutil.JSONNum, end jsonutil.JSONNum) (jsonutil.JSONArr, error)

Range generates an array of sequentially ordered number from start (inclusive) to end (exclusive) by a step of 1. Example: $Range(2, 5) returns: [2, 3, 4] $Range(5, 2) returns: [5, 4, 3] $Range(-2, 1) returns: [-2, -1, 0]

func ReformatTime

func ReformatTime(inFormat, date, outFormat jsonutil.JSONStr) (jsonutil.JSONStr, error)

ReformatTime uses a Go or Python time-format to convert date into another Go or Python time-formatted date time.

func SortAndTakeTop

func SortAndTakeTop(arr jsonutil.JSONArr, key jsonutil.JSONStr, desc jsonutil.JSONBool) (jsonutil.JSONToken, error)

SortAndTakeTop sorts the elements in the array by the key in the specified direction and returns the top element.

func SplitTime

func SplitTime(format jsonutil.JSONStr, date jsonutil.JSONStr) (jsonutil.JSONArr, error)

SplitTime splits a time string into components based on the Go (https://golang.org/pkg/time/#Time.Format) and Python time-format provided. An array with all components (year, month, day, hour, minute, second and nanosecond) will be returned.

func StrCat

func StrCat(args ...jsonutil.JSONToken) (jsonutil.JSONStr, error)

StrCat joins the input strings with the separator.

func StrFmt

func StrFmt(format jsonutil.JSONStr, item jsonutil.JSONToken) (jsonutil.JSONStr, error)

StrFmt formats the given item using the given Go format specifier (https://golang.org/pkg/fmt/).

func StrJoin

func StrJoin(sep jsonutil.JSONStr, args ...jsonutil.JSONToken) (jsonutil.JSONStr, error)

StrJoin joins the inputs together and adds the separator between them. Non-string arguments are converted to strings before joining.

func StrSplit

func StrSplit(str jsonutil.JSONStr, sep jsonutil.JSONStr) (jsonutil.JSONArr, error)

StrSplit splits a string by the separator and ignores empty entries.

func Sub

Sub subtracts the second argument from the first.

func SubStr

func SubStr(str jsonutil.JSONStr, start, end jsonutil.JSONNum) (jsonutil.JSONStr, error)

SubStr returns a part of the string that is between the start index (inclusive) and the end index (exclusive). If the end index is greater than the length of the string, the end index is truncated to the length.

func Sum

func Sum(operands ...jsonutil.JSONNum) (jsonutil.JSONNum, error)

Sum adds up all given values.

func ToLower

func ToLower(str jsonutil.JSONStr) (jsonutil.JSONStr, error)

ToLower converts the given string with all unicode characters mapped to their lowercase.

func ToUpper

func ToUpper(str jsonutil.JSONStr) (jsonutil.JSONStr, error)

ToUpper converts the given string with all unicode characters mapped to their uppercase.

func Trim

func Trim(str jsonutil.JSONStr) (jsonutil.JSONStr, error)

Trim strips the leading and trailing whitespace of the input string.

func Type

func Type(object jsonutil.JSONToken) (jsonutil.JSONStr, error)

Type returns the type of the given JSON Token as a string.

func UUID

func UUID() (jsonutil.JSONStr, error)

UUID generates a RFC4122 (https://tools.ietf.org/html/rfc4122) UUID.

func UnionBy

func UnionBy(items jsonutil.JSONArr, keys ...jsonutil.JSONStr) (jsonutil.JSONArr, error)

UnionBy unions the items in the given array by the given keys, such that each item in the resulting array has a unique combination of those keys. The first unique element is picked when deduplicating. The items in the resulting array are ordered deterministically (i.e unioning of array [x, y, z] and array [z, x, x, y], both return [x, y, z]).

E.g: Arguments: items: `[{"id": 1}, {"id": 2}, {"id": 1, "foo": "hello"}]`, keys: "id" Return: [{"id": 1}, {"id": 2}]

func Unique

func Unique(array jsonutil.JSONArr) (jsonutil.JSONArr, error)

Unique returns the unique elements in the array by comparing their hashes.

func UnnestArrays

func UnnestArrays(c jsonutil.JSONContainer) (jsonutil.JSONArr, error)

UnnestArrays takes a json object with nested arrays (e.g.: {"key1": [{}...], "key2": {}}) and returns an unnested array that contains the top level key in the "k" field and each array element, unnested, in the "v" field (e.g.: [{"k": "key1", "v": {}} ...]). If the value of a key is an object, it simply returns that object. The output is sorted by the keys, and the array ordering is preserved. If the nested array is empty, the key is ignored.

E.g: c: `{"key1":[{"a": "z"}, {"b": "y"}], "key2": {"c": "x"}, "key3": []} return: [{"k": "key1", "v":{"a": "z"}}`, {"k": "key1", "v":{"b": "y"}}, {"k": "key2", "v":{"c": "x"}}]

func Void

func Void(unused ...jsonutil.JSONToken) (jsonutil.JSONToken, error)

Void returns nil given any inputs. You non-nil into the Void, the Void nils back.

Types

This section is empty.

Jump to

Keyboard shortcuts

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