Skip to content

Commit 8a545f2

Browse files
authored
Add attribute to handle deserialization of constructor parameters with name overrides (#231)
* Add attribute to handle deserialization of constructor parameters with name overrides * changie new * Update names * Add a test. Address PR feedback. * Relocate new attribute
1 parent 125d467 commit 8a545f2

File tree

6 files changed

+86
-11
lines changed

6 files changed

+86
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
component: sdk
2+
kind: Improvements
3+
body: Add attribute to handle deserialization of constructor parameters with name
4+
overrides
5+
time: 2024-02-17T07:53:37.629486995-08:00
6+
custom:
7+
PR: "231"

CONTRIBUTING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This repo is made up of three main components. The host runtime (pulumi-language
55
## Changelog
66

77
Changelog management is done via [`changie`](https://changie.dev/).
8+
See the [installation](https://changie.dev/guide/installation/) guide for `changie`.
89

910
Run `changie new` in the top level directory. Here is an example of what that looks like:
1011

@@ -25,4 +26,4 @@ $ changie batch auto
2526
$ changie merge
2627
$ git add .
2728
$ git commit -m "Changelog for $(changie latest)"
28-
```
29+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016-2024, Pulumi Corporation
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.Immutable;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Pulumi.Serialization;
9+
using Xunit;
10+
11+
namespace Pulumi.Tests.Serialization
12+
{
13+
public class ConstructorParamAttributeTests : ConverterTests
14+
{
15+
[OutputType]
16+
public class MyResource
17+
{
18+
public readonly string StringProp;
19+
20+
[OutputConstructor]
21+
public MyResource([OutputConstructorParameter("string_prop")] string stringProp)
22+
{
23+
StringProp = stringProp;
24+
}
25+
}
26+
27+
[Fact]
28+
public async Task TestOutputConstructorParameter()
29+
{
30+
var warnings = new List<string>();
31+
32+
var data = Converter.ConvertValue<MyResource>(warnings.Add, "", await SerializeToValueAsync(new Dictionary<string, object>
33+
{
34+
{ "string_prop", "somevalue" },
35+
}));
36+
37+
Assert.Equal("somevalue", data.Value.StringProp);
38+
}
39+
}
40+
}

sdk/Pulumi/Pulumi.xml

+15-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/Pulumi/Serialization/Attributes.cs

+15
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ public sealed class OutputConstructorAttribute : Attribute
7777
{
7878
}
7979

80+
/// <summary>
81+
/// Attribute used by a Pulumi Cloud Provider Package to mark
82+
/// constructor parameters with a name override.
83+
/// </summary>
84+
[AttributeUsage(AttributeTargets.Parameter)]
85+
public sealed class OutputConstructorParameterAttribute : Attribute
86+
{
87+
public string Name { get; }
88+
89+
public OutputConstructorParameterAttribute(string name)
90+
{
91+
Name = name;
92+
}
93+
}
94+
8095
/// <summary>
8196
/// Attribute used by a Pulumi Cloud Provider Package to mark enum types.
8297
///

sdk/Pulumi/Serialization/Converter.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ private static (object?, string?) TryConvertObject(Action<string> warn, string c
200200
for (int i = 0, n = constructorParameters.Length; i < n; i++)
201201
{
202202
var parameter = constructorParameters[i];
203+
var parameterName = parameter.Name!;
204+
var attribute = parameter.GetCustomAttribute<OutputConstructorParameterAttribute>();
205+
if (attribute != null)
206+
{
207+
parameterName = attribute.Name;
208+
}
203209

204210
// Note: TryGetValue may not find a value here. That can happen for things like
205211
// unknown vals. That's ok. We'll pass that through to 'Convert' and will get the
206212
// default value needed for the parameter type.
207-
dictionary!.TryGetValue(parameter.Name!, out var argValue);
213+
dictionary!.TryGetValue(parameterName, out var argValue);
208214

209215
arguments[i] = ConvertObject(warn, $"{targetType.FullName}({parameter.Name})", argValue, parameter.ParameterType);
210216
}

0 commit comments

Comments
 (0)