Skip to content

Commit 3bbea5d

Browse files
committed
Swap references based on pre-loaded values
1 parent f97f77a commit 3bbea5d

1 file changed

Lines changed: 52 additions & 36 deletions

File tree

yaml-generation/generateDimensions.php

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@
161161
}
162162
}
163163

164-
164+
// Identify any issues regarding the references
165165
$implementationReferences = readYaml($implementationReferenceFile)['implementations'];
166-
assert_unique_refs($implementationReferences, $errorMsg);
166+
$references = array("implementations" => $implementationReferences);
167+
assertUniqueRefs($references, $errorMsg);
167168

168-
resolve_json_ref($dimensionsAggregated);
169+
resolveYamlReferences($dimensionsAggregated, $references, $errorMsg);
169170

170171

172+
// Inform of any errors detected
171173
if (count($errorMsg) > 0) {
172174
echo "\n\nFound " . count($errorMsg) . " errors:\n";
173175
foreach ($errorMsg as $e) {
@@ -177,30 +179,34 @@
177179
}
178180

179181

180-
181-
182+
// Store generated data
182183
$dimensionsString = yaml_emit($dimensionsAggregated);
183184
$targetGeneratedFile = getcwd() . "/src/assets/YAML/generated/generated.yaml";
184185
echo "\nStoring to $targetGeneratedFile\n";
185186
file_put_contents($targetGeneratedFile, $dimensionsString);
186187

187188

188189

189-
function assert_unique_refs($implementationReferences, &$errorMsg) {
190-
assert_unique_ref_by_key($implementationReferences, 'uuid', $errorMsg);
191-
assert_unique_ref_by_key($implementationReferences, 'name', $errorMsg);
192-
assert_unique_ref_by_key($implementationReferences, 'url', $errorMsg);
190+
function assertUniqueRefs($all_references, &$errorMsg) {
191+
foreach ($all_references as $references) {
192+
assertUniqueRefByKey($references, 'uuid', $errorMsg);
193+
assertUniqueRefByKey($references, 'name', $errorMsg);
194+
assertUniqueRefByKey($references, 'url', $errorMsg);
195+
assertUniqueRefByKey($references, "\n description", $errorMsg);
196+
}
193197
}
194198

195-
function assert_unique_ref_by_key($implementationReferences, $keyToAssert, &$errorMsg) {
199+
function assertUniqueRefByKey($references, $keyToAssert, &$errorMsg) {
196200
$all_values = array();
201+
$printable_keyToAssert = $keyToAssert;
202+
$keyToAssert = trim($keyToAssert);
197203

198-
foreach ($implementationReferences as $key => $reference) {
204+
foreach ($references as $key => $reference) {
199205
if (array_key_exists($keyToAssert, $reference)) {
200206
$value = $reference[$keyToAssert];
201207
// echo "$key: $value\n";
202208
if (array_key_exists($value, $all_values)) {
203-
array_push($errorMsg, "Duplicate '$keyToAssert' in reference: " . $all_values[$value] . " and $key: '$value'");
209+
array_push($errorMsg, "Duplicate '$keyToAssert' in reference file: " . $all_values[$value] . " and $key: $printable_keyToAssert='$value'");
204210
} else {
205211
$all_values[$value] = $key;
206212
}
@@ -254,42 +260,52 @@ function array_merge_recursive_ex(array $array1, array $array2)
254260
}
255261

256262

257-
/**
258-
* Traverse in-place an array and replaces json-pointers.
259-
*
260-
* @param unknown $data (reference)
263+
/*
264+
* Traverse an array in-place and replaces yaml pointers.
261265
*/
262-
function resolve_json_ref(&$data)
266+
function resolveYamlReferences(&$data, $references, &$errorMsg)
263267
{
264-
265-
266-
/**
267-
*
268-
* @param unknown $value (reference)
269-
* @param unknown $key
270-
* @param unknown $ctx
271-
*/
272-
function resolve_json_ref_cb(&$value, $key, $ctx)
268+
function resolveYamlReferencesCallback(&$value, $key, $payload)
273269
{
274270
if (!is_array($value)) {
275271
return;
276272
}
277273

278274
if (!array_key_exists('$ref', $value)) {
279-
$ctx["ctx"] = &$value;
280-
array_walk($value, "resolve_json_ref_cb", $ctx);
275+
// Call recursively until $value contains a '$ref' child
276+
array_walk($value, "resolveYamlReferencesCallback", $payload);
277+
return;
278+
} else {
279+
list($context, $ref) = extractReference($value['$ref']);
280+
if (is_null($context) || is_null($ref)) {
281+
array_push($payload['errorMsg'],"Invalid syntax in reference file: '" . $value['$ref'] . "'");
281282
return;
282283
}
284+
if (!array_key_exists($context, $payload['references']) ||
285+
!array_key_exists($ref, $payload['references'][$context]) ) {
286+
array_push($payload['errorMsg'],"Reference does not exist: '$context/$ref'");
287+
return;
288+
}
283289

284-
// echo "key: $key\nvalue: ".var_dump($ctx). "\n";
285-
$ctx["ctx"][$key] = readYaml($value['$ref']);
286-
290+
// Insert the actual reference object, instead of the reference link
291+
$value = $payload['references'][$context][$ref];
292+
}
287293
}
288294

289295

290-
// Create a context variable to store
291-
// the reference to the actual data, so that
292-
// resolve_reference can replace them.
293-
$ctx = array("ctx" => null);
294-
array_walk($data, "resolve_json_ref_cb", $ctx);
296+
// Call resolve_yaml_references_cb for each and every node in the data array
297+
$payload = array('references' => &$references, 'errorMsg' => &$errorMsg);
298+
array_walk($data, "resolveYamlReferencesCallback", $payload);
299+
}
300+
301+
302+
/*
303+
* Extract the context id and the reference id from the '$ref' value in the yaml
304+
*/
305+
function extractReference($str) {
306+
$regExp = "~#/([\w-]+)/([\w-]+)~";
307+
if (preg_match($regExp, $str, $matches)) {
308+
return array($matches[1], $matches[2]);
309+
}
310+
return null;
295311
}

0 commit comments

Comments
 (0)