-
Notifications
You must be signed in to change notification settings - Fork 368
/
InputDataSet.cs
92 lines (75 loc) · 3.85 KB
/
InputDataSet.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Parameters;
namespace Microsoft.TemplateEngine.Edge.Template;
/// <summary>
/// Data model for passing data into the <see cref="TemplateCreator"/>.
/// </summary>
public class InputDataSet : IReadOnlyDictionary<ITemplateParameter, InputParameterData>
{
private readonly IReadOnlyDictionary<ITemplateParameter, InputParameterData> _parametersData;
/// <summary>
/// Creates new instance of the <see cref="InputDataSet"/> type.
/// </summary>
/// <param name="parameters"></param>
/// <param name="parameterData"></param>
public InputDataSet(IParameterDefinitionSet parameters, IReadOnlyList<InputParameterData> parameterData)
{
_parametersData = parameterData.ToDictionary(d => d.ParameterDefinition, d => d);
ParameterDefinitionSet = new ParameterDefinitionSet(parameters.AsReadonlyDictionary());
}
/// <summary>
/// Creates new instance of the <see cref="InputDataSet"/> type.
/// To be used to convert legacy parameters dictionaries into this data model.
/// </summary>
/// <param name="templateInfo"></param>
public InputDataSet(ITemplateInfo templateInfo)
: this(templateInfo, (IReadOnlyDictionary<string, object?>?)null)
{ }
/// <summary>
/// Creates new instance of the <see cref="InputDataSet"/> type.
/// To be used to convert legacy parameters dictionaries into this data model.
/// </summary>
/// <param name="templateInfo"></param>
/// <param name="inputParameters"></param>
public InputDataSet(ITemplateInfo templateInfo, IReadOnlyDictionary<string, string?> inputParameters)
: this(templateInfo, inputParameters.ToDictionary(p => p.Key, p => (object?)p.Value))
{ }
private InputDataSet(ITemplateInfo templateInfo, IReadOnlyDictionary<string, object?>? inputParameters)
{
_parametersData = templateInfo.ParameterDefinitions.ToDictionary(p => p, p =>
{
object? value = null;
bool isSet = inputParameters != null && inputParameters.TryGetValue(p.Name, out value);
return new InputParameterData(p, value, isSet ? DataSource.User : DataSource.NoSource, isSet ? InputDataStateUtil.GetInputDataState(value) : InputDataState.Unset);
});
ParameterDefinitionSet = new ParameterDefinitionSet(templateInfo.ParameterDefinitions);
}
/// <summary>
/// Indicates whether template creator should ignore evaluation results that does not match validation evaluation.
/// Warning will be logged and external evaluation results will be used.
/// </summary>
public bool ContinueOnMismatchedConditionsEvaluation { get; init; }
/// <summary>
/// Descriptors of the parameters.
/// </summary>
public ParameterDefinitionSet ParameterDefinitionSet { get; }
/// <inheritdoc/>
public IEnumerable<ITemplateParameter> Keys => _parametersData.Keys;
/// <inheritdoc/>
public IEnumerable<InputParameterData> Values => _parametersData.Values;
/// <inheritdoc/>
public int Count => _parametersData.Count;
/// <inheritdoc/>
public InputParameterData this[ITemplateParameter key] => _parametersData[key];
/// <inheritdoc/>
public bool ContainsKey(ITemplateParameter key) => _parametersData.ContainsKey(key);
/// <inheritdoc/>
public IEnumerator<KeyValuePair<ITemplateParameter, InputParameterData>> GetEnumerator() => _parametersData.GetEnumerator();
/// <inheritdoc/>
public bool TryGetValue(ITemplateParameter key, out InputParameterData value) => _parametersData.TryGetValue(key, out value);
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => _parametersData.GetEnumerator();
}