Decorator syntax
Overview
ARM copy loops support a mode (serial or parallel) as well as a batchSize parameter to control how looping is done. We seem to have agreement that we should use decorators to express a modification of the loop behavior, but we have not formally agreed on the syntax for them.
Decorators are coming up in other places as well (parameters and outputs), but I put those areas of the language aside in this proposal.
Mode
Option 1
We can set serial mode on a resource loop as follows:
@serial
resource myAccounts 'Microsoft.Storage/storageAccounts?2020-01-01' = [for account in accounts: {
location: account.location
name: account.name,
...
}]
Parallel mode is the default, but it could be optionally expressed using @parallel instead of @serial in the example above.
Option 2
Instead of two different decorators, we could use one @mode decorators like these:
@mode('serial')@mode('parallel')
Batch size
Similar to loop mode, batch size could be expressed with something like @batchSize(2)
Combining decorators
Option 1
We could allow one decorator per line like this:
@parallel
@batchSize(42)
Option 2
We could allow combining decorators on a single line using commas:
@parallel, @batchSize(42)
Have we decided to go with decorators? Anders still seems hesitant on it.
Anders is hesitant, but accepts that it may be the least bad option :)
The other place we have a modifier-like syntax would be parameters. Putting a couple examples here for reference:
@minValue(3)
@maxValue(24)
@description("name of the storage account")
parameter storageAccountName int
@minValue(3), @maxValue(24), @description("name of the storage account")
parameter storageAccountName int
alternative with less @'s
@{minValue(3), maxValue(24), description("name of the storage account")}
parameter storageAccountName int
alternative with less @'s
@{minValue(3), maxValue(24), description("name of the storage account")} parameter storageAccountName int
I like the idea of decorator block to avoid too many @. Maybe support both syntax? @minValue(3) for single decorator, @{multiple decorators}
Yup, I like the multi-decorator syntax too.
There's also an interesting argument for combining some of the constraints into a single decorator like @length(3,24)
We might need a restriction on complex values in decorators (objects/arrays) with all the syntaxes above. Otherwise, we'll be in curly brace hell again.
good point - allowedValues will be a challenge I think...
@allowedValues([
'small'
'medium'
'large'
])
parameter size string
FWIW you don't need commas between multiple decorators (assuming they are called with parens).
Perhaps a pipe dream but it would be great if users could compose a length decorator from a provided minLenth and maxLength decorator. This composition could be used to encapsulate common parameter types users have in their problem domain.
@alex-frankel Yeah good pt. allowedValues makes that unavoidable. Also a single allowed value can actually be an object 😈 - ARM does JToken.DeepEquals() to compare.
@bterlson Are you thinking the comma-less group would be in that block syntax above or like this?
@length(1,4) @description('I am so descriptive')
Composing user-defined decorators could definitely be interesting once there are more opportunities for decorating things. Currently agreed upon syntax limits what you could do with them (tweak a loop, tweak a parameter and that's basically it), but I suspect the list where they fit will grow.
Interesting - it feels like we should flag a single allowedValue as an error or warning since it should just be a variable or hardcoded value at that point, right?
Sorry my statement was ambiguous. I meant that in an array of allowed values, a single array item can be an object. (Or another array. Or anything really.)
The type checking we will do here will check that allowedValues is an array and that the type of each item in the array is assignable to the parameter type. We could check the length of the array. 0 would definitely be an error, but 1 is a warning at best. If the user is planning to reserve a parameter and add more allowed values in the future, they could very well just specify one for now. Yes, they could achieve the same by adding the parameter later with a default value, but I don't want to choose that path for them.
Ah, gotcha. I agree with the logic that it should not be an error, and I can see why we may want to hold off from the warning as well. I've also seen text in a darker/faded out color for things like "unused" params and variables, maybe that would be better.
More supporting evidence to making this change: #293
Ah, gotcha. I agree with the logic that it should not be an error, and I can see why we may want to hold off from the warning as well. I've also seen text in a darker/faded out color for things like "unused" params and variables, maybe that would be better.
FYI, the easiest way to do that in vscode is to add the vscode.DiagnosticTag.Unnecessary tag to the warning diagnostic, but if you don't want to add warnings, you could also do it via a text decorator. I'd also suggest you use tslint's model for disabling warnings.
Another potential use-case - semantic documentation which can show up on hovers would be pretty neat (especially when working with modules) - e.g. something like the following if you just pretend my comments are actually useful:
@doc 'The prefix to use for the diagnostics storage account'
param diagsAccountPrefix string
@doc 'The diagnostics storage account name'
var diagsAccountName = '${diagsAccountPrefix}${uniqueString(resourceGroup().id)}'
@doc 'The diagnostics storage account'
resource diagsAccount 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: diagsAccountName
...
}
Yeah, being able to add descriptions and other text that shows up in hovers would be amazing.
Our team had a discussion and we decided to implement the multi-line decorator syntax for 0.3:
@minValue(3)
@maxValue(24)
@description("name of the storage account")
parameter storageAccountName int
Once we make our language less new line sensitive (https://github.com/Azure/bicep/issues/146), we'll need to revise the syntax for putting multiple decorators on the same line.
The @doc decorator for semantic documentation is definitely something we would like to have as well, but it's a separate feature which will come after the 0.3 release.
@alex-frankel I liked the multi line syntax. It's readable, close to decorators concept in c#, so it will be natural to developers.
Maybe to add though will this also support a decorator for reference if we use the decorator @secure or would the reference be an optional param of the @secure decorator?
@lr90 are you referring to referencing a keyvault secret? This decorator syntax is for the definition of a parameter, not for providing the value of it. So you will still need to reference using the parameters file. Separately, there is a gap for referencing secrets that we will take care of with https://github.com/Azure/bicep/issues/1028#issuecomment-742630201
I think the @secure decorator will not take any arguments
Yes I was, taken a look a #1028 and understand now. Thanks
I also like the multi-line syntax. Seems like a good breaking change to include.