Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
23 changes: 21 additions & 2 deletions sdk/provisioning/Azure.Provisioning/src/BicepDictionaryOfT.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -75,8 +76,26 @@ public static implicit operator BicepDictionary<T>(ProvisioningVariable referenc
/// <returns>The value.</returns>
public BicepValue<T> this[string key]
{
get => _values[key];
set => _values[key] = value;
get
{
if (_isOutput)
{
return BicepSyntax.Index(_self!.GetReference(), BicepSyntax.Value(key));
}
else
{
return _values[key];
}
}

set
{
if (_kind == BicepValueKind.Expression || _isOutput)
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Clarify the behavior in the set accessor by documenting that assignment is disallowed for output properties and when the value kind is an expression, which would help maintain the intended immutability.

Copilot uses AI. Check for mistakes.
{
throw new InvalidOperationException($"Cannot assign to {_self?.PropertyName}");
}
_values[key] = value;
}
}

public void Add(string key, BicepValue<T> value) => _values.Add(key, value);
Expand Down
4 changes: 4 additions & 0 deletions sdk/provisioning/Azure.Provisioning/src/BicepListOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public BicepValue<T> this[int index]
// can reference their members.
return _referenceFactory!(BicepSyntax.Index(_expression!, BicepSyntax.Value(index)));
}
else if (_isOutput)
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Ensure that _self is never null when _isOutput is true to prevent a potential NullReferenceException; consider adding documentation or an explicit null check.

Copilot uses AI. Check for mistakes.
{
return BicepSyntax.Index(_self!.GetReference(), BicepSyntax.Value(index));
}
else
{
return _values[index];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Azure.Provisioning.Primitives;
using NUnit.Framework;

namespace Azure.Provisioning.Tests.BicepValues;
Expand Down Expand Up @@ -39,10 +42,136 @@ public void ValidateLiteralBicepValue()
AssertExpression("-2147483647", new BicepValue<double>(-2147483647d));
AssertExpression("-2147483648", new BicepValue<double>(-2147483648d));
AssertExpression("json('-2147483649')", new BicepValue<double>(-2147483649d));
}

static void AssertExpression(string expected, BicepValue bicepValue)
[Test]
public void ValidateOutputArray()
{
var resource = new TestConstruct("test");
var expression = resource.Properties.Ports[0];
AssertExpression(
"test.properties.ports[0]",
expression
);
}

[Test]
public void ValidateOutputDictionary()
{
var resource = new TestConstruct("test")
{
Properties = new()
};
var expression = resource.Properties.Endpoints["reference"];
AssertExpression(
"test.properties.endpoints['reference']",
expression
);
}

[Test]
public void ValidateInputArray()
{
var resource = new TestConstruct("test")
{
Properties = new()
{
IpAddresses = ["192.168.1.1"]
}
};
var expression = resource.Properties.IpAddresses[0];
AssertExpression(
"'192.168.1.1'",
expression
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
Assert.AreEqual(expected, bicepValue.ToString());
var e = resource.Properties.IpAddresses[1];
});
}

[Test]
public void ValidateInputDictionary()
{
var resource = new TestConstruct("test")
{
Properties = new()
{
Tags = new()
{
["foo"] = "bar"
}
}
};
var expression = resource.Properties.Tags["foo"];
AssertExpression(
"'bar'",
expression
);
Assert.Throws<KeyNotFoundException>(() =>
{
var t = resource.Properties.Tags["bar"];
});
}

private class TestConstruct : ProvisionableResource
{
protected override void DefineProvisionableProperties()
{
_properties = DefineModelProperty<TestProperties>("Properties", ["properties"]);
}

public TestProperties Properties
{
get { Initialize(); return _properties!; }
set { Initialize(); AssignOrReplace(ref _properties, value); }
}
private TestProperties? _properties;

public TestConstruct(string bicepIdentifier, string? resourceVersion = null) : base(bicepIdentifier, "Microsoft.Tests/testResource", resourceVersion ?? "2025-05-27")
{
}
}

private class TestProperties : ProvisionableConstruct
{
protected override void DefineProvisionableProperties()
{
_ipAddresses = DefineListProperty<string>("IpAddresses", ["ipAddresses"]);
_tags = DefineDictionaryProperty<string>("Tags", ["tags"]);
_ports = DefineListProperty<string>("Ports", ["ports"], isOutput: true);
_endpoints = DefineDictionaryProperty<string>("Endpoints", ["endpoints"], isOutput: true);
}

public BicepList<string> IpAddresses
{
get { Initialize(); return _ipAddresses!; }
set { Initialize(); AssignOrReplace(ref _ipAddresses, value); }
}
private BicepList<string>? _ipAddresses;

public BicepDictionary<string> Tags
{
get { Initialize(); return _tags!; }
set { Initialize(); AssignOrReplace(ref _tags, value); }
}
private BicepDictionary<string>? _tags;

public BicepList<string> Ports
{
get { Initialize(); return _ports!; }
}
private BicepList<string>? _ports;

public BicepDictionary<string> Endpoints
{
get { Initialize(); return _endpoints!; }
}
private BicepDictionary<string>? _endpoints;
}

private static void AssertExpression(string expected, BicepValue bicepValue)
{
Assert.AreEqual(expected, bicepValue.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Azure.Provisioning.Expressions;
using Azure.Provisioning.Primitives;
using NUnit.Framework;

namespace Azure.Provisioning.Tests.Expressions
Expand Down