-
Notifications
You must be signed in to change notification settings - Fork 754
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
Fix for decompiler to support templatespec #10329
Conversation
9ac23cb
to
4c83044
Compare
ResourceGroupLevelResourceId id; | ||
var templateSpec = ""; | ||
if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) { | ||
throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource); | ||
} | ||
templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last(); | ||
return SyntaxFactory.CreateStringLiteral(templateSpec); |
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.
Some styling/simplification suggestions:
- Apart from places where it's necessary, I would recommend avoiding separating the declaration & assignment of variables. For example, instead of:
You could write:
ResourceGroupLevelResourceId id; var templateSpec = ""; if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) { throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource); } templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
if (ResourceGroupLevelResourceId.TryParse(templateID, out var id) is not true) { throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource); } var templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
- String interpolation usually offers a cleaner approach for building strings. Instead of:
You could write:
var templateSpec = "ts:" + id.SubscriptionId + "/" + id.ResourceGroup + "/" + id.NameHierarchy.First() + ":" + id.NameHierarchy.Last();
var templateSpec = $"ts:{id.SubscriptionId}/{id.ResourceGroup}/{id.NameHierarchy.First()}:{id.NameHierarchy.Last()}";
- The
is not true
syntax you're using is a relatively new feature for C# - pattern matching. It's really powerful for some scenarios (such as checking for null), but checking a boolean value, it's simpler and more idiomatic to just reference it directly in theif
statement. Instead of:You could write:if (ResourceGroupLevelResourceId.TryParse(templateID, out var id) is not true) {
if (!ResourceGroupLevelResourceId.TryParse(templateID, out var id)) {
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.
Updated the code. Thanks!
if ((pathProperty?.Value<string>() is not string && idProperty?.Value<string>() is not string )) | ||
{ | ||
throw new ConversionFailedException($"Unable to find \"uri\" or \"relativePath\" properties under {resource["name"]}.properties.templateLink for linked template.", resource); | ||
throw new ConversionFailedException($"Unable to find \"uri\" or \"relativePath\" or \"id\" properties under {resource["name"]}.properties.templateLink for linked template.", resource); | ||
} | ||
var templatePathString = idProperty?.Value<string>() ?? pathProperty?.Value<string>() ?? ""; |
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'd recommend restructuring this a bit (pulling your logic from line 1424 up), to cut down on the casts, usage of ?
, improving variable naming, and avoiding conflating uses of the templatePathString
property:
SyntaxBase modulePath;
Uri? jsonTemplateUri = null;
if (pathProperty?.Value<string>() is string templatePathString)
{
modulePath = ...
}
else if (idProperty?.Value<string>() is string templateSpecIdString)
{
modulePath = GetModuleFromId(templateSpecIdString, resource);
}
else
{
throw new ConversionFailedException...
}
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.
Updated it. Thanks!
ResourceGroupLevelResourceId id; | ||
var templateSpec = ""; | ||
if (ResourceGroupLevelResourceId.TryParse(templateID, out id) is not true) { | ||
throw new ConversionFailedException($"Unable to parse resourceId {templateID}", resource); |
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.
This exception is going to be shown to users - could you make it more descriptive? The context of why we're trying to parse this resourceId may not be clear.
Suggestion of something like the following: "Unable to interpret \"{templateID}\" as a valid template spec resource id under property {resource["name"]}.properties.templateLink.id"
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.
Updated it. Thanks!!
25b9c6e
to
43d4b2d
Compare
4d00906
to
8dfc69a
Compare
8dfc69a
to
7be192c
Compare
This PR adds support for template spec while decompiling [#3246 ].
It add supports for the case where the id has the resourceId which is not parameterized.
Contributing a Pull Request
If you haven't already, read the full contribution guide. The guide may have changed since the last time you read it, so please double-check. Once you are done and ready to submit your PR, run through the relevant checklist below.
Contributing a feature
Microsoft Reviewers: Open in CodeFlow