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

Add a resource<'type@version'> utility type #12591

Merged
merged 26 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c0aac54
Rename '<' and '>' tokens to LeftChevron and RightChevron, respectively
jeskew Nov 2, 2023
0fae639
Add syntax kind + parser support for utility types
jeskew Nov 2, 2023
cd0b425
Add a utility type for declaring a type that matches the entirety of …
jeskew Nov 30, 2023
0fef927
Fix failing tests
jeskew Dec 1, 2023
2f84542
Move resource type behind feature flag
jeskew Dec 1, 2023
7b109c6
Regenerate baselines
jeskew Dec 1, 2023
ab981b8
Ensure unbound types are replaced in imported function overloads
jeskew Dec 1, 2023
be98da0
Update config schema and tests to reflect new feature flag
jeskew Dec 1, 2023
84f72bf
Support namespace-qualified parameterized types
jeskew Dec 6, 2023
6cc32a6
Add missing binder test
jeskew Dec 6, 2023
e9eea38
Offer completions for the resource utility type
jeskew Dec 6, 2023
533c039
Merge branch 'main' into jeskew/resource-utility-type
jeskew Dec 6, 2023
4f34752
Add signature help support for parameterized types
jeskew Dec 11, 2023
21631ab
Merge branch 'main' into jeskew/resource-utility-type
jeskew Dec 11, 2023
7eeb422
Merge branch 'main' into jeskew/resource-utility-type
jeskew Dec 15, 2023
67653d8
Merge branch 'main' into jeskew/resource-utility-type
jeskew Jan 3, 2024
b3fc5c7
Merge branch 'main' into jeskew/resource-utility-type
jeskew Jan 4, 2024
72c0ed8
Merge branch 'main' into jeskew/resource-utility-type
jeskew Jan 4, 2024
8b1cfcf
Correct merge issue
jeskew Jan 4, 2024
121685e
Add baseline
jeskew Jan 4, 2024
f8c36ae
Update SignatureHelpTests to expect signature help for parameterizedT…
jeskew Jan 4, 2024
5951549
Add some baselines
anthony-c-martin Jan 5, 2024
80c1dd6
Fix V1 formatting issue
jeskew Jan 5, 2024
96a0954
Fix failing tests
jeskew Jan 5, 2024
32db842
Clean up diagnostic reporting on `sealed` misuses
jeskew Jan 5, 2024
d4b213e
Fix failing test
jeskew Jan 5, 2024
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
154 changes: 154 additions & 0 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,4 +2042,158 @@ func b() string[] => ['c', 'd']
result.Template.Should().NotBeNull();
result.Template.Should().HaveValueAtPath("languageVersion", "2.0");
}

[TestMethod]
public void Resource_derived_types_are_bound_when_imported_from_ARM_JSON_models()
{
var result = CompilationHelper.Compile(new ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true, CompileTimeImportsEnabled: true)),
("main.bicep", """
import {foo} from 'mod.json'

param location string = resourceGroup().location
param fooParam foo = {
bar: {
name: 'acct'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
properties: {
unknownProperty: false
}
}
}

output fooOutput foo = fooParam
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"definitions": {
"foo": {
"metadata": {
"{{LanguageConstants.MetadataExportedPropertyName}}": true
},
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
},
"resources": []
}
"""));

result.Should().NotHaveAnyCompilationBlockingDiagnostics();
result.Should().HaveDiagnostics(new[]
{
("BCP037", DiagnosticLevel.Warning, """The property "unknownProperty" is not allowed on objects of type "StorageAccountPropertiesCreateParametersOrStorageAccountProperties". Permissible properties include "accessTier", "allowBlobPublicAccess", "allowCrossTenantReplication", "allowedCopyScope", "allowSharedKeyAccess", "azureFilesIdentityBasedAuthentication", "customDomain", "defaultToOAuthAuthentication", "dnsEndpointType", "encryption", "immutableStorageWithVersioning", "isHnsEnabled", "isLocalUserEnabled", "isNfsV3Enabled", "isSftpEnabled", "keyPolicy", "largeFileSharesState", "minimumTlsVersion", "networkAcls", "publicNetworkAccess", "routingPreference", "sasPolicy", "supportsHttpsTrafficOnly"."""),
});
}

[TestMethod]
public void Resource_derived_typed_compile_time_imports_raise_diagnostic_when_imported_from_ARM_JSON_models_without_feature_flag_set()
{
var result = CompilationHelper.Compile(ServicesWithCompileTimeTypeImports,
("main.bicep", """
import {foo} from 'mod.json'

param location string = resourceGroup().location
param fooParam foo = {
bar: {
name: 'acct'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
}

output fooOutput foo = fooParam
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"definitions": {
"foo": {
"metadata": {
"{{LanguageConstants.MetadataExportedPropertyName}}": true
},
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
},
"resources": []
}
"""));

result.Should().HaveDiagnostics(new[]
{
("BCP385", DiagnosticLevel.Error, """Using resource-derived types requires enabling EXPERIMENTAL feature "ResourceDerivedTypes"."""),
});
}

[TestMethod]
public void Resource_derived_typed_compile_time_imports_raise_diagnostic_when_imported_from_ARM_JSON_models_with_unrecognized_resource()
{
var result = CompilationHelper.Compile(new ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true, CompileTimeImportsEnabled: true)),
("main.bicep", """
import {foo} from 'mod.json'

param location string = resourceGroup().location
param fooParam foo = {
bar: {
name: 'acct'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
}

output fooOutput foo = fooParam
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"definitions": {
"foo": {
"metadata": {
"{{LanguageConstants.MetadataExportedPropertyName}}": true
},
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Foo/bars@2022-09-01"
}
}
}
},
"resources": []
}
"""));

result.Should().HaveDiagnostics(new[]
{
("BCP081", DiagnosticLevel.Warning, """Resource type "Microsoft.Foo/bars@2022-09-01" does not have types available."""),
});
}
}
133 changes: 133 additions & 0 deletions src/Bicep.Core.IntegrationTests/UserDefinedTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,137 @@ public void Parsing_incomplete_tuple_type_expressions_halts()

result.Template.Should().BeNull();
}

[TestMethod]
public void Resource_derived_type_should_compile_successfully()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
"""
type myType = resource<'Microsoft.Storage/storageAccounts@2022-09-01'>
""");

result.Template.Should().HaveValueAtPath("definitions", JToken.Parse($$"""
{
"myType": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
"""));
}

[TestMethod]
public void Resource_derived_type_should_compile_successfully_with_namespace_qualified_syntax()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
"""
var resource = 'foo'
type myType = sys.resource<'Microsoft.Storage/storageAccounts@2022-09-01'>
""");

result.Template.Should().HaveValueAtPath("definitions", JToken.Parse($$"""
{
"myType": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
"""));
}

[TestMethod]
public void Param_with_resource_derived_type_can_be_loaded()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
("main.bicep", """
param location string = resourceGroup().location

module mod 'mod.json' = {
name: 'mod'
params: {
foo: {
bar: {
name: 'acct'
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
properties: {
unknownProperty: false
}
}
}
}
}
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"parameters": {
"foo": {
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
}
}
},
"resources": []
}
"""));

result.Should().NotHaveAnyCompilationBlockingDiagnostics();
result.Should().HaveDiagnostics(new[]
{
("BCP037", DiagnosticLevel.Warning, """The property "unknownProperty" is not allowed on objects of type "StorageAccountPropertiesCreateParametersOrStorageAccountProperties". Permissible properties include "accessTier", "allowBlobPublicAccess", "allowCrossTenantReplication", "allowedCopyScope", "allowSharedKeyAccess", "azureFilesIdentityBasedAuthentication", "customDomain", "defaultToOAuthAuthentication", "dnsEndpointType", "encryption", "immutableStorageWithVersioning", "isHnsEnabled", "isLocalUserEnabled", "isNfsV3Enabled", "isSftpEnabled", "keyPolicy", "largeFileSharesState", "minimumTlsVersion", "networkAcls", "publicNetworkAccess", "routingPreference", "sasPolicy", "supportsHttpsTrafficOnly"."""),
});
}

[TestMethod]
public void Output_with_resource_derived_type_can_be_loaded()
{
var result = CompilationHelper.Compile(new UnitTests.ServiceBuilder().WithFeatureOverrides(new(TestContext, ResourceDerivedTypesEnabled: true)),
("main.bicep", """
module mod 'mod.json' = {
name: 'mod'
}

output out string = mod.outputs.foo.bar.properties.unknownProperty
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"outputs": {
"foo": {
"type": "object",
"additionalProperties": {
"type": "object",
"metadata": {
"{{LanguageConstants.MetadataResourceDerivedTypePropertyName}}": "Microsoft.Storage/storageAccounts@2022-09-01"
}
},
"value": {}
}
},
"resources": []
}
"""));

result.Should().NotHaveAnyCompilationBlockingDiagnostics();
result.Should().HaveDiagnostics(new[]
{
("BCP053", DiagnosticLevel.Warning, """The type "StorageAccountPropertiesCreateParametersOrStorageAccountProperties" does not contain property "unknownProperty". Available properties include "accessTier", "allowBlobPublicAccess", "allowCrossTenantReplication", "allowedCopyScope", "allowSharedKeyAccess", "azureFilesIdentityBasedAuthentication", "blobRestoreStatus", "creationTime", "customDomain", "defaultToOAuthAuthentication", "dnsEndpointType", "encryption", "failoverInProgress", "geoReplicationStats", "immutableStorageWithVersioning", "isHnsEnabled", "isLocalUserEnabled", "isNfsV3Enabled", "isSftpEnabled", "keyCreationTime", "keyPolicy", "largeFileSharesState", "lastGeoFailoverTime", "minimumTlsVersion", "networkAcls", "primaryEndpoints", "primaryLocation", "privateEndpointConnections", "provisioningState", "publicNetworkAccess", "routingPreference", "sasPolicy", "secondaryEndpoints", "secondaryLocation", "statusOfPrimary", "statusOfSecondary", "storageAccountSkuConversionStatus", "supportsHttpsTrafficOnly"."""),
});
}
}
2 changes: 2 additions & 0 deletions src/Bicep.Core.Samples/DataSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static class DataSets

public static DataSet ResourcesManagementGroup_CRLF => CreateDataSet();

public static DataSet ResourceDerivedTypes_LF => CreateDataSet();

public static DataSet ResourcesTenant_CRLF => CreateDataSet();

public static DataSet TypeDeclarations_LF => CreateDataSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ var lt = 4 < 's'
//@[09:0016) | └─BinaryOperationSyntax
//@[09:0010) | ├─IntegerLiteralSyntax
//@[09:0010) | | └─Token(Integer) |4|
//@[11:0012) | ├─Token(LessThan) |<|
//@[11:0012) | ├─Token(LeftChevron) |<|
//@[13:0016) | └─StringSyntax
//@[13:0016) | └─Token(StringComplete) |'s'|
//@[16:0017) ├─Token(NewLine) |\n|
Expand All @@ -544,7 +544,7 @@ var gt = false>[
//@[09:0018) | └─BinaryOperationSyntax
//@[09:0014) | ├─BooleanLiteralSyntax
//@[09:0014) | | └─Token(FalseKeyword) |false|
//@[14:0015) | ├─Token(GreaterThan) |>|
//@[14:0015) | ├─Token(RightChevron) |>|
//@[15:0018) | └─ArraySyntax
//@[15:0016) | ├─Token(LeftSquare) |[|
//@[16:0017) | ├─Token(NewLine) |\n|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ var lt = 4 < 's'
//@[04:06) Identifier |lt|
//@[07:08) Assignment |=|
//@[09:10) Integer |4|
//@[11:12) LessThan |<|
//@[11:12) LeftChevron |<|
//@[13:16) StringComplete |'s'|
//@[16:17) NewLine |\n|
var lteq = null <= 10
Expand All @@ -359,7 +359,7 @@ var gt = false>[
//@[04:06) Identifier |gt|
//@[07:08) Assignment |=|
//@[09:14) FalseKeyword |false|
//@[14:15) GreaterThan |>|
//@[14:15) RightChevron |>|
//@[15:16) LeftSquare |[|
//@[16:17) NewLine |\n|
]
Expand Down
Loading
Loading