-
Notifications
You must be signed in to change notification settings - Fork 811
Add Experimental Feature @nullIfNotFound() for Existing Resources #18697
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
base: main
Are you sure you want to change the base?
Conversation
|
Test this change out locally with the following install scripts (Action run 20277551735) VSCode
Azure CLI
|
Dotnet Test Results 102 files - 51 102 suites - 51 39m 47s ⏱️ - 19m 52s Results for commit 0c7e587. ± Comparison against base commit 56292d6. This pull request removes 1956 and adds 676 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
| /// <summary> | ||
| /// Checks if the syntax has a decorator with the specified name. | ||
| /// </summary> | ||
| public static bool HasDecorator(this DecorableSyntax syntax, string decoratorName) | ||
| => syntax.Decorators.Any(d => d.Expression is FunctionCallSyntax functionCall && functionCall.NameEquals(decoratorName)); | ||
|
|
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.
Decorators can be fully qualified, so you can't always make this determination based on syntax alone. There is a helper method that handles instance function calls, but you would need to call it with the model:
bicep/src/Bicep.Core/Semantics/SemanticModelHelper.cs
Lines 48 to 64 in 56292d6
| public static DecoratorSyntax? TryGetDecoratorInNamespace(SemanticModel semanticModel, DecorableSyntax syntax, string @namespace, string decoratorName) | |
| => TryGetDecoratorInNamespace(semanticModel.Binder, semanticModel.TypeManager.GetDeclaredType, syntax, @namespace, decoratorName); | |
| public static DecoratorSyntax? TryGetDecoratorInNamespace(IBinder binder, Func<SyntaxBase, TypeSymbol?> getDeclaredTypeFunc, DecorableSyntax syntax, string @namespace, string decoratorName) | |
| { | |
| return syntax.Decorators.FirstOrDefault(decorator => | |
| { | |
| if (SymbolHelper.TryGetSymbolInfo(binder, getDeclaredTypeFunc, decorator.Expression) is not FunctionSymbol functionSymbol || | |
| functionSymbol.DeclaringObject is not NamespaceType namespaceType) | |
| { | |
| return false; | |
| } | |
| return LanguageConstants.IdentifierComparer.Equals(namespaceType.ExtensionName, @namespace) && | |
| LanguageConstants.IdentifierComparer.Equals(functionSymbol.Name, decoratorName); | |
| }); | |
| } |
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.
Removed from SyntaxExtensions and added TryGetDecoratorInNamespace to IsResourceEnabled
| private bool? IsResourceEnabled(ResourceSymbol resource) | ||
| { | ||
| // Nullable existing resources may not exist at deployment time | ||
| if (resource.DeclaringResource.IsNullableExistingResource()) |
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.
Is this the only place .IsNullableExistingResource() is called? If so, you could simplify a bit by doing the decorator check here.
e9e6057 to
0c7e587
Compare
Description
Adds nullable existing resource support with @nullIfNotFound decorator. Note, @nullIfNotFound() will not work until backend changes have been deployed
Example Usage
Checklist
Microsoft Reviewers: Open in CodeFlow