jsonlogic

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2021 License: MIT Imports: 7 Imported by: 0

README

Build Status

json-logic-go

This parser accepts JsonLogic rules and executes them in Go.

The JsonLogic format is designed to allow you to share rules (logic) between front-end and back-end code (regardless of language difference), even to store logic along with a record in a database. JsonLogic is documented extensively at JsonLogic.com, including examples of every supported operation and a place to try out rules in your browser.

API

Apply

func Apply(inputs ...string) (interface{}, error)

The first input is the json rule, as a string:

Apply(`{ "==" : [1, 1] } `);

The second input, optional, is the data source:

Apply(`{ "var" : ["a"] }`, `{ a : 1, b: 2 }`);

Apply() uses json.Unmarshal to convert the string inputs to generic interface{}.

The return value is of type interface{} because it can be anything such as a string, boolean, map, array, etc. The function may also return an error.

ApplyJSONInterfaces

func ApplyJSONInterfaces(inputs ...interface{}) (interface{}, error)

This has the same behavior as Apply(), but expects inputs already unmarshalled. This should not be used with your own custom types, but rather ONLY with unmarshalled json.

AddOperation

func AddOperation(name string, implementation Operation) error

This allows you to extend the range of operations provided by jsonlogic.
An Operation has should have the following signature: func(args ...interface{}) (interface{}, error)

For example:

func add(args ...interface{}) (interface{}, error) {
	x, y := float64(args[0].(float64)), float64(args[1].(float64))

	return (x + y), nil
}

jsonlogic.AddOperation("add", add)
result, _ = Apply(`{"add": [1, 2]}`)

Documentation

Overview

Package jsonlogic is a lightweight rule engine that uses the syntax defined on jsonlogic.com

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddOperation

func AddOperation(name string, implementation Operation) error

AddOperation allows you to add a custom operation that will run a Go function. The `implementation` must be an `Operation`. See type definition.

func Apply

func Apply(inputs ...string) (interface{}, error)

Apply takes in a rule and an optional data object and applies its logic. Parameters are passed as strings and will be unmarshalled.

func ApplyJSONInterfaces

func ApplyJSONInterfaces(inputs ...interface{}) (interface{}, error)

ApplyJSONInterfaces takes a rule and an optional data object and applies its logic. The parameters are unmarshalled JSON interfaces. Note this is not meant to be used with any other types except an interface{} generated by Go's Unmarshal method.

Types

type Operation

type Operation func(args ...interface{}) (interface{}, error)

Operation defines an operation with function signature: `func(args ...interface{}) (interface{}, error)` The first argument is the value(s) passed to the operation, the second argument is an optional data array.

Jump to

Keyboard shortcuts

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