-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* FEATURE: #2560 DateTime Query Evaluator #2561 (#2562) * chore: query docs * feat: date time property evaluator * bug: null location guard check * feat: enhanced query command test suite * chore: formatting failure * chore: codeql warning * bug: fixed failing string op test case * feat: date time test cases Co-authored-by: Michael C. Fanning <mikefan@microsoft.com>
- Loading branch information
1 parent
d73a262
commit 31f49b2
Showing
10 changed files
with
292 additions
and
19 deletions.
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Query.Evaluators | ||
{ | ||
/// <summary> | ||
/// DateTimeEvaluator implements IExpressionEvaluator given a getter which can | ||
/// get the desired Property Name as a DateTime. | ||
/// | ||
/// Usage: | ||
/// if (String.Equals(term.PropertyName, "ID", StringComparison.OrdinalIgnoreCase)) | ||
/// { | ||
/// // Show the DateTimeEvaluator how to get the 'Timestamp' property as a DateTime, and it'll implement the term matching. | ||
/// return new DateTimeEvaluator<Result>(result => result.ID, term); | ||
/// } | ||
/// </summary> | ||
/// <typeparam name="T">Type of Item Evaluator will evaluate.</typeparam> | ||
public class DateTimeEvaluator<T> : IExpressionEvaluator<T> | ||
{ | ||
private readonly Func<T, DateTime> _getter; | ||
private readonly DateTime _value; | ||
private readonly Action<ICollection<T>, BitArray> _evaluateSet; | ||
|
||
public DateTimeEvaluator(Func<T, DateTime> getter, TermExpression term) | ||
{ | ||
_getter = getter; | ||
|
||
if (!DateTime.TryParse(term.Value, out DateTime parsedValue)) { throw new QueryParseException($"{term} value {term.Value} was not a valid DateTime format."); } | ||
_value = parsedValue; | ||
|
||
_evaluateSet = Comparer(term); | ||
} | ||
|
||
public void Evaluate(ICollection<T> list, BitArray matches) | ||
{ | ||
_evaluateSet(list, matches); | ||
} | ||
|
||
private Action<ICollection<T>, BitArray> Comparer(TermExpression term) | ||
{ | ||
switch (term.Operator) | ||
{ | ||
case CompareOperator.Equals: | ||
return EvaluateEquals; | ||
case CompareOperator.NotEquals: | ||
return EvaluateNotEquals; | ||
case CompareOperator.LessThan: | ||
return EvaluateLessThan; | ||
case CompareOperator.LessThanOrEquals: | ||
return EvaluateLessThanOrEquals; | ||
case CompareOperator.GreaterThan: | ||
return EvaluateGreaterThan; | ||
case CompareOperator.GreaterThanOrEquals: | ||
return EvaluateGreaterThanOrEquals; | ||
default: | ||
throw new QueryParseException($"{term} does not support operator {term.Operator}"); | ||
} | ||
} | ||
|
||
private void EvaluateEquals(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, _getter(item).Equals(_value)); | ||
i++; | ||
} | ||
} | ||
|
||
private void EvaluateNotEquals(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, !_getter(item).Equals(_value)); | ||
i++; | ||
} | ||
} | ||
|
||
private void EvaluateLessThan(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, _getter(item) < _value); | ||
i++; | ||
} | ||
} | ||
|
||
private void EvaluateLessThanOrEquals(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, _getter(item) <= _value); | ||
i++; | ||
} | ||
} | ||
|
||
private void EvaluateGreaterThan(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, _getter(item) > _value); | ||
i++; | ||
} | ||
} | ||
|
||
private void EvaluateGreaterThanOrEquals(ICollection<T> list, BitArray matches) | ||
{ | ||
int i = 0; | ||
foreach (T item in list) | ||
{ | ||
matches.Set(i, _getter(item) >= _value); | ||
i++; | ||
} | ||
} | ||
} | ||
} |
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
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
77 changes: 77 additions & 0 deletions
77
src/Test.UnitTests.Sarif.Multitool.Library/TestData/QueryCommand/WithProperties.sarif
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,77 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.1", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "Test", | ||
"version": "1.0.0", | ||
"rules": [ | ||
{ | ||
"id": "TEST0001", | ||
"name": "Test", | ||
"shortDescription": { | ||
"text": "Test description." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description." | ||
} | ||
}, | ||
"properties": { | ||
"cwe": "CWE-755", | ||
"security-severity": 8.0, | ||
"vulnPublicationDate": "2022-04-24T10:58:25Z" | ||
} | ||
}, | ||
{ | ||
"id": "TEST0002", | ||
"name": "Test 2", | ||
"shortDescription": { | ||
"text": "Test description 2." | ||
}, | ||
"messageStrings": { | ||
"default": { | ||
"text": "Test description 2." | ||
} | ||
}, | ||
"properties": { | ||
"cwe": "CWE-766", | ||
"security-severity": 3.0, | ||
"vulnPublicationDate": "2022-01-15T10:58:25Z" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"ruleId": "TEST0001", | ||
"ruleIndex": 0, | ||
"message": { | ||
"text": "Test text." | ||
}, | ||
"properties": { | ||
"packageManager": "nuget", | ||
"severity": 2.0, | ||
"patchPublicationDate": "2022-06-01T10:58:25Z" | ||
} | ||
}, | ||
{ | ||
"ruleId": "TEST0002", | ||
"ruleIndex": 1, | ||
"message": { | ||
"text": "Test text 2." | ||
}, | ||
"properties": { | ||
"packageManager": "npm", | ||
"severity": 9.0, | ||
"patchPublicationDate": "2022-10-01T10:58:25Z" | ||
} | ||
} | ||
], | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
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
Oops, something went wrong.