-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom jsonLogic string evaluators (#158)
Signed-off-by: Florian Bacher <[email protected]> Co-authored-by: Todd Baert <[email protected]>
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
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 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 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 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"" | ||
]}"; | ||
|
||
// 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()); | ||
} | ||
} | ||
} |