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

Switch support #448

Open
slavizh opened this issue Sep 4, 2020 · 6 comments
Open

Switch support #448

slavizh opened this issue Sep 4, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@slavizh
Copy link
Contributor

slavizh commented Sep 4, 2020

Is your feature request related to a problem? Please describe.
Provide option for Switch support. In cases where we have more then two choices we need switch support. I do not think Ternary operator can do that. In ARM templates you can solve that in some ways like having variable objects like

variables: { 
  "mySwitch": {
    "first": 1,
    "second": 2,
    "third": 3
   }
}

and of course when you need to use it you do something like:

 "someProperty": "[parameters('InputParameter')[string(variables('mySwitch'))]]"

You could basically have switch function and simplify that approach in Bicep. Downside is that I do not see a way of default value or more advanced ways of working like in PowerShell.

@slavizh slavizh added the enhancement New feature or request label Sep 4, 2020
@ghost ghost added the Needs: Triage 🔍 label Sep 4, 2020
@anthony-c-martin
Copy link
Member

Note that in Bicep the equivalent should work:

param switchVal string

var myVar = {
  first: 'name1'
  second: 'name2'
  third: 'name3'
}

var chosenName = myVar[switchVal]

We should also soon be able to go even further and use the type system to validate that e.g. we've got a mismatch between param declaration and usage:

param switchVal string {
  allowed: [
    'first'
    'second'
    'third'
    'fourth' // show a warning or error because the "switch" statement doesn't support 'fourth'
  ]
}

@slavizh
Copy link
Contributor Author

slavizh commented Sep 8, 2020

@anthony-c-martin Yes it will work but the point was to have easier to use syntax by novice people who may come from different languages background.

@ChristopherGLewis
Copy link
Contributor

This is where I have issues the "json-like" syntax.

When you look at the var myVar, in bicep you have the "no comma" object

param switchVal string
var myVar = {
  first: 'name1'
  second: 'name2'
  third: 'name3'
}
var chosenName = myVar[switchVal]
output return string = chosenName

However, if we switch this to a parameter, the parameter becomes an object and passing this requires real JSON:

Bicep

param switchVal string
param myParam object
var chosenName = myParam[switchVal]
output return string = chosenName

PowerShell

$json = '{"third":"name3","first":"name1","second":"name2"}'
$hash = $json | ConvertFrom-Json
$param = @{
     myParam = $Hash
}
New-AzResourceGroupDeployment -Name testDeploy -ResourceGroupName BicepTesting `
     -TemplateFile .\switchTest2.json -TemplateParameterObject $param

CLI

az deployment group create --resource-group bicepTesting \
    --template-file switchTest2.json \
    --parameters myParam='{"third":"name3","first":"name1","second":"name2"}'

I realize this is a parameter vs. template issue, but I feel it would be a much nicer development experience overall if objects and arrays supported pure JSON syntax.

@thesushil
Copy link

What is the bicep equivalent of default case of real switch?

@alex-frankel
Copy link
Collaborator

There is no switch expression so there is no equivalent of a default on a switch. The only conditional logic on properties is the ternary operator:

var trueThing = true

output isItTrue string = trueThing ? 'It is true' : 'If it is not, I will be emitted by default'

In some sense, the third argument of the ternary operator is the closes that exists to default. You can also chain ternary expressions together, but that would be very finnicky code to maintain.

@aytimothy
Copy link

aytimothy commented Apr 17, 2023

In some sense, the third argument of the ternary operator is the closes that exists to default. You can also chain ternary expressions together, but that would be very finnicky code to maintain.

I think that's the point of having a if-ifelse-else (#1171) or switch-like operator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants