jsonlogic

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 6 Imported by: 0

README

jsonlogic-go

Go Report Card PkgGoDev

A jsonlogic library in golang.

It is meant to be a 'stricter' version comparing to the js version: It should behave the same as the js version, if not then an error should be returned.

This means that if an expression is evaluated successful in server side using this library, then it is expected to be evaluated to the same result in client side. But the reverse direction maybe not true.

Notable restrictions

  • ==/!= operations are not supported. Use strict version instead: ===/!==
  • Many operations will check the minimal number of params. See doc for detail. Some examples:
    • {"var":[]} is ok in js. But not ok in jsonlogic-go. ({"var":null} or {"var":[null]} is ok though)
    • {"===":["x"]} is ok in js. But jsonlogic-go requires at least two params.
  • Many operations will check the type of params. Some examples:
    • Comparing and equality checking only accepts json primitves (null/bool/numeric/string)
      • You can still {">":[1,"-1"]} but not {">":[1,[]]}
  • No NaN/+Inf/-Inf:
    • {"/":[1,0]} gets null in js but got an error in this library.

Reference

Alternatives

Alternative jsonlogic implementation in golang you may also interested.

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultJSONLogic is the JSONLogic instance used by package-level Apply/AddOperation.
	DefaultJSONLogic = New()
)

Functions

func AddOpAdd

func AddOpAdd(jl *JSONLogic)

AddOpAdd adds "+" operation to the JSONLogic instance. Param restriction:

  • Must be evaluated to json primitives that can convert to numeric.

func AddOpAll

func AddOpAll(jl *JSONLogic)

AddOpAll adds "all" operation to the JSONLogic instance. Param restriction:

  • At least two params: the first evaluated to an array and the second the logic.

func AddOpAnd

func AddOpAnd(jl *JSONLogic)

AddOpAnd adds "and" operation to the JSONLogic instance. Param restriction:

  • At least one param.

func AddOpCat

func AddOpCat(jl *JSONLogic)

AddOpCat adds "cat" operation to the JSONLogic instance. Params restriction:

  • All items must be evaluated to json primitives that can converted to string.

func AddOpDiv

func AddOpDiv(jl *JSONLogic)

AddOpDiv adds "/" operation to the JSONLogic instance. Param restriction:

  • At least two params.
  • Must be evaluated to json primitives that can convert to numeric.

func AddOpDoubleNegative

func AddOpDoubleNegative(jl *JSONLogic)

AddOpDoubleNegative adds "!!" operation to the JSONLogic instance. Param Restriction: the same as "!".

func AddOpFilter

func AddOpFilter(jl *JSONLogic)

AddOpFilter adds "filter" operation to the JSONLogic instance. Param restriction:

  • At least two params: the first evaluated to an array and the second the logic.

func AddOpGreaterEqual

func AddOpGreaterEqual(jl *JSONLogic)

AddOpGreaterEqual adds ">=" operation to the JSONLogic instance. Param restriction: the same as "<".

func AddOpGreaterThan

func AddOpGreaterThan(jl *JSONLogic)

AddOpGreaterThan adds ">" operation to the JSONLogic instance. Param restriction: the same as "<".

func AddOpIf

func AddOpIf(jl *JSONLogic)

AddOpIf adds "if"/"?:" operation to the JSONLogic instance.

func AddOpIn

func AddOpIn(jl *JSONLogic)

AddOpIn adds "in" operation to the JSONLogic instance. Params restriction:

  • At least two params: the first to check and the second evaluated to an array or string.
  • All items must be evaluated to json primitives.

func AddOpLessEqual

func AddOpLessEqual(jl *JSONLogic)

AddOpLessEqual adds "<=" operation to the JSONLogic instance. Param restriction: the same as "<".

func AddOpLessThan

func AddOpLessThan(jl *JSONLogic)

AddOpLessThan adds "<" operation to the JSONLogic instance. Param restriction:

  • At least two params.
  • Must be evaluated to json primitives.
  • If comparing numerics, then params must be able to convert to numeric. (See ToNumeric)

func AddOpMap

func AddOpMap(jl *JSONLogic)

AddOpMap adds "map" operation to the JSONLogic instance. Param restriction:

  • At least two params: the first evaluated to an array and the second the logic.

func AddOpMax

func AddOpMax(jl *JSONLogic)

AddOpMax adds "max" operation to the JSONLogic instance. Param restriction: the same as "and".

func AddOpMerge

func AddOpMerge(jl *JSONLogic)

AddOpMerge adds "merge" operation to the JSONLogic instance.

func AddOpMin

func AddOpMin(jl *JSONLogic)

AddOpMin adds "min" operation to the JSONLogic instance. Param restriction:

  • Must be evaluated to json primitives that can convert to numeric.

func AddOpMinus

func AddOpMinus(jl *JSONLogic)

AddOpMinus adds "-" operation to the JSONLogic instance. Param restriction:

  • At least one param.
  • Must be evaluated to json primitives that can convert to numeric.

func AddOpMissing

func AddOpMissing(jl *JSONLogic)

AddOpMissing adds "missing" operation to the JSONLogic instance. NOTE: null/"" is considered as missing, for example:

logic: {"missing":"a"}
data: {"a":null}
result will be ["a"]

ref:

  • json-logic-js/logic.js::"missing"

func AddOpMissingSome

func AddOpMissingSome(jl *JSONLogic)

AddOpMissingSome adds "missing_some" operation to the JSONLogic instance. Param restriction:

  • At least 2 params.
  • The first must be evaluated to a numeric and the second evaluated to an array.

func AddOpMod

func AddOpMod(jl *JSONLogic)

AddOpMod adds "%" operation to the JSONLogic instance. Param restriction:

  • At least two params.
  • Must be evaluated to json primitives that can convert to numeric.

func AddOpMul

func AddOpMul(jl *JSONLogic)

AddOpMul adds "*" operation to the JSONLogic instance. Param restriction:

  • At least one param.
  • Must be evaluated to json primitives that can convert to numeric.

func AddOpNegative

func AddOpNegative(jl *JSONLogic)

AddOpNegative adds "!" operation to the JSONLogic instance. Param restriction:

  • At least one param.

func AddOpNone

func AddOpNone(jl *JSONLogic)

AddOpNone adds "none" operation to the JSONLogic instance. Param restriction:

  • At least two params: the first evaluated to an array and the second the logic.

func AddOpOr

func AddOpOr(jl *JSONLogic)

AddOpOr adds "or" operation to the JSONLogic instance. Param restriction:

  • At least one param.

func AddOpReduce

func AddOpReduce(jl *JSONLogic)

AddOpReduce adds "reduce" operation to the JSONLogic instance. Param restriction:

  • At least three params: the first evaluated to an array, the second the logic and the third the initial value.

func AddOpSome

func AddOpSome(jl *JSONLogic)

AddOpSome adds "some" operation to the JSONLogic instance. Param restriction:

  • At least two params: the first evaluated to an array and the second the logic.

func AddOpStrictEqual

func AddOpStrictEqual(jl *JSONLogic)

AddOpStrictEqual adds "===" operation to the JSONLogic instance. Param restriction:

  • At least two params.
  • Params must be evaluated to json primitives.

func AddOpStrictNotEqual

func AddOpStrictNotEqual(jl *JSONLogic)

AddOpStrictNotEqual adds "!==" operation to the JSONLogic instance. Param restriction: the same as "===".

func AddOpSubstr

func AddOpSubstr(jl *JSONLogic)

AddOpSubstr adds "substr" operation to the JSONLogic instance. Params restriction:

  • Two to three params: the first evaluated to string, and the second/third to numbers.

func AddOpVar

func AddOpVar(jl *JSONLogic)

AddOpVar adds "var" operation to the JSONLogic instance. Param restriction:

  • At least one param (the key).
  • Keys must be evaluated to json primitives.

func AddOperation

func AddOperation(name string, op Operation)

AddOperation is equivalent to DefaultJSONLogic.AddOperation.

func Apply

func Apply(logic, data interface{}) (res interface{}, err error)

Apply is equivalent to DefaultJSONLogic.Apply.

func ApplyParams added in v0.2.0

func ApplyParams(apply Applier, params []interface{}, data interface{}) ([]interface{}, error)

ApplyParams apply data to an array of params. Useful in operation implementation.

func CompareValues added in v0.3.0

func CompareValues(symbol CompSymbol, left, right interface{}) (bool, error)

CompareValues compares json primitives. It should be the same as JavaScript's "<"/"<="/">"/">="/"==="/"!==", except:

  • an error is returned if any value is not a json primitive.
  • any error retuend by ToNumeric.

ref:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than > First, objects are converted to primitives using Symbol.ToPrimitive with the hint parameter be 'number'. > If both values are strings, they are compared as strings, based on the values of the Unicode code points they contain. > Otherwise JavaScript attempts to convert non-numeric types to numeric values: > Boolean values true and false are converted to 1 and 0 respectively. > null is converted to 0. > undefined is converted to NaN. > Strings are converted based on the values they contain, and are converted as NaN if they do not contain numeric values. > If either value is NaN, the operator returns false. > Otherwise the values are compared as numeric values.

func IsPrimitive added in v0.3.0

func IsPrimitive(obj interface{}) bool

IsPrimitive returns true if obj is json primitive (null/bool/float64/string).

func ToBool added in v0.3.0

func ToBool(obj interface{}) bool

ToBool returns the truthy of a json object. ref:

func ToNumeric added in v0.3.0

func ToNumeric(obj interface{}) (f float64, err error)

ToNumeric converts json primitive to numeric. It should be the same as JavaScript's Number(), except:

  • an error is returned if obj is not a json primitive.
  • an error is returned if obj is string but not well-formed.
  • the number is NaN or +Inf/-Inf.

func ToString added in v0.3.0

func ToString(obj interface{}) (string, error)

ToString converts json primitive to string. It should be the same as JavaScript's String(), except:

  • an error is returned if obj is not a json primitive.
  • obj is number NaN or +Inf/-Inf.

Types

type Applier

type Applier func(logic, data interface{}) (res interface{}, err error)

type CompSymbol added in v0.3.0

type CompSymbol string

CompSymbol represents compare operator.

const (
	LT CompSymbol = "<"
	LE CompSymbol = "<="
	GT CompSymbol = ">"
	GE CompSymbol = ">="
	EQ CompSymbol = "==="
	NE CompSymbol = "!=="
)

type JSONLogic

type JSONLogic struct {
	// contains filtered or unexported fields
}

JSONLogic is an evaluator of json logic with a set of operations.

func Clone added in v0.3.2

func Clone() *JSONLogic

Clone is equivalent to DefaultJSONLogic.Clone.

func New

func New() *JSONLogic

New creates a root (no parent) JSONLogic with standard operations.

func NewEmpty

func NewEmpty() *JSONLogic

NewEmpty creates a root (no parent) JSONLogic with no operation.

func NewInherit added in v0.4.0

func NewInherit(parent *JSONLogic) *JSONLogic

NewInherit creates a child JSONLogic instance.

func (*JSONLogic) AddOperation

func (jl *JSONLogic) AddOperation(name string, op Operation)

AddOperation adds a named operation to JSONLogic instance. Can override parent's same name operation.

func (*JSONLogic) Apply

func (jl *JSONLogic) Apply(logic, data interface{}) (res interface{}, err error)

Apply data to logic and returns a result. Both logic/data must be one of 'encoding/json' supported types:

  • nil
  • bool
  • float64
  • string
  • []interface{} with items of supported types
  • map[string]interface{} with values of supported types

func (*JSONLogic) Clone added in v0.3.2

func (jl *JSONLogic) Clone() *JSONLogic

Clone clones a JSONLogic instance.

type Operation

type Operation func(apply Applier, params []interface{}, data interface{}) (interface{}, error)

type TestCase added in v0.3.1

type TestCase struct {
	Logic  string
	Data   string
	Result interface{}
	Err    bool
}

TestCase is a single test case.

func (TestCase) Run added in v0.3.1

func (tc TestCase) Run(a *assert.Assertions, jl *JSONLogic)

Run a single test case

type TestCases added in v0.3.1

type TestCases []TestCase

TestCases is a set of test cases.

func (TestCases) Run added in v0.3.1

func (tcs TestCases) Run(a *assert.Assertions, jl *JSONLogic)

Run a set of test cases.

Directories

Path Synopsis
Package ext contains some useful extension operations.
Package ext contains some useful extension operations.

Jump to

Keyboard shortcuts

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