Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get the jsonlogic evaluated back from the method that apply #65

Closed
FlorianRuen opened this issue Jul 31, 2023 · 3 comments · Fixed by #66
Closed

get the jsonlogic evaluated back from the method that apply #65

FlorianRuen opened this issue Jul 31, 2023 · 3 comments · Fixed by #66

Comments

@FlorianRuen
Copy link
Contributor

FlorianRuen commented Jul 31, 2023

Hey @diegoholiveira,

I'm using this library to apply a jsonlogic built from my UI (using https://react-querybuilder.js.org), but for my use case, I need to help users to "debug" if the response is false

To achieve this, my idea is to print to the user, the logic applied, but I want the vars replaced directly. To be clear, instead of

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

I need

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

I haven't seen any function that can achieve this (only solveVars and getVar)
There is any way to get this result ?

Kindly

@FlorianRuen
Copy link
Contributor Author

@diegoholiveira
I've created a new function on a fork of your repo using solveVars

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
}

It seems working for all my cases. Is this a good way to do it?
If so, I can do some cleaning and create a pull request on this repo

@diegoholiveira
Copy link
Owner

hi @FlorianRuen ,

It looks like a good approach, so yeah, clear it out and send me PR and I'll review it.

@FlorianRuen
Copy link
Contributor Author

FlorianRuen commented Aug 1, 2023

@diegoholiveira You can find the pull request right here : #66
Feel free to ask if changes are needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants