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

Correctly decompile negative numbers in TLEs #14317

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var logRetentionDays = -10
var test = (logRetentionDays != -10)

output test bool = test

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"logRetentionDays": -10,
"test": "[not(equals(variables('logRetentionDays'), -10))]"
},
"resources": [],
"outputs": {
"test": {
"type": "bool",
"value": "[variables('test')]"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var sortNumeric = sort(
8
3
10
13
-13
5
],
(x, y) => (x < y)
Expand Down
25 changes: 11 additions & 14 deletions src/Bicep.Decompiler/TemplateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,15 @@ private SyntaxBase ParseJToken(JToken? value)
{
JObject jObject => ParseJObject(jObject),
JArray jArray => ParseJArray(jArray),
JValue jValue => ParseJValue(jValue),
JValue jValue => ParseJValue(jValue, permitExpressions: true),
null => throw new ArgumentNullException(nameof(value)),
_ => throw new ConversionFailedException($"Unrecognized token type {value.Type}", value),
};

private SyntaxBase ParseJTokenExpression(JTokenExpression expression)
=> expression.Value.Type switch
{
JTokenType.String => SyntaxFactory.CreateStringLiteral(expression.Value.Value<string>()!),
JTokenType.Integer => expression.Value.Value<long>() is long value && value >= 0 ? ParseIntegerJToken((JValue)value) : ParseIntegerJToken((JValue)(-value)),
JTokenType.Boolean => expression.Value.Value<bool>() ?
new BooleanLiteralSyntax(SyntaxFactory.TrueKeywordToken, true) :
new BooleanLiteralSyntax(SyntaxFactory.FalseKeywordToken, false),
JTokenType.Null => new NullLiteralSyntax(SyntaxFactory.NullKeywordToken),
=> expression.Value switch {
// we don't want to parse expressions inside an expression - e.g. "[concat('[concat()]')]"
JValue value => ParseJValue(value, permitExpressions: false),
_ => throw new NotImplementedException($"Unrecognized expression {ExpressionsEngine.SerializeExpression(expression)}"),
};

Expand Down Expand Up @@ -718,14 +713,16 @@ private static ExpressionSyntax ParseIntegerJToken(JValue value)
return SyntaxFactory.CreatePositiveOrNegativeInteger(value.Value<long>());
}

private SyntaxBase ParseJValue(JValue value)
private SyntaxBase ParseJValue(JValue value, bool permitExpressions)
=> value.Type switch
{
JTokenType.String => ParseString(value.ToString(CultureInfo.InvariantCulture), value),
JTokenType.Uri => ParseString(value.ToString(CultureInfo.InvariantCulture), value),
JTokenType.String or
JTokenType.Uri or
JTokenType.Date or
JTokenType.Float => permitExpressions ?
ParseString(value.ToString(CultureInfo.InvariantCulture), value) :
SyntaxFactory.CreateStringLiteral(value.ToString(CultureInfo.InvariantCulture)),
anthony-c-martin marked this conversation as resolved.
Show resolved Hide resolved
JTokenType.Integer => ParseIntegerJToken(value),
JTokenType.Date => ParseString(value.ToString(CultureInfo.InvariantCulture), value),
JTokenType.Float => ParseString(value.ToString(CultureInfo.InvariantCulture), value),
JTokenType.Boolean => value.Value<bool>() ?
new BooleanLiteralSyntax(SyntaxFactory.TrueKeywordToken, true) :
new BooleanLiteralSyntax(SyntaxFactory.FalseKeywordToken, false),
Expand Down
Loading