library

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JPLJSONStripper = jpl.JPLStripperFunc(func(k *string, v any, iter jpl.IterFunc) (result any, remove bool, err jpl.JPLError) {
	r := v
	if t, ok := v.(jpl.JPLType); ok {
		var err jpl.JPLError
		if r, err = t.JSON(); err != nil {
			return nil, false, err
		}
	}
	return RawStripper(k, r, iter)
})

Stripper that allows JSON like values and unwraps JPLTypes

View Source
var JPLStripper = jpl.JPLStripperFunc(func(k *string, v any, iter jpl.IterFunc) (result any, remove bool, err jpl.JPLError) {
	r := v
	if t, ok := v.(jpl.JPLType); ok {
		var err jpl.JPLError
		if r, err = t.JSON(); err != nil {
			return nil, false, err
		}
	}
	if _, ok := v.(jpl.JPLFunc); ok {
		return r, false, nil
	}
	return RawStripper(k, r, iter)
})

Stripper that only allows normalized values and unwraps JPLTypes

View Source
var JPLTypedStripper = jpl.JPLStripperFunc(func(k *string, v any, iter jpl.IterFunc) (result any, remove bool, err jpl.JPLError) {
	if _, ok := v.(jpl.JPLType); ok {
		return v, false, nil
	}
	if _, ok := v.(jpl.JPLFunc); ok {
		return v, false, nil
	}
	return RawStripper(k, v, iter)
})

Stripper that allows JPLTypes and normalized values

View Source
var JSONStripper = jpl.JPLStripperFunc(func(k *string, v any, iter jpl.IterFunc) (result any, remove bool, err jpl.JPLError) {
	r := v
	if m, ok := r.(json.Marshaler); ok {
		bytes, err := m.MarshalJSON()
		if err != nil {
			return nil, false, AdaptError(err)
		}
		err = json.Unmarshal(bytes, &r)
		if err != nil {
			return nil, false, AdaptError(err)
		}
	}
	return RawStripper(k, r, iter)
})

Stripper that allows JSON like values and parses `json.Marshaler` interfaces

View Source
var RawStripper = jpl.JPLStripperFunc(func(k *string, v any, iter jpl.IterFunc) (result any, remove bool, err jpl.JPLError) {
	top := k == nil

	switch v := v.(type) {
	case jpl.JPLFunc:
		return nil, !top, nil
	case string, bool:
		return v, false, nil
	case float64:
		if math.IsNaN(v) || math.IsInf(v, 0) {
			return nil, false, nil
		}
		return v, false, nil
	case []any:
		changes := make([]*ArrayEntry[any], len(v))
		for i, entry := range v {
			key := strconv.Itoa(i)
			result, remove, err := iter(&key, entry)
			if err != nil {
				return nil, false, err
			} else if remove {
				result = nil
			}
			changes[i] = &ArrayEntry[any]{i, result}
		}
		return ApplyArray(v, changes, nil), false, nil
	case map[string]any:
		changes := make([]*ObjectEntry[any], 0, len(v))
		for i, entry := range v {
			result, remove, err := iter(&i, entry)
			if err != nil {
				return nil, false, err
			} else if remove {
				result = nil
			}
			changes = append(changes, &ObjectEntry[any]{i, result, remove})
		}
		return ApplyObject(v, changes), false, nil
	case nil:
		return nil, false, nil
	default:
		return nil, false, NewFatalError(fmt.Sprintf("unexpected %T", v))
	}
})

Stripper that allows JSON like values

Functions

func AdaptError

func AdaptError(err error) jpl.JPLError

Wrap error in a JPLExecutionError if it is not already a JPLError

func AlterJPLType

func AlterJPLType(t jpl.JPLType, updater jpl.JPLModifier) (any, jpl.JPLError)

Alter the specified JPLType

func AlterValue

func AlterValue(value any, updater jpl.JPLModifier) (any, jpl.JPLError)

Alter the specified normalized value using the specified updater

func ApplyArray

func ApplyArray[Value any](source []Value, changes []*ArrayEntry[Value], filler Value) []Value

Apply all changes immutably to the source array. Indices can be negative to be applied from the end of the array.

func ApplyCombinations

func ApplyCombinations[Value any](source []Value, combinations [][]Value) [][]Value

Create all possible combinations immutably. If the specified `combinations` array is empty, the resulting array contains a single empty array. This function has essentially the same base functionality as the `mux` function, but uses a more performant approach for generating immutable arrays as it reduces the number of necessary array copies.

`applyCombinations([], [[1, 2], [3, 4]])` for example produces: - `[1, 3]` - `[1, 4]` - `[2, 3]` - `[2, 4]`

If the values of `source` are equal to the values of one of the combinations, it is used instead of a copy in the output array, e.g.: `let i = [1, 2]; applyCombinations(i, [[1], [2]])[0] == i` - `true`

func ApplyObject

func ApplyObject[Value any](source map[string]Value, changes []*ObjectEntry[Value]) map[string]Value

Apply all changes immutably to the source object.

func AssertType

func AssertType(value any, expectedType jpl.JPLDataType) (any, jpl.JPLError)

Assert the type for the specified unwrapped value

func Compare

func Compare(a, b any) (int, jpl.JPLError)

Compare the specified normalized values

func CompareArrays

func CompareArrays(a, b any) (int, jpl.JPLError)

Compare the specified normalized arrays based on their lexical order

func CompareObjects

func CompareObjects(a, b any) (int, jpl.JPLError)

Compare the specified normalized objects

func CompareStrings

func CompareStrings(a, b any) (int, jpl.JPLError)

Compare the specified normalized strings based on their unicode code points

func CopyMap

func CopyMap[Key comparable, Value any](source map[Key]Value) (result map[Key]Value)

Copy the specified map

func CopySlice

func CopySlice[Value any](source []Value) (result []Value)

Copy the specified slice

func DisplayValue

func DisplayValue(value any) (string, jpl.JPLError)

Format the specified normalized value as a string

func Equals

func Equals(a, b any) (bool, jpl.JPLError)

Determine if the specified normalized values can be considered to be equal

func GetMapKeys

func GetMapKeys[Key comparable, Value any](source map[Key]Value) []Key

Return the specified map's keys

func GetMapValues

func GetMapValues[Key comparable, Value any](source map[Key]Value) []Value

Return the specified map's values

func IsSame

func IsSame(a, b any) bool

Check if both values are the same.

JPLTypes are compared by type and `JPLType.IsSame`, JPLFuncs, []any and map[string]any are compared by memory address, and other comparable values using `==`.

func MarshalJPLType

func MarshalJPLType(t jpl.JPLType) ([]byte, jpl.JPLError)

Marshal the specified JPLType as JSON

func Merge

func Merge(a, b any) (any, jpl.JPLError)

Deep merge the specified normalized values

func MergeMaps

func MergeMaps[Key comparable, Value any](sources ...map[Key]Value) (result map[Key]Value)

Merge the specified maps into a single one

func MergeSegments

func MergeSegments[Value any](segments [][]Value) []Value

Create a single array from the specified array segments

func Mux

func Mux[Input any](args [][]Input, cb jpl.IMuxer[Input]) jpl.JPLError

Multiplex the specified array of arguments by calling cb for all possible combinations of arguments.

`mux([[1,2], [3,4]], cb)` for example yields: - `cb(1, 3)` - `cb(1, 4)` - `cb(2, 3)` - `cb(2, 4)`

func MuxAll

func MuxAll[Input any, Output any](args [][]Input, cb jpl.IOMuxer[Input, []Output]) ([]Output, jpl.JPLError)

Multiplex the specified array of arguments and return a single array of all merged result arrays produced by the callbacks

func MuxOne

func MuxOne[Input any, Output any](args [][]Input, cb jpl.IOMuxer[Input, Output]) ([]Output, jpl.JPLError)

Multiplex the specified array of arguments and return the results produced by the callbacks

func NativeFunction

func NativeFunction(fn func(runtime jpl.JPLRuntime, input any, args ...any) ([]any, error)) jpl.JPLFunc

Wrap the specified function to allow it to be used as a JPL function.

`next` must be called for each single result, which produces an array of results itself. All resulting results must be returned as a single array.

It is recommended to check the provided JPLRuntimeSignal in asynchronous routines to stop the routine when execution has been canceled: ``` if err := signal.CheckHealth(); err != nil { return nil, err } ```

func NewError

func NewError(message string, name string) jpl.JPLError

func NewErrorEnclosure

func NewErrorEnclosure(inner jpl.JPLError) jpl.JPLErrorEnclosure

func NewExecutionError

func NewExecutionError(message string, name string) jpl.JPLExecutionError

func NewFatalError

func NewFatalError(message string) jpl.JPLFatalError

func NewPiperWithScope

func NewPiperWithScope(piper jpl.JPLPiper) jpl.JPLScopedPiper

A JPLPiper can be used in place of a JPLScopedPiper

func NewPiperWithoutScope

func NewPiperWithoutScope(piper jpl.JPLScopedPiper, scope jpl.JPLRuntimeScope) jpl.JPLPiper

A JPLScopedPiper can be used in place of a JPLPiper

func NewReferenceError

func NewReferenceError(value any, replacements ...any) (jpl.JPLReferenceError, jpl.JPLError)

`value` can by of any type. If at least one replacement is specified, the value is formatted as a template.

func NewRuntimeError

func NewRuntimeError(value any, replacements ...any) (jpl.JPLRuntimeError, jpl.JPLError)

`value` can by of any type. If at least one replacement is specified, the value is formatted as a template.

func NewRuntimeScope

func NewRuntimeScope(presets *jpl.JPLRuntimeScopeConfig) jpl.JPLRuntimeScope

func NewRuntimeSignal

func NewRuntimeSignal(parent jpl.JPLRuntimeSignal) jpl.JPLRuntimeSignal

func NewSyntaxError

func NewSyntaxError(message string) jpl.JPLSyntaxError

func NewType

func NewType(value any) (jpl.JPLType, jpl.JPLError)

func NewTypeConversionError

func NewTypeConversionError(value any, replacements ...any) (jpl.JPLTypeConversionError, jpl.JPLError)

`value` can by of any type. If at least one replacement is specified, the value is formatted as a template.

func NewTypeError

func NewTypeError(value any, replacements ...any) (jpl.JPLTypeError, jpl.JPLError)

`value` can by of any type. If at least one replacement is specified, the value is formatted as a template.

func NewZeroDivisionError

func NewZeroDivisionError(value any, replacements ...any) (jpl.JPLZeroDivisionError, jpl.JPLError)

`value` can by of any type. If at least one replacement is specified, the value is formatted as a template.

func Normalize

func Normalize(value any) (any, jpl.JPLError)

Normalize the specified external value

func NormalizeValue

func NormalizeValue(value any) (any, jpl.JPLError)

// Normalize the specified external value

func NormalizeValues

func NormalizeValues(values any, name string) ([]any, jpl.JPLError)

Normalize the specified array of external values

func ObjectFromEntries

func ObjectFromEntries[Value any](entries []*ObjectEntry[Value]) map[string]Value

func OrphanFunction

func OrphanFunction(argNames []string, instructions definition.Pipe, presets *jpl.JPLRuntimeScopeConfig) jpl.JPLFunc

Create an orphan JPL function from the specified instructions.

Some optional scope presets may be specified, e.g. for allowing the function access to some specified variables. Other than that, the function does not have access to any external variables.

func ScopedFunction

func ScopedFunction(argNames []string, instructions definition.Pipe, scope jpl.JPLRuntimeScope) jpl.JPLFunc

Create a scoped JPL function from the specified instructions.

The function is bound to the specified scope.

func StrictDisplayValue

func StrictDisplayValue(value any) (string, jpl.JPLError)

Format the specified normalized value as a string, without removing escaping

func Stringify

func Stringify(value any, unescapeString bool, escapeFunctions bool) (string, jpl.JPLError)

Stringify the specified normalized value

func StringifyJSON

func StringifyJSON(value any, unescapeString bool) (string, jpl.JPLError)

Stringify the specified normalized value for usage in program outputs

func Strip

func Strip(value any, replacer jpl.JPLReplacer, stripper jpl.JPLStripper) (any, jpl.JPLError)

Unwrap the specified value similar to `JSON.stringify`. However, unlike with `JSON.stringify`, object member functions `toJSON` are not supported as they could interfere with user defined members. Instead, JPLTypes are being unwrapped by default.

A custom stripper can be provided to customize the behavior.

func StripJSON

func StripJSON(value any) (any, jpl.JPLError)

Strip the specified normalized value for usage in program outputs

func StripValue

func StripValue(value any) (any, jpl.JPLError)

Strip the specified normalized value for usage in JPL operations

func StripValues

func StripValues(values any, name string) ([]any, jpl.JPLError)

Strip the specified array of normalized values for usage in JPL operations

func SubSlice

func SubSlice[Value any](source []Value, from int, to int) []Value

Return a sub slice of the specified slice.

`from` and `to` are allowed to be negative or to be outside of the range of `source`.

func Template

func Template(tmpl any, replacements ...any) (string, jpl.JPLError)

Format the specified template string. The general form of a format is a percent sign, followed by optional flags, an optional width and a verb.

Examples: - `%10s`: Format the next replacement as a string and pads the result at the left with spaces to be at least 10 unicode codepoints long. - `%*<10v`: Format the next replacement as a JSON value and truncates it, if it is longer then 10 unicode codepoints.

The width specifies the desired field width and defaults to whatever is necessary to display the full replacement. If the width is specified without any other corresponding flags, it is used for padding the field if necessary.

Valid flags:

- `*`: Do not pad the value even if it is shorter than the specified width - `-`: Pad the value at the right rather than the left - `<`: Truncate the value at the right if it is too long for the specified width

Valid verbs:

- `%`: Returns a literal `%` - `s`: Format the next replacement as a string (like JSON, but does not escape strings) - `v`: Format the next replacement as a JSON value

func ThrowAny

func ThrowAny[ErrorType jpl.JPLError](err1 ErrorType, err2 jpl.JPLError) jpl.JPLError

Return any of the specified errors.

func Truthy

func Truthy(value any) (bool, jpl.JPLError)

Determine whether the specified normalized value should be considered as truthy in JPL operations

func Type

func Type(value any) (jpl.JPLDataType, jpl.JPLError)

Resolve the type of the specified normalized value for JPL operations

func TypeOf

func TypeOf(value any) (jpl.JPLDataType, jpl.JPLError)

Resolve the type of the specified normalized value

func Unwrap

func Unwrap(value any) (any, jpl.JPLError)

Unwrap the specified normalized value

func UnwrapValue

func UnwrapValue(value any) (any, jpl.JPLError)

Unwrap the specified normalized value for usage in JPL operations

func UnwrapValues

func UnwrapValues(values any, name string) ([]any, jpl.JPLError)

Unwrap the specified array of normalized values for usage in JPL operations

Types

type ArrayEntry

type ArrayEntry[Value any] struct {
	Index int
	Value Value
}

func ArrayEntries

func ArrayEntries[Value any](source []Value) []*ArrayEntry[Value]

type JPLPiperMuxer

type JPLPiperMuxer struct{ jpl.JPLPiper }

A JPLPiper can be used as an IOMuxer. For each argument, `JPLPiper.Pipe` is called and the results are combined into a single array.

func NewPiperMuxer

func NewPiperMuxer(piper jpl.JPLPiper) JPLPiperMuxer

func (JPLPiperMuxer) Mux

func (m JPLPiperMuxer) Mux(args ...any) ([]any, jpl.JPLError)

type JPLScopedPiperMuxer

type JPLScopedPiperMuxer struct {
	jpl.JPLScopedPiper
	Scope jpl.JPLRuntimeScope
}

A JPLScopedPiper can be used as an IOMuxer. For each argument, `JPLScopedPiper.Pipe` is called and the results are combined into a single array.

func NewScopedPiperMuxer

func NewScopedPiperMuxer(piper jpl.JPLScopedPiper, scope jpl.JPLRuntimeScope) JPLScopedPiperMuxer

func (JPLScopedPiperMuxer) Mux

func (m JPLScopedPiperMuxer) Mux(args ...any) ([]any, jpl.JPLError)

type ObjectEntry

type ObjectEntry[Value any] struct {
	Key     string
	Value   Value
	NoValue bool
}

func FilteredObjectEntries

func FilteredObjectEntries[Value any](source map[string]Value, ignoredKeys ...string) []*ObjectEntry[Value]

func ObjectEntries

func ObjectEntries[Value any](source map[string]Value) []*ObjectEntry[Value]

Jump to

Keyboard shortcuts

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