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

feat: add function to get jsonlogic with solved vars (replaced by values) #66

Conversation

FlorianRuen
Copy link
Contributor

@FlorianRuen FlorianRuen commented Aug 1, 2023

Close #65

I added one function in vars.go (not exported)

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

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

	resultJson, err := json.Marshal(result)

	if err != nil {
		return nil, err
	}

	// we need to use Unquote due to unicode characters (example \u003e= need to be >=)
	// used for prettier json.RawMessage
	resultEscaped, err := strconv.Unquote(strings.Replace(strconv.Quote(string(resultJson)), `\\u`, `\u`, -1))

	if err != nil {
		return nil, err
	}

	return []byte(resultEscaped), nil
}

And one exported function which uses the one above

func GetJsonLogicWithSolvedVars(rule, data json.RawMessage) ([]byte, error) {
	if data == nil {
		data = json.RawMessage("{}")
	}

	// parse rule and data from json.RawMessage to interface
	var _rule interface{}
	var _data interface{}

	err := json.Unmarshal(rule, &_rule)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(data, &_data)
	if err != nil {
		return nil, err
	}

	return solveVarsBackToJsonLogic(_rule, _data)
}

The README.md has also been updated to add an example of this new function and a test has been added in jsonlogic_test.go

@codecov
Copy link

codecov bot commented Aug 2, 2023

Codecov Report

Merging #66 (4d3c856) into master (1a4645b) will decrease coverage by 1.37%.
The diff coverage is 55.88%.

@@            Coverage Diff             @@
##           master      #66      +/-   ##
==========================================
- Coverage   95.84%   94.47%   -1.37%     
==========================================
  Files           9        9              
  Lines         962      996      +34     
==========================================
+ Hits          922      941      +19     
- Misses         28       38      +10     
- Partials       12       17       +5     

@diegoholiveira diegoholiveira merged commit bbdc99d into diegoholiveira:master Aug 2, 2023
6 of 8 checks passed
renovate bot added a commit to open-feature/flagd that referenced this pull request Aug 2, 2023
….3.0 (#785)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[github.com/diegoholiveira/jsonlogic/v3](https://togithub.com/diegoholiveira/jsonlogic)
| require | minor | `v3.2.7` -> `v3.3.0` |

---

### Release Notes

<details>
<summary>diegoholiveira/jsonlogic
(github.com/diegoholiveira/jsonlogic/v3)</summary>

###
[`v3.3.0`](https://togithub.com/diegoholiveira/jsonlogic/releases/tag/v3.3.0)

[Compare
Source](https://togithub.com/diegoholiveira/jsonlogic/compare/v3.2.7...v3.3.0)

#### What's Changed

- feat: add function to get jsonlogic with solved vars (replaced by
values) by [@&#8203;FlorianRuen](https://togithub.com/FlorianRuen) in
[diegoholiveira/jsonlogic#66

#### New Contributors

- [@&#8203;FlorianRuen](https://togithub.com/FlorianRuen) made their
first contribution in
[diegoholiveira/jsonlogic#66

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/open-feature/flagd).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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 this pull request may close these issues.

get the jsonlogic evaluated back from the method that apply
2 participants