Documentation ¶
Index ¶
- Variables
- func AddOpAdd(jl *JSONLogic)
- func AddOpAll(jl *JSONLogic)
- func AddOpAnd(jl *JSONLogic)
- func AddOpCat(jl *JSONLogic)
- func AddOpDiv(jl *JSONLogic)
- func AddOpDoubleNegative(jl *JSONLogic)
- func AddOpFilter(jl *JSONLogic)
- func AddOpGreaterEqual(jl *JSONLogic)
- func AddOpGreaterThan(jl *JSONLogic)
- func AddOpIf(jl *JSONLogic)
- func AddOpIn(jl *JSONLogic)
- func AddOpLessEqual(jl *JSONLogic)
- func AddOpLessThan(jl *JSONLogic)
- func AddOpMap(jl *JSONLogic)
- func AddOpMax(jl *JSONLogic)
- func AddOpMerge(jl *JSONLogic)
- func AddOpMin(jl *JSONLogic)
- func AddOpMinus(jl *JSONLogic)
- func AddOpMissing(jl *JSONLogic)
- func AddOpMissingSome(jl *JSONLogic)
- func AddOpMod(jl *JSONLogic)
- func AddOpMul(jl *JSONLogic)
- func AddOpNegative(jl *JSONLogic)
- func AddOpNone(jl *JSONLogic)
- func AddOpOr(jl *JSONLogic)
- func AddOpReduce(jl *JSONLogic)
- func AddOpSome(jl *JSONLogic)
- func AddOpStrictEqual(jl *JSONLogic)
- func AddOpStrictNotEqual(jl *JSONLogic)
- func AddOpSubstr(jl *JSONLogic)
- func AddOpVar(jl *JSONLogic)
- func AddOperation(name string, op Operation)
- func Apply(logic, data interface{}) (res interface{}, err error)
- func ApplyParams(apply Applier, params []interface{}, data interface{}) ([]interface{}, error)
- func CompareValues(symbol CompSymbol, left, right interface{}) (bool, error)
- func IsPrimitive(obj interface{}) bool
- func ToBool(obj interface{}) bool
- func ToNumeric(obj interface{}) (f float64, err error)
- func ToString(obj interface{}) (string, error)
- type Applier
- type CompSymbol
- type JSONLogic
- type Operation
- type TestCase
- type TestCases
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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:
- http://jsonlogic.com/truthy.html
- json-logic-js/logic.js::truthy
Types ¶
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 NewEmpty ¶
func NewEmpty() *JSONLogic
NewEmpty creates a root (no parent) JSONLogic with no operation.
func NewInherit ¶ added in v0.4.0
NewInherit creates a child JSONLogic instance.
func (*JSONLogic) AddOperation ¶
AddOperation adds a named operation to JSONLogic instance. Can override parent's same name operation.