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

Adds missing support for lexicographical comparison for numeric operators #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions JsonLogic.Net.UnitTests/JsonLogicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public JsonLogicTests(ITestOutputHelper output)
[InlineData("{`>=`: [3, 1, 1]}", true)]
[InlineData("{`>=`: [3, 4, 1]}", false)]

[InlineData("{`<`: [`2020-01-31`, `2020-02-01`, `2020-02-02`]}", true)]
[InlineData("{`<`: [`2020-01-31`, `2020-02-02`, `2020-02-02`]}", false)]
[InlineData("{`<`: [`2020-01-31`, `2020-02-03`, `2020-02-02`]}", false)]
[InlineData("{`<=`: [`2020-01-31`, `2020-02-01`, `2020-02-02`]}", true)]
[InlineData("{`<=`: [`2020-01-31`, `2020-02-02`, `2020-02-02`]}", true)]
[InlineData("{`<=`: [`2020-01-31`, `2020-02-03`, `2020-02-02`]}", false)]
[InlineData("{`>`: [`2020-02-02`, `2020-02-01`, `2020-01-31`]}", true)]
[InlineData("{`>`: [`2020-02-02`, `2020-01-31`, `2020-01-31`]}", false)]
[InlineData("{`>`: [`2020-02-02`, `2020-02-03`, `2020-01-31`]}", false)]
[InlineData("{`>=`: [`2020-02-02`, `2020-02-01`, `2020-01-31`]}", true)]
[InlineData("{`>=`: [`2020-02-02`, `2020-01-31`, `2020-01-31`]}", true)]
[InlineData("{`>=`: [`2020-02-02`, `2020-02-03`, `2020-01-31`]}", false)]

[InlineData("{`var`: `name`}", "John Doe")]
[InlineData("{`var`: `address.zip`}", "33333")]
[InlineData("{`var`: [`nonexistent`, `default-value`]}", "default-value")]
Expand Down
47 changes: 47 additions & 0 deletions JsonLogic.Net.UnitTests/jsonlogic.com_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1101,5 +1101,52 @@
},
null,
"apple"
],
[
{
"<": [
"2020-01-30",
{
"var": "sys.date"
},
"2020-02-01"
]
},
{
"sys": {
"date": "2020-02-01"
}
},
false
],
[
{
"<": [
"2020-01-30",
{
"var": "sys.date"
},
"2020-02-01"
]
},
{
"sys": {
"date": "2020-01-31"
}
},
true
],
[
{
"<": [
"A",
"B",
"C",
"D",
"E"
]
},
null,
true
]
]
50 changes: 34 additions & 16 deletions JsonLogic.Net/EvaluateOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ private void AddDefaultOperations()
AddOperator("min", (p, args, data) => args.Select(a => Convert.ToDouble(p.Apply(a, data)))
.Aggregate((prev, next) => (prev < next) ? prev : next));

AddOperator("<", DoubleArgsSatisfy((prev, next) => prev < next));

AddOperator("<=", DoubleArgsSatisfy((prev, next) => prev <= next));
AddOperator("<", GenericArgsSatisfy((prev, next) => prev < next, (prev, next) => string.CompareOrdinal(prev, next) < 0));

AddOperator(">", DoubleArgsSatisfy((prev, next) => prev > next));
AddOperator("<=", GenericArgsSatisfy((prev, next) => prev <= next, (prev, next) => string.CompareOrdinal(prev, next) <= 0));

AddOperator(">", GenericArgsSatisfy((prev, next) => prev > next, (prev, next) => string.CompareOrdinal(prev, next) > 0));

AddOperator(">=", DoubleArgsSatisfy((prev, next) => prev >= next));
AddOperator(">=", GenericArgsSatisfy((prev, next) => prev >= next, (prev, next) => string.CompareOrdinal(prev, next) >= 0));

AddOperator("var", (p, args, data) => {
if (args.Count() == 0) return data;
Expand Down Expand Up @@ -300,19 +300,37 @@ private Type DictionaryType(object d)
{
return d.GetType().GetTypeInfo().ImplementedInterfaces.FirstOrDefault(t => t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>));
}

// private Type EnumerableType(object d)
// {
// return d.GetType().GetTypeInfo().ImplementedInterfaces.FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
// }

private Func<IProcessJsonLogic, JToken[], object, object> DoubleArgsSatisfy(Func<double, double, bool> criteria)

private Func<IProcessJsonLogic, JToken[], object, object> GenericArgsSatisfy(Func<Double, Double, bool> criteriaDouble, Func<string, string, bool> criteriaText)
{
return (p, args, data) => {
var values = args.Select(a => a == null ? 0d : Double.Parse(p.Apply(a, data).ToString())).ToArray();
for (int i = 1; i < values.Length; i++) {
if (!criteria(values[i-1], values[i])) return false;
return (p, args, data) =>
{
var values = args
.Where(a => a != null)
.Select(a => p.Apply(a, data))
.Select(a => JToken.FromObject(a))
.ToArray();

// all values text?
var allText = values.All(a => a.Type == JTokenType.String);
if (allText)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, this method handles two cases: text and double. We can extract each of those cases into a method and then simplify this method to something like:

return allText ? CheckCriteria(values.Cast<string>()) : CheckCriteria(values.Cast<double>());

{
var valuesText = args.Select(a => a == null ? "" : p.Apply(a, data).ToString()).ToArray();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the values are already generated. There is no need to re-apply the evaluations. You can use the values like var textValues = values.Cast<strirng>().ToArray();

for (int i = 1; i < valuesText.Length; i++) {

if (!criteriaText(valuesText[i-1], valuesText[i])) return false;
}

return true;
}

// not all values are of type text, assume these are Doubles or any other type and therefore handle this as before
var valuesDouble = values.Select(a => a == null ? 0d : Double.Parse(p.Apply(a, data).ToString())).ToArray();
for (int i = 1; i < valuesDouble.Length; i++) {

if (!criteriaDouble(valuesDouble[i-1], valuesDouble[i])) return false;
}

return true;
};
}
Expand Down