-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add custom jsonLogic string evaluators #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b0f780
add custom jsonLogic string evaluators
bacherfl 959156f
add unit tests
bacherfl cf951e2
fix formatting
bacherfl 36e5f40
Merge branch 'main' into feat/jsonlogic-string-comparison
toddbaert da9813e
additional tests
bacherfl f26811b
Merge branch 'feat/jsonlogic-string-comparison' of https://github.com…
bacherfl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...penFeature.Contrib.Providers.Flagd/Resolver/InProcess/CustomEvaluators/StringEvaluator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using JsonLogic.Net; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace OpenFeature.Contrib.Providers.Flagd.Resolver.InProcess.CustomEvaluators | ||
{ | ||
internal class StringEvaluator | ||
{ | ||
internal object StartsWith(IProcessJsonLogic p, JToken[] args, object data) | ||
{ | ||
return p.Apply(args[0], data).ToString().StartsWith(p.Apply(args[1], data).ToString()); | ||
} | ||
|
||
internal object EndsWith(IProcessJsonLogic p, JToken[] args, object data) | ||
{ | ||
return p.Apply(args[0], data).ToString().EndsWith(p.Apply(args[1], data).ToString()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
test/OpenFeature.Contrib.Providers.Flagd.Test/StringEvaluatorTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System.Collections.Generic; | ||
using JsonLogic.Net; | ||
using Newtonsoft.Json.Linq; | ||
using OpenFeature.Contrib.Providers.Flagd.Resolver.InProcess.CustomEvaluators; | ||
using Xunit; | ||
|
||
namespace OpenFeature.Contrib.Providers.Flagd.Test | ||
{ | ||
public class StringEvaluatorTest | ||
{ | ||
|
||
[Fact] | ||
public void StartsWith() | ||
{ | ||
// Arrange | ||
var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default); | ||
var stringEvaluator = new StringEvaluator(); | ||
EvaluateOperators.Default.AddOperator("starts_with", stringEvaluator.StartsWith); | ||
|
||
var targetingString = @"{""starts_with"": [ | ||
{ | ||
""var"": [ | ||
""color"" | ||
] | ||
}, | ||
""yellow"" | ||
]}"; | ||
|
||
// Parse json into hierarchical structure | ||
var rule = JObject.Parse(targetingString); | ||
|
||
var data = new Dictionary<string, string> { { "color", "yellowcolor" } }; | ||
|
||
// Act & Assert | ||
var result = evaluator.Apply(rule, data); | ||
Assert.True(result.IsTruthy()); | ||
|
||
data.Clear(); | ||
data.Add("color", "blue"); | ||
|
||
result = evaluator.Apply(rule, data); | ||
Assert.False(result.IsTruthy()); | ||
} | ||
|
||
[Fact] | ||
public void EndsWith() | ||
{ | ||
// Arrange | ||
var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default); | ||
var stringEvaluator = new StringEvaluator(); | ||
EvaluateOperators.Default.AddOperator("ends_with", stringEvaluator.EndsWith); | ||
|
||
var targetingString = @"{""ends_with"": [ | ||
{ | ||
""var"": [ | ||
""color"" | ||
] | ||
}, | ||
""purple"" | ||
]}"; | ||
Comment on lines
+53
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multi-line strings work great for this. The equivalent tests are so ugly in Java. |
||
|
||
// Parse json into hierarchical structure | ||
var rule = JObject.Parse(targetingString); | ||
|
||
var data = new Dictionary<string, string> { { "color", "deep-purple" } }; | ||
|
||
// Act & Assert | ||
var result = evaluator.Apply(rule, data); | ||
Assert.True(result.IsTruthy()); | ||
|
||
data.Clear(); | ||
data.Add("color", "purple-nightmare"); | ||
|
||
result = evaluator.Apply(rule, data); | ||
Assert.False(result.IsTruthy()); | ||
} | ||
|
||
[Fact] | ||
public void NonStringTypeInRule() | ||
{ | ||
// Arrange | ||
var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default); | ||
var stringEvaluator = new StringEvaluator(); | ||
EvaluateOperators.Default.AddOperator("ends_with", stringEvaluator.EndsWith); | ||
|
||
var targetingString = @"{""ends_with"": [ | ||
{ | ||
""var"": [ | ||
""color"" | ||
] | ||
}, | ||
1 | ||
]}"; | ||
|
||
// Parse json into hierarchical structure | ||
var rule = JObject.Parse(targetingString); | ||
|
||
var data = new Dictionary<string, string> { { "color", "deep-purple" } }; | ||
|
||
// Act & Assert | ||
var result = evaluator.Apply(rule, data); | ||
Assert.False(result.IsTruthy()); | ||
} | ||
|
||
[Fact] | ||
public void NonStringTypeInData() | ||
{ | ||
// Arrange | ||
var evaluator = new JsonLogicEvaluator(EvaluateOperators.Default); | ||
var stringEvaluator = new StringEvaluator(); | ||
EvaluateOperators.Default.AddOperator("ends_with", stringEvaluator.EndsWith); | ||
|
||
var targetingString = @"{""ends_with"": [ | ||
{ | ||
""var"": [ | ||
""color"" | ||
] | ||
}, | ||
""green"" | ||
]}"; | ||
|
||
// Parse json into hierarchical structure | ||
var rule = JObject.Parse(targetingString); | ||
|
||
var data = new Dictionary<string, int> { { "color", 5 } }; | ||
|
||
// Act & Assert | ||
var result = evaluator.Apply(rule, data); | ||
Assert.False(result.IsTruthy()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.