Skip to content

Commit 6f73524

Browse files
committed
fix(ibm-api-symmetry): print info logs in coherent order
The code to determine if one schema is a graph fragment of the other uses a depth-first algorithm that prints a log, if it determines the schema to violate the graph fragment pattern, with the reason behind the violation. Due to the depth-first nature of the algorithm, the logs are currently printed in depth first order, which is not as coherent for the user to read. This change introduces a stack to collect the logs during processing and print them in reverse order afterwards to give a user a sense of what happened during the processing. Signed-off-by: Dustin Popp <[email protected]>
1 parent 7498e49 commit 6f73524

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

packages/ruleset/src/functions/api-symmetry.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const {
2222
let ruleId;
2323
let logger;
2424

25+
// The graph fragment check is depth-first. Use this stack to
26+
// print the relevant info logs in a sane order.
27+
const infoLogStack = [];
28+
2529
/**
2630
* The implementation for this rule makes assumptions that are dependent on the
2731
* presence of the following other rules:
@@ -174,7 +178,7 @@ function checkForGraphFragmentPattern(
174178
schema => getSchemaType(variant) === getSchemaType(schema)
175179
)
176180
) {
177-
logger.info(
181+
infoLogStack.push(
178182
`${ruleId}: variant and canonical schemas are different types`
179183
);
180184
result = false;
@@ -198,7 +202,7 @@ function checkForGraphFragmentPattern(
198202
)
199203
)
200204
) {
201-
logger.info(
205+
infoLogStack.push(
202206
`${ruleId}: variant is array with schema that is not a graph fragment of canonical items schema`
203207
);
204208
result = false;
@@ -222,7 +226,7 @@ function checkForGraphFragmentPattern(
222226
)
223227
)
224228
) {
225-
logger.info(
229+
infoLogStack.push(
226230
`${ruleId}: variant is dictionary with an additionalProperties schema that is not a graph fragment of canonical`
227231
);
228232
result = false;
@@ -258,7 +262,7 @@ function checkForGraphFragmentPattern(
258262
)
259263
)
260264
) {
261-
logger.info(
265+
infoLogStack.push(
262266
`${ruleId}: variant is dictionary with a patternProperties schema that is not a graph fragment of canonical`
263267
);
264268
result = false;
@@ -301,7 +305,7 @@ function checkForGraphFragmentPattern(
301305
// Note: Prototype schemas are allowed to define writeOnly properties
302306
// that don't exist on the canonical schema.
303307
if (!valid && !(considerWriteOnly && prop.writeOnly)) {
304-
logger.info(
308+
infoLogStack.push(
305309
propExistsSomewhere
306310
? `${ruleId}: nested object property ${name} is not a graph fragment of canonical property ${name}`
307311
: `${ruleId}: property '${name}' does not exist on the canonical schema`
@@ -324,7 +328,7 @@ function checkForGraphFragmentPattern(
324328
true
325329
)
326330
) {
327-
logger.info(
331+
infoLogStack.push(
328332
`${ruleId}: variant schema applicator '${applicator}' is not a graph fragment of the canonical schema`
329333
);
330334
result = false;
@@ -409,6 +413,12 @@ function checkForGraphFragmentPattern(
409413
false
410414
);
411415

416+
// Print the logs gathered within the `isGraphFragment` function,
417+
// in reverse order - it will be coherent for the user.
418+
while (infoLogStack.length) {
419+
logger.info(infoLogStack.pop());
420+
}
421+
412422
logger.debug(
413423
`${ruleId}: isGraphFragment() returned [${variantIsGraphFragment},${variantOmitsProperties}]`
414424
);

0 commit comments

Comments
 (0)