-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterUtil.hpp
More file actions
268 lines (240 loc) · 11.5 KB
/
Copy pathParameterUtil.hpp
File metadata and controls
268 lines (240 loc) · 11.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef RUNCPP2_PARAMETER_UTIL_HPP
#define RUNCPP2_PARAMETER_UTIL_HPP
#include "runcpp2/Data/ParameterValue.hpp"
#include "runcpp2/ParseUtil.hpp"
namespace runcpp2
{
//Expecting `std::unordered_map<std::string, ParameterValue> Parameters;` and
//`std::unordered_map<std::string, std::string> Variables;` from T
template<typename T>
inline DS::Result<void> ParseParametersAndVariables(T& data, YAML::ConstNodePtr node)
{
std::vector<NodeRequirement> requirements =
{
NodeRequirement("Parameters", YAML::NodeType::Map, false, true),
NodeRequirement("Variables", YAML::NodeType::Map, false, true),
};
DS_ASSERT_TRUE(CheckNodeRequirements(node, requirements));
//NOTE: Evaluate Parameters first, since we need to perform substitutions
if(ExistAndHasChild(node, "Parameters"))
{
YAML::ConstNodePtr parametersNode = node->GetMapValueNode("Parameters");
for(int i = 0; i < parametersNode->GetChildrenCount(); ++i)
{
std::string parameterName = parametersNode->GetMapKeyScalarAt<std::string>(i).DS_TRY();
YAML::ConstNodePtr valueNode = parametersNode->GetMapValueNodeAt(i);
Data::ParameterValue paramValue;
paramValue.ParseYAML_Node(valueNode).DS_TRY();
if(data.Parameters.count(parameterName) != 0)
{
return DS_ERROR_MSG("ScriptInfo: Same parameter (" + parameterName +
") is added more than once");
}
data.Parameters[parameterName] = paramValue;
}
}
//NOTE: Evaluate Parameters first, since we need to perform substitutions
if(ExistAndHasChild(node, "Variables"))
{
YAML::ConstNodePtr variablesNode = node->GetMapValueNode("Variables");
for(int i = 0; i < variablesNode->GetChildrenCount(); ++i)
{
std::string variableName = variablesNode->GetMapKeyScalarAt<std::string>(i).DS_TRY();
std::string variableValue = variablesNode->GetMapValueScalarAt<std::string>(i).DS_TRY();
if(data.Variables.count(variableName) != 0)
{
return DS_ERROR_MSG("ScriptInfo: Same variable (" + variableName +
") is added more than once");
}
data.Variables[variableName] = variableValue;
}
}
return {};
}
//Expecting `std::unordered_map<std::string, ParameterValue> Parameters;` and
//`std::unordered_map<std::string, std::string> Variables;` from T
template<typename T>
inline DS::Result<void>
ApplyParametersAndVariables(T& data,
YAML::NodePtr& node,
YAML::ResourceHandle& resourceHandle,
std::unordered_map< std::string,
std::vector<std::string>>& substitutionMap,
const std::unordered_map<std::string, std::string>& inputParameters,
const std::vector<YAML::ConstNodePtr>& excludedNodes)
{
//Remove parameters and variables in the cloned node.
if(node->HasMapKey("Parameters"))
{
node->RemoveMapChild("Parameters").DS_TRY();
}
if(node->HasMapKey("Variables"))
{
node->RemoveMapChild("Variables").DS_TRY();
}
//Populate parameters values first
for(auto it = data.Parameters.begin(); it != data.Parameters.end(); ++it)
{
bool isDefault = true;
std::string valueToParse;
if(inputParameters.count(it->first) == 0)
valueToParse = it->second.Default;
else
{
valueToParse = inputParameters.at(it->first);
isDefault = false;
}
//Parse the value
std::string subKey = "{" + it->first + "}";
//Check if it is optional, if so check if it can be empty
if(it->second.Optional && valueToParse.empty())
{
static_assert((int)Data::ParameterValue::ConstraintType::Count == 5, "");
if(it->second.CurrentConstraintType != Data::ParameterValue::ConstraintType::None)
continue;
}
if(it->second.Array)
{
std::vector<std::string> inputValues;
SplitString(valueToParse, ",", inputValues);
for(int i = 0; i < inputValues.size(); ++i)
{
bool inConstraint = it->second.IsInputInConstraint(inputValues[i]).DS_TRY();
if(!inConstraint)
{
return DS_ERROR_MSG("Input parameter[" + DS_STR(i) + "]: " +
inputValues[i] +
(isDefault ? "(Default)" : "") +
" is not in constraint");
}
}
substitutionMap[subKey] = inputValues;
}
else
{
bool inConstraint = it->second.IsInputInConstraint(valueToParse).DS_TRY();
if(!inConstraint)
{
return DS_ERROR_MSG("Input parameter: " + it->first +
(isDefault ? "(Default)" : "") +
" is not in constraint");
}
substitutionMap[subKey] = {valueToParse};
}
} //for(auto it = Parameters.begin(); it != Parameters.end(); ++it)
std::unordered_map<std::string, std::vector<std::string>> variablesMap;
//Populate variables next
for(auto it = data.Variables.begin(); it != data.Variables.end(); ++it)
{
//Check if the same key already exists in the parameter
if(data.Parameters.count(it->first) != 0)
{
return DS_ERROR_MSG("Variable name " +
it->first +
" already exists in Parameters");
}
std::string subKey = "{" + it->first + "}";
variablesMap[subKey] = {};
PerformMultiSubstitutions( substitutionMap,
{},
it->second,
variablesMap[subKey]).DS_TRY();
}
substitutionMap.insert(variablesMap.begin(), variablesMap.end());
//NOTE: We still need to iterate through the whole thing even if we have nothing to
// substitute because we need to get the escaped strings
//Perform substitution recursively over the whole YAML object
std::deque<YAML::NodePtr> nodesToVisit;
nodesToVisit.push_back(node);
while(!nodesToVisit.empty())
{
YAML::NodePtr currentNode = nodesToVisit.front();
nodesToVisit.pop_front();
bool skipThis = false;
for(int i = 0; i < excludedNodes.size(); ++i)
{
if(excludedNodes.at(i).get() == currentNode.get())
{
skipThis = true;
break;
}
}
if(skipThis)
continue;
static_assert((int)YAML::NodeType::Count == 4, "");
switch(currentNode->GetType())
{
case YAML::NodeType::Scalar:
{
YAML::Node* parent = currentNode->GetParent();
std::string scalarValue = currentNode->GetScalar<std::string>().DS_TRY();
if(parent && parent->GetType() == YAML::NodeType::Sequence)
{
std::vector<std::string> newValues;
PerformMultiSubstitutions( substitutionMap,
{},
scalarValue,
newValues).DS_TRY();
if(newValues.empty())
return DS_ERROR_MSG("Substitution array returned empty");
//Update the current value
currentNode->InitScalar(newValues[0], resourceHandle).DS_TRY();
//Find the index of the current node
int currentIndex = -1;
for(int i = 0; i < parent->GetChildrenCount(); ++i)
{
if(parent->GetSequenceChildNode(i) == currentNode)
{
currentIndex = i;
break;
}
}
if(currentIndex == -1)
return DS_ERROR_MSG("Cannot find current node from parent?");
//Then insert the rest of the substituted values after the current one
if(newValues.size() > 1)
{
for(int i = 1; i < newValues.size(); ++i)
{
YAML::NodePtr newChild =
parent->CreateSequenceChildAt(currentIndex + i).DS_TRY();
newChild->InitScalar(newValues[i], resourceHandle).DS_TRY();
}
}
}
else
{
PerformSubstitutions(substitutionMap, {}, scalarValue).DS_TRY();
currentNode->InitScalar(scalarValue, resourceHandle).DS_TRY();
}
break;
} //case YAML::NodeType::Scalar:
case YAML::NodeType::Alias:
return DS_ERROR_MSG("Anchors should be resolved. This should not be reached");
case YAML::NodeType::Sequence:
for(int i = currentNode->GetChildrenCount() - 1; i >= 0; --i)
nodesToVisit.push_back(currentNode->GetSequenceChildNode(i));
break;
case YAML::NodeType::Map:
for(int i = currentNode->GetChildrenCount() - 1; i >= 0; --i)
nodesToVisit.push_back(currentNode->GetMapValueNodeAt(i));
break;
} //switch(currentNode->GetType())
} //while(!nodesToVisit.empty())
return {};
}
inline void CreateParameterValues( const std::string rawParams,
std::unordered_map<std::string, std::string>& outParameters)
{
std::vector<std::string> paramNameVals;
SplitString(rawParams, ":", paramNameVals);
if(paramNameVals.size() % 2 != 0)
{
ssLOG_ERROR("Failed to parse parameters. Defaults to no parameters");
return;
}
for(int i = 0; i < paramNameVals.size(); i += 2)
outParameters[paramNameVals[i]] = paramNameVals[i + 1];
}
}
#endif