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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 2 additions & 6 deletions sdk/provisioning/Azure.Provisioning/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Release History

## 1.3.0-beta.1 (Unreleased)
## 1.3.0 (2025-08-01)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
- Supported `FormattableString` in `BicepFunction.Interpolate` method. ([#47360](https://github.com/Azure/azure-sdk-for-net/issues/47360))

## 1.2.1 (2025-07-09)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ public ref partial struct BicepInterpolatedStringHandler
public BicepInterpolatedStringHandler(int literalLength, int formattedCount) { throw null; }
public void AppendFormatted<T>(T t) { }
public void AppendLiteral(string text) { }
public static implicit operator Azure.Provisioning.Expressions.BicepInterpolatedStringHandler (System.FormattableString formattable) { throw null; }
}
public partial class BicepProgram
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ public ref partial struct BicepInterpolatedStringHandler
public BicepInterpolatedStringHandler(int literalLength, int formattedCount) { throw null; }
public void AppendFormatted<T>(T t) { }
public void AppendLiteral(string text) { }
public static implicit operator Azure.Provisioning.Expressions.BicepInterpolatedStringHandler (System.FormattableString formattable) { throw null; }
}
public partial class BicepProgram
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>Contains the core functionality for defining Azure infrastructure with dotnet code.</Description>
<Version>1.3.0-beta.1</Version>
<Version>1.3.0</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>1.2.1</ApiCompatVersion>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Azure.Core;
using Azure.Provisioning.Resources;
using Azure.Provisioning.Utilities;

namespace Azure.Provisioning.Expressions;

Expand Down Expand Up @@ -293,8 +294,7 @@ public static BicepValue<string> ToUpper(BicepValue<object> value) =>
/// Combines multiple string values <paramref name="values"/> and returns
/// the concatenated string. This represents the <c>concat</c> Bicep
/// function. To improve readability, prefer
/// <see cref="BicepFunction.Interpolate"/> instead of
/// <see cref="Concat"/>.
/// <see cref="Interpolate"/> instead of <see cref="Concat"/>.
/// </summary>
/// <param name="values">Strings in sequential order for concatenation.</param>
/// <returns>A string or array of concatenated values.</returns>
Expand All @@ -310,11 +310,17 @@ public static BicepValue<string> Concat(params BicepValue<string>[] values)
}

/// <summary>
/// Convert a formattable string with literal text, C# expressions, and
/// Bicep expressions into an interpolated Bicep string.
/// Builds a Bicep interpolated string expression from C# interpolated string syntax
/// or a <see cref="FormattableString"/> instance.
/// Use this method to combine literal text, C# expressions and Bicep expressions
/// into a single Bicep string value.
/// </summary>
/// <param name="handler">A bicep interpolated string handler.</param>
/// <returns>An interpolated string.</returns>
/// <param name="handler">
/// The <see cref="BicepInterpolatedStringHandler"/> that collects literal and formatted segments from the interpolated string.
/// </param>
/// <returns>
/// A <see cref="BicepValue{String}"/> representing the constructed Bicep interpolated string expression of type <c>string</c>.
/// </returns>
public static BicepValue<string> Interpolate(BicepInterpolatedStringHandler handler) =>
handler.Build();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Azure.Provisioning.Utilities;

namespace Azure.Provisioning.Expressions;

Expand Down Expand Up @@ -108,6 +110,10 @@ public void AppendFormatted<T>(T t)
{
_expressions.Add(exp);
}
else if (t is FormattableString formattable)
{
AppendFormattableString(formattable);
}
else
{
string? s = t?.ToString();
Expand All @@ -118,10 +124,34 @@ public void AppendFormatted<T>(T t)
}
}

internal void AppendFormattableString(FormattableString value)
{
var formatSpan = value.Format.AsSpan();
foreach (var (span, isLiteral, index) in FormattableStringHelpers.GetFormattableStringFormatParts(formatSpan))
{
if (isLiteral)
{
AppendLiteral(span.ToString());
}
else
{
// this is not a literal therefore an argument
AppendFormatted(value.GetArgument(index));
}
}
}

internal readonly BicepValue<string> Build()
{
BicepValue<string> value = new(new InterpolatedStringExpression([.. _expressions]));
value._isSecure = _isSecure;
return value;
}

public static implicit operator BicepInterpolatedStringHandler(FormattableString formattable)
{
var handler = new BicepInterpolatedStringHandler(formattable.Format.Length, formattable.ArgumentCount);
handler.AppendFormattableString(formattable);
return handler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.Provisioning.Utilities
{
internal static class FormattableStringHelpers
{
/// <summary>
/// Parses the format from a <see cref="FormattableString"/> and returns an enumerator
/// that yields the parts of the format.
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
public static GetFormatPartsEnumerator GetFormattableStringFormatParts(ReadOnlySpan<char> format) => new GetFormatPartsEnumerator(format);

public ref struct GetFormatPartsEnumerator
{
private ReadOnlySpan<char> _format;
public Part Current { get; private set; }

public GetFormatPartsEnumerator(ReadOnlySpan<char> format)
{
_format = format;
Current = default;
}

public readonly GetFormatPartsEnumerator GetEnumerator() => this;

public bool MoveNext()
{
if (_format.Length == 0)
{
return false;
}

var separatorIndex = _format.IndexOfAny('{', '}');

if (separatorIndex == -1)
{
Current = new Part(_format, true);
_format = ReadOnlySpan<char>.Empty;
return true;
}

var separator = _format[separatorIndex];
// Handle {{ and }} escape sequences
if (separatorIndex + 1 < _format.Length && _format[separatorIndex + 1] == separator)
{
Current = new Part(_format.Slice(0, separatorIndex + 1), true);
_format = _format.Slice(separatorIndex + 2);
return true;
}

var isLiteral = separator == '{';

// Skip empty literals
if (isLiteral && separatorIndex == 0 && _format.Length > 1)
{
separatorIndex = _format.IndexOf('}');
if (separatorIndex == -1)
{
Current = new Part(_format.Slice(1), true);
_format = ReadOnlySpan<char>.Empty;
return true;
}

Current = new Part(_format.Slice(1, separatorIndex - 1), false);
}
else
{
Current = new Part(_format.Slice(0, separatorIndex), isLiteral);
}

_format = _format.Slice(separatorIndex + 1);
return true;
}

internal readonly ref struct Part
{
public Part(ReadOnlySpan<char> span, bool isLiteral)
{
Span = span;
IsLiteral = isLiteral;
}

public ReadOnlySpan<char> Span { get; }
public bool IsLiteral { get; }

public void Deconstruct(out ReadOnlySpan<char> span, out bool isLiteral, out int argumentIndex)
{
span = Span;
isLiteral = IsLiteral;

if (IsLiteral)
{
argumentIndex = -1;
}
else
{
var formatSeparatorIndex = span.IndexOf(':');
var indexSpan = formatSeparatorIndex == -1 ? span : span.Slice(0, formatSeparatorIndex);
#if NET8_0_OR_GREATER
argumentIndex = int.Parse(indexSpan);
#else
argumentIndex = int.Parse(indexSpan.ToString());
#endif
}
}
}
}
}
}
Loading