Skip to content

Add format() experimental function #779

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

Merged
merged 5 commits into from
May 6, 2025

Conversation

SteveL-MSFT
Copy link
Member

PR Summary

Add experimental format() function. Use of this function will emit a warning as it doesn't currently support all the same format options as dotnet which is what ARM is based on.

Also updated versions of some crates.

PR Context

Fix #767

@Gijsreyn
Copy link
Contributor

Gijsreyn commented May 3, 2025

Just a question aside Steve, if this is experimental, should users toggle it on or off in the dsc.settings.json?

@SteveL-MSFT
Copy link
Member Author

Just a question aside Steve, if this is experimental, should users toggle it on or off in the dsc.settings.json?

Good question. I don't think we need that since it's something you have to explicitly use.

Copy link
Member

@andyleejordan andyleejordan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First basic round of testing is successful:

Logs:

2025-05-05T22:58:42.459120Z DEBUG dsc_lib::configure: 773: Invoke property expressions
2025-05-05T22:58:42.460693Z DEBUG dsc_lib::parser: 48: Parsing statement: [format('{0}\Themes\Personalize', variables('keyPathPrefix'))]
2025-05-05T22:58:42.470561Z DEBUG dsc_lib::parser: 84: Parsing expression
2025-05-05T22:58:42.471435Z DEBUG dsc_lib::parser::expressions: 43: Parsing function '{Node function (0, 1) - (0, 61)}'
2025-05-05T22:58:42.472362Z DEBUG dsc_lib::parser::expressions: 43: Parsing function '{Node function (0, 34) - (0, 60)}'
2025-05-05T22:58:42.473285Z DEBUG dsc_lib::parser::functions: 57: Function name: 'variables'
2025-05-05T22:58:42.474161Z DEBUG dsc_lib::parser::functions: 57: Function name: 'format'
2025-05-05T22:58:42.475071Z DEBUG dsc_lib::parser::functions: 80: Argument is a value: 'String("{0}\\Themes\\Personalize")'
2025-05-05T22:58:42.477017Z DEBUG dsc_lib::parser::functions: 75: Argument is an expression
2025-05-05T22:58:42.478129Z DEBUG dsc_lib::parser::functions: 80: Argument is a value: 'String("keyPathPrefix")'
2025-05-05T22:58:42.479103Z DEBUG dsc_lib::functions::variables: 28: variables function
2025-05-05T22:58:42.480001Z  WARN dsc_lib::functions::format: 99: `format()` function is experimental
2

Bicep:

var keyPathPrefix = 'HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion'

resource systemUsesLightTheme 'Microsoft.Windows/Registry@2025-04-07' = {
    name: 'SystemUsesLightTheme'
    properties: {
        keyPath: '${keyPathPrefix}\\Themes\\Personalize'
        valueName: 'SystemUsesLightTheme'
        valueData: {
          DWord: valueData
        }
    }
}

ARM:

  "variables": {
    "valueData": "[if(equals(parameters('colorMode'), 'light'), 1, 0)]",
    "keyPathPrefix": "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion"
  },
  "resources": [
    {
      "type": "Microsoft.Windows/Registry",
      "apiVersion": "2025-04-07",
      "name": "SystemUsesLightTheme",
      "properties": {
        "keyPath": "[format('{0}\\Themes\\Personalize', variables('keyPathPrefix'))]",
        "valueName": "SystemUsesLightTheme",
        "valueData": {
          "DWord": "[variables('valueData')]"
        }
      }
    },

And this worked such that I didn't have to use Bicep's concat() any more (which throws a warning to use string interpolation). Would be happy to test some more, at least before release, but I liked the ample unit tests. Thing we'll have to worry about in general is bug-for-bug compatibility between Bicep (implemented in C#) and DSC (in Rust).

linked-hash-map = "0.5"
num-traits = "0.2"
regex = "1.11"
rt-format = "0.3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package seems reasonable enough, but I'll point out that it's last release was 3 years ago, and it has this note:

Is stable API a must-have? If so, you might consider the alternatives. This crate is still not at version 1.0, which means that the API is still not completely stable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just something to consider, dyn-fmt appears more recently updated than the other two contenders (rt-format as above and the similarly named dynfmt last published 4 years ago).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm not saying it's better, I'm just looking into it.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the variety of options listed in the rt-format repository. Maybe will switch to dyn-fmt in the future. Since this is experimental, we can leave it as-is for now.

@SteveL-MSFT SteveL-MSFT merged commit a83c425 into PowerShell:main May 6, 2025
4 checks passed
@SteveL-MSFT SteveL-MSFT deleted the format-function branch May 6, 2025 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bicep compat functions
3 participants