Skip to content

CIDgravity/jsonlogic

 
 

Repository files navigation

Go JSON Logic (CIDgravity custom version)

Forked from https://github.com/diegoholiveira/jsonlogic For original documentation, view the original repository

Added function to generate the json logic with solved variables

func solveVarsBackToJsonLogic(rule, data interface{}) ([]byte, error) {
	ruleMap := rule.(map[string]interface{})
	result := make(map[string]interface{})

	for operator, values := range ruleMap {
		result[operator] = solveVars(values, data)
	}

	body, err := json.Marshal(result)

	if err != nil {
		return nil, err
	}

	return body, nil
}

For example, if you specify the folowing rules model :

{
  "and":[
    { "==":[{ "var":"VariableA" }, true] },
    { "==":[{ "var":"VariableB" }, true] },
    { ">=":[{ "var":"VariableC" }, 17179869184] },
    { "==":[{ "var":"VariableD" }, "0"] },
    { "<":[{ "var":"VariableE" }, 20] }
  ]
}

You will get as output, the folowing response (using a specific data, all variables will be replaced with matching values) :

{
  "and":[
    { "==":[false, true] },
    { "==":[true, true] },
    { ">=":[34359738368, 17179869184] },
    { "==":[12, "0"] },
    { "<":[14, 20] }
  ]
}

About

Go Lang implementation of JsonLogic

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%