-
Notifications
You must be signed in to change notification settings - Fork 21
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
var valuesText = args.Select(a => a == null ? "" : p.Apply(a, data).ToString()).ToArray(); | ||
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. the values are already generated. There is no need to re-apply the evaluations. You can use the values like |
||
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; | ||
}; | ||
} | ||
|
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.
There was a problem hiding this comment.
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: