|
161 | 161 | } |
162 | 162 | } |
163 | 163 |
|
164 | | - |
| 164 | +// Identify any issues regarding the references |
165 | 165 | $implementationReferences = readYaml($implementationReferenceFile)['implementations']; |
166 | | -assert_unique_refs($implementationReferences, $errorMsg); |
| 166 | +$references = array("implementations" => $implementationReferences); |
| 167 | +assertUniqueRefs($references, $errorMsg); |
167 | 168 |
|
168 | | -resolve_json_ref($dimensionsAggregated); |
| 169 | +resolveYamlReferences($dimensionsAggregated, $references, $errorMsg); |
169 | 170 |
|
170 | 171 |
|
| 172 | +// Inform of any errors detected |
171 | 173 | if (count($errorMsg) > 0) { |
172 | 174 | echo "\n\nFound " . count($errorMsg) . " errors:\n"; |
173 | 175 | foreach ($errorMsg as $e) { |
|
177 | 179 | } |
178 | 180 |
|
179 | 181 |
|
180 | | - |
181 | | - |
| 182 | +// Store generated data |
182 | 183 | $dimensionsString = yaml_emit($dimensionsAggregated); |
183 | 184 | $targetGeneratedFile = getcwd() . "/src/assets/YAML/generated/generated.yaml"; |
184 | 185 | echo "\nStoring to $targetGeneratedFile\n"; |
185 | 186 | file_put_contents($targetGeneratedFile, $dimensionsString); |
186 | 187 |
|
187 | 188 |
|
188 | 189 |
|
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 | + } |
193 | 197 | } |
194 | 198 |
|
195 | | -function assert_unique_ref_by_key($implementationReferences, $keyToAssert, &$errorMsg) { |
| 199 | +function assertUniqueRefByKey($references, $keyToAssert, &$errorMsg) { |
196 | 200 | $all_values = array(); |
| 201 | + $printable_keyToAssert = $keyToAssert; |
| 202 | + $keyToAssert = trim($keyToAssert); |
197 | 203 |
|
198 | | - foreach ($implementationReferences as $key => $reference) { |
| 204 | + foreach ($references as $key => $reference) { |
199 | 205 | if (array_key_exists($keyToAssert, $reference)) { |
200 | 206 | $value = $reference[$keyToAssert]; |
201 | 207 | // echo "$key: $value\n"; |
202 | 208 | 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'"); |
204 | 210 | } else { |
205 | 211 | $all_values[$value] = $key; |
206 | 212 | } |
@@ -254,42 +260,52 @@ function array_merge_recursive_ex(array $array1, array $array2) |
254 | 260 | } |
255 | 261 |
|
256 | 262 |
|
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. |
261 | 265 | */ |
262 | | -function resolve_json_ref(&$data) |
| 266 | +function resolveYamlReferences(&$data, $references, &$errorMsg) |
263 | 267 | { |
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) |
273 | 269 | { |
274 | 270 | if (!is_array($value)) { |
275 | 271 | return; |
276 | 272 | } |
277 | 273 |
|
278 | 274 | 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'] . "'"); |
281 | 282 | return; |
282 | 283 | } |
| 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 | + } |
283 | 289 |
|
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 | + } |
287 | 293 | } |
288 | 294 |
|
289 | 295 |
|
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; |
295 | 311 | } |
0 commit comments