-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Parameter.cs
156 lines (138 loc) · 5.61 KB
/
Parameter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Azure.Provisioning
{
/// <summary>
/// Represents a parameter of an <see cref="IConstruct"/>.
/// </summary>
#pragma warning disable AZC0012 // Avoid single word type names
public readonly struct Parameter
#pragma warning restore AZC0012 // Avoid single word type names
{
/// <summary>
/// Gets the name of the parameter.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the description of the parameter.
/// </summary>
public string? Description { get; }
/// <summary>
/// Gets the default value of the parameter.
/// </summary>
public object? DefaultValue { get; }
/// <summary>
/// Gets a value indicating whether the parameter is secure.
/// </summary>
public bool IsSecure { get; }
/// <summary>
/// Gets the type of the parameter.
/// </summary>
public BicepType ParameterType { get; }
/// <summary>
/// Gets a value indicating whether the parameter is an expression.
/// </summary>
internal bool IsExpression { get; }
/// <summary>
/// Whether the parameter was constructed from an Output.
/// </summary>
public bool IsFromOutput => Output != null;
internal bool IsLiteral => Output?.IsLiteral ?? false;
/// <summary>
/// The value of the parameter.
/// </summary>
public string? Value { get; }
/// <summary>
/// The source of the parameter.
/// </summary>
public IConstruct? Source { get; }
internal Output? Output { get; }
/// <summary>
///
/// </summary>
/// <param name="output"></param>
public Parameter(Output output)
{
Name = output.Name;
IsSecure = output.IsSecure;
Value = output.Value;
Source = output.Source;
ParameterType = output.OutputType;
Output = output;
}
internal Parameter(string name, string? description, object? defaultValue, bool isSecure, IConstruct source, string? value, Output? output)
{
Name = name;
Description = description;
DefaultValue = defaultValue;
IsSecure = isSecure;
Source = source;
Value = value;
Output = output;
}
/// <summary>
/// Initializes a new instance of the <see cref="Parameter"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="description">The parameter description.</param>
/// <param name="defaultValue">The parameter defaultValue.</param>
/// <param name="isSecure">Is the parameter secure.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public Parameter(string name, string? description, object? defaultValue, bool isSecure)
: this(name, BicepType.String, description, defaultValue, isSecure)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Parameter"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="parameterType">The parameter type.</param>
/// <param name="description">The parameter description.</param>
/// <param name="defaultValue">The parameter defaultValue.</param>
/// <param name="isSecure">Is the parameter secure.</param>
public Parameter(string name, BicepType parameterType = BicepType.String, string? description = default, object? defaultValue = default, bool isSecure = false)
{
Name = name;
ParameterType = parameterType;
Description = description;
DefaultValue = defaultValue;
IsSecure = isSecure;
}
/// <summary>
/// Initializes a new instance of the <see cref="Parameter"/>.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="description">The parameter description.</param>
/// <param name="defaultValue">The parameter defaultValue.</param>
/// <param name="isSecure">Is the parameter secure.</param>
/// <param name="isExpression">Is the parameter an expression.</param>
internal Parameter(string name, string? description, object? defaultValue = default, bool isSecure = false, bool isExpression = false)
: this(name, BicepType.String, description, defaultValue, isSecure)
{
IsExpression = isExpression;
}
internal string GetParameterString(IConstruct parentScope)
{
// If the parameter is not from an output, use the parameter name.
if (Output == null)
{
return Name;
}
// If the parameter is from an output that is not in the current scope, use the parameter name.
if (!parentScope.GetOutputs().Contains(Output))
{
return Name;
}
// If the parameter is an output from the current scope, use its Value.
if (ReferenceEquals(Output!.Resource.ModuleScope, parentScope))
{
return Value!;
}
// Otherwise it is an output from a different scope, use the full reference.
return $"{Output!.Resource.ModuleScope!.Name}.outputs.{Name}";
}
}
}