Skip to content

Support FormattableString as argument of BicepFunction.Interpolate #51419

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 10 commits into from
Aug 1, 2025

Conversation

ArcturusZhang
Copy link
Member

@ArcturusZhang ArcturusZhang commented Jul 23, 2025

Fixes #47360

Our current overloads only support the "literal interpolated strings", for instance:

BicepFunction.Interpolate($"Hello {world}");

this works fine now, but if we do this:

FormattableString fs = $"Hello {world}";
Bicep.Function.Interpolate(fs); // error

this would not compile.

This PR adds an implicit operator for the BicepInterpolatedStringHandler from FormattableString to make the second usage of above available.

Why we do not make another overload that takes the FormattableString directly?
Because if we do that, like this:

public static BicepExpression Interpolate(BicepInterpolatedStringHandler handler);
public static BicepExpression Interpolate(FormattableString fs);

and when we call this:

BicepFunction.Interpolate($"Hello world");

this would not compile complaining about "ambiguous call from the above two overloads".

Therefore the current approach I am taking is that I added an implicit operator, therefore when you are using FormattableString as argument, the compiler would call the cast operator first, and then follow the old way to give you the interpolated result.

Contributing to the Azure SDK

Please see our CONTRIBUTING.md if you are not familiar with contributing to this repository or have questions.

For specific information about pull request etiquette and best practices, see this section.

@ArcturusZhang ArcturusZhang changed the title Adding an overload of BicepFunction.Interpolate(FormattableString) Support FormattableString as argument of BicepFunction.Interpolate Jul 24, 2025
@ArcturusZhang ArcturusZhang marked this pull request as ready for review July 24, 2025 03:37
@Copilot Copilot AI review requested due to automatic review settings July 24, 2025 03:37
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for FormattableString as an argument to BicepFunction.Interpolate, addressing a user request to enable natural interpolated string syntax. The changes introduce parsing capabilities for FormattableString format strings and allow seamless conversion between FormattableString objects and Bicep interpolation expressions.

Key changes:

  • Adds a new FormattableStringHelpers utility class for parsing FormattableString format strings
  • Extends BicepStringBuilder and BicepInterpolatedStringHandler to handle FormattableString inputs
  • Adds comprehensive test coverage for the new FormattableString functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
FormattableStringHelpers.cs New utility class that provides parsing logic for FormattableString format strings
BicepStringBuilder.cs Adds FormattableString support with new overloads and conversion logic
BicepFunction.cs Minor documentation formatting improvement
BicepFunctionTests.cs Comprehensive test suite covering FormattableString interpolation scenarios
Comments suppressed due to low confidence (4)

sdk/provisioning/Azure.Provisioning/src/Utilities/FormattableStringHelpers.cs:10

  • [nitpick] The method name GetFormattableStringFormatParts contains redundant terminology. Consider renaming to GetFormatParts since the class name already indicates it's for FormattableString helpers.
        public static GetPathPartsEnumerator GetFormattableStringFormatParts(ReadOnlySpan<char> format) => new GetPathPartsEnumerator(format);

sdk/provisioning/Azure.Provisioning/src/Utilities/FormattableStringHelpers.cs:12

  • The struct name GetPathPartsEnumerator is misleading as it processes format parts, not path parts. Consider renaming to FormatPartsEnumerator.
        public ref struct GetPathPartsEnumerator

sdk/provisioning/Azure.Provisioning/src/Utilities/FormattableStringHelpers.cs:14

  • The field name _path is inconsistent with the actual purpose. This field holds the remaining format string, so it should be named _format or _remaining.
            private ReadOnlySpan<char> _path;

sdk/provisioning/Azure.Provisioning/src/Utilities/FormattableStringHelpers.cs:18

  • The parameter name format in the constructor is inconsistent with the field name _path. Consider renaming the field to _format to maintain consistency.
            {

Copy link

github-actions bot commented Jul 24, 2025

API Change Check

APIView identified API level changes in this PR and created the following API reviews

Azure.Provisioning

@ArcturusZhang ArcturusZhang merged commit 20e1f3a into Azure:main Aug 1, 2025
17 checks passed
@ArcturusZhang ArcturusZhang deleted the interpolate-enhancement branch August 1, 2025 03:13
github-merge-queue bot pushed a commit to microsoft/typespec that referenced this pull request Aug 4, 2025
I get some feedbacks here
Azure/azure-sdk-for-net#51419 (comment)
when I copy the code here to provisioning.
Therefore this PR backports the same changes resolving those comments
here.
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.

[Azure.Provisioning]: Add support for BicepFunction.Interpolate that takes a format string
2 participants