-
Notifications
You must be signed in to change notification settings - Fork 40
Add metadata strings for operators and improve CLI output #102
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
Draft
yane3628
wants to merge
8
commits into
main
Choose a base branch
from
yane3628/cliImprovements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04e0e8a
Add failure message for each operator
yane3628 e7a6e0f
Fix merge conflicts
yane3628 d176d9c
Use TestUtilities function to reduce duplicate code
yane3628 ac3eb43
Add failure message for in operator
yane3628 b33c11e
Use constant placeholder when calling FailureMessage in Result
yane3628 d9c7ff2
Add negation logic to the failure message
yane3628 9cd1832
Add line number, file path, and failure message to CLI output
yane3628 8664a96
Add failure message for structured operators
yane3628 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 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
334 changes: 334 additions & 0 deletions
334
src/Analyzer.JsonRuleEngine.UnitTests/JsonRuleResultTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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,26 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Azure.Templates.Analyzer.RuleEngines.JsonEngine.Constants | ||
| { | ||
| /// <summary> | ||
| /// Defines all constants used in JsonEngine | ||
| /// </summary> | ||
| internal class JsonRuleEngineConstants | ||
| { | ||
| internal const string ActualValuePlaceholder = "{actualValue}"; | ||
| internal const string PathPlaceholder = "{path}"; | ||
| internal const string ExpectedValuePlaceholder = "{expectedValue}"; | ||
| internal const string NegationPlaceholder = "{negation}"; | ||
|
|
||
| internal static string EqualsFailureMessage = $"Value \"{ActualValuePlaceholder}\" found at \"{PathPlaceholder}\" is {NegationPlaceholder} equal to \"{ExpectedValuePlaceholder}\"."; | ||
| internal static string ExistsFailureMessage = $"Value found at \"{PathPlaceholder}\" exists: {ActualValuePlaceholder}, expected: {ExpectedValuePlaceholder}."; | ||
| internal static string HasValueFailureMessage = $"Value found at \"{PathPlaceholder}\" has a value: {ActualValuePlaceholder}, expected: {ExpectedValuePlaceholder}."; | ||
| internal static string RegexFailureMessage = $"Value \"{ActualValuePlaceholder}\" found at \"{PathPlaceholder}\" does not match regex pattern: \"{ExpectedValuePlaceholder}\"."; | ||
| internal static string InFailureMessage = $"Value \"{ExpectedValuePlaceholder}\" is not in the list at path \"{PathPlaceholder}\"."; | ||
|
|
||
| internal static string AnyOfFailureMessage = $"No evaluations evaluted to true for the following json property: \"{PathPlaceholder}\"."; | ||
| internal static string AllOfFailureMessage = $"One or more evaluations were false for the following json property: \"{PathPlaceholder}\"."; | ||
|
|
||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Azure.Templates.Analyzer.RuleEngines.JsonEngine.Constants; | ||
| using Microsoft.Azure.Templates.Analyzer.RuleEngines.JsonEngine.Expressions; | ||
| using Microsoft.Azure.Templates.Analyzer.Types; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace Microsoft.Azure.Templates.Analyzer.RuleEngines.JsonEngine | ||
| { | ||
|
|
@@ -27,8 +29,32 @@ internal class JsonRuleResult : IResult | |
| internal string JsonPath { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the expression associated with this result | ||
| /// Gets the expression associated with this result. | ||
| /// </summary> | ||
| internal Expression Expression { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the actual value present at the specified path. | ||
| /// </summary> | ||
| internal JToken ActualValue { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the messsage which explains why the evaluation failed. | ||
| /// </summary> | ||
| public string FailureMessage() | ||
|
Contributor
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. As much as possible, I think all this logic should be in the Operators and Expressions. I think it would be easier to understand and write if the failure messages were built right where the evaluations happen. |
||
| { | ||
| string failureMessage = Expression.FailureMessage; | ||
|
|
||
| if (Expression is LeafExpression) | ||
| { | ||
| string expectedValue = ((Expression as LeafExpression).Operator.SpecifiedValue == null || (Expression as LeafExpression).Operator.SpecifiedValue.ToObject<object>() == null) ? "null" : (Expression as LeafExpression).Operator.SpecifiedValue.ToString(); | ||
| failureMessage = failureMessage.Replace(JsonRuleEngineConstants.ExpectedValuePlaceholder, expectedValue); | ||
| failureMessage = failureMessage.Replace(JsonRuleEngineConstants.NegationPlaceholder, (Expression as LeafExpression).Operator.IsNegative ? "" : "not"); | ||
| } | ||
|
|
||
| string actualValue = (ActualValue == null || ActualValue.ToObject<object>() == null) ? "null" : ActualValue.ToString(); | ||
| failureMessage = failureMessage.Replace(JsonRuleEngineConstants.ActualValuePlaceholder, actualValue); | ||
| return failureMessage.Replace(JsonRuleEngineConstants.PathPlaceholder, JsonPath); | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
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
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
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.
I think it would be better if each of these was implemented as a method directly in the corresponding Operator, and the placeholders would be filled in using parameters to the method.
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.
Alternatively, maybe we could update how
EvaluateExpression()works, and either create the string and have it ready in another property after evaluation, or return the string as part of the function call.