mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 17:23:37 +08:00
refactor(schema): select output pattern path in one pass (#17461)
This commit is contained in:
@@ -423,24 +423,19 @@ const normalizeAdvertisedSchema = (
|
||||
export const presentOutputJsonSchema = (
|
||||
root: Record<string, unknown>,
|
||||
): Record<string, unknown> => {
|
||||
const patterns = advertisedSchemaPatterns(root).map(({ pattern }) => ({
|
||||
pattern,
|
||||
projection: projectPortablePattern(pattern),
|
||||
}));
|
||||
if (hasLiteralReference(root))
|
||||
let widened = false;
|
||||
let rewritten = false;
|
||||
for (const { pattern } of advertisedSchemaPatterns(root)) {
|
||||
const projection = projectPortablePattern(pattern);
|
||||
if (projection.kind !== "exact") widened = true;
|
||||
else if (projection.pattern !== pattern) rewritten = true;
|
||||
}
|
||||
// Exact re-emission leaves dependent schemas intact. Widening needs dependency analysis.
|
||||
if (hasLiteralReference(root) || widened)
|
||||
return normalizeAdvertisedSchema(root).schema as Record<string, unknown>;
|
||||
// Output schemas reach several megabytes. Exact re-emission changes no
|
||||
// dependent, so only a widened pattern needs the dependency analysis.
|
||||
if (patterns.some(({ projection }) => projection.kind !== "exact"))
|
||||
return normalizeAdvertisedSchema(root).schema as Record<string, unknown>;
|
||||
if (
|
||||
patterns.every(
|
||||
({ pattern, projection }) =>
|
||||
projection.kind === "exact" && projection.pattern === pattern,
|
||||
)
|
||||
)
|
||||
return root;
|
||||
return replaceExactPatterns(root) as Record<string, unknown>;
|
||||
return rewritten
|
||||
? (replaceExactPatterns(root) as Record<string, unknown>)
|
||||
: root;
|
||||
};
|
||||
|
||||
/** A reference into literal data needs dependency analysis, not in-place rewriting. */
|
||||
|
||||
Reference in New Issue
Block a user