Skip to content

Commit 61703bb

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 the user a better sense of what happened during the processing. Signed-off-by: Dustin Popp <[email protected]>
1 parent f4cfcf1 commit 61703bb

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:
@@ -175,7 +179,7 @@ function checkForGraphFragmentPattern(
175179
schema => getSchemaType(variant) === getSchemaType(schema)
176180
)
177181
) {
178-
logger.info(
182+
infoLogStack.push(
179183
`${ruleId}: variant and canonical schemas are different types`
180184
);
181185
result = false;
@@ -199,7 +203,7 @@ function checkForGraphFragmentPattern(
199203
)
200204
)
201205
) {
202-
logger.info(
206+
infoLogStack.push(
203207
`${ruleId}: variant is array with schema that is not a graph fragment of canonical items schema`
204208
);
205209
result = false;
@@ -223,7 +227,7 @@ function checkForGraphFragmentPattern(
223227
)
224228
)
225229
) {
226-
logger.info(
230+
infoLogStack.push(
227231
`${ruleId}: variant is dictionary with an additionalProperties schema that is not a graph fragment of canonical`
228232
);
229233
result = false;
@@ -259,7 +263,7 @@ function checkForGraphFragmentPattern(
259263
)
260264
)
261265
) {
262-
logger.info(
266+
infoLogStack.push(
263267
`${ruleId}: variant is dictionary with a patternProperties schema that is not a graph fragment of canonical`
264268
);
265269
result = false;
@@ -302,7 +306,7 @@ function checkForGraphFragmentPattern(
302306
// Note: Prototype schemas are allowed to define writeOnly properties
303307
// that don't exist on the canonical schema.
304308
if (!valid && !(considerWriteOnly && prop.writeOnly)) {
305-
logger.info(
309+
infoLogStack.push(
306310
propExistsSomewhere
307311
? `${ruleId}: nested object property ${name} is not a graph fragment of canonical property ${name}`
308312
: `${ruleId}: property '${name}' does not exist on the canonical schema`
@@ -325,7 +329,7 @@ function checkForGraphFragmentPattern(
325329
true
326330
)
327331
) {
328-
logger.info(
332+
infoLogStack.push(
329333
`${ruleId}: variant schema applicator '${applicator}' is not a graph fragment of the canonical schema`
330334
);
331335
result = false;
@@ -410,6 +414,12 @@ function checkForGraphFragmentPattern(
410414
false
411415
);
412416

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

0 commit comments

Comments
 (0)