Skip to content

Commit

Permalink
Fix doc refs to things with definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeiser committed Feb 20, 2025
1 parent a3bcdbd commit 2282328
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 70 deletions.
58 changes: 41 additions & 17 deletions bin/clover/src/cfDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type CfMultiTypeProperty =
& {
type?: undefined;
oneOf?: CfProperty[];
allOf?: CfProperty[];
anyOf?: CfProperty[];
};

Expand Down Expand Up @@ -315,37 +316,60 @@ export async function loadCfDatabase(
): Promise<CfDb> {
path ??= DEFAULT_PATH;
if (Object.keys(DB).length === 0) {
for (const data of await readSpecs(path)) {
const typeName: string = data.typeName;
for (const cfSchema of await readSchemas(path)) {
const typeName: string = cfSchema.typeName;

if (services && !services.some((service) => typeName.match(service))) {
continue;
}

logger.debug(`Loaded ${typeName}`);

try {
const expandedSchema = await $RefParser.dereference(data, {
dereference: {
circular: "ignore",
onDereference: (path: string, ref: JSONSchema.Object) => {
const name = path.split("/").pop();
ref.title = ref.title ?? name;
},
},
}) as CfSchema;
DB[typeName] = expandedSchema;
} catch (e) {
logger.error(`failed to expand ${typeName}`, e);
DB[typeName] = data;
// Mark all definition props with their enclosing name for doc link generation
if (cfSchema.definitions) {
for (const [defName, defProp] of Object.entries(cfSchema.definitions)) {
for (const cfProp of nestedCfProps(defProp)) {
(cfProp as { defName?: string }).defName = defName;
}
}
}

// Dereference the schema
const dereferencedSchema = await $RefParser.dereference(cfSchema, {
dereference: {
circular: "ignore",
onDereference: (path: string, ref: JSONSchema.Object) => {
const name = path.split("/").pop();
ref.title = ref.title ?? name;
},
},
}) as CfSchema;
DB[typeName] = dereferencedSchema;
}
}

return DB;
}

async function readSpecs(path: string) {
function* nestedCfProps(prop: CfProperty): Generator<CfProperty> {
yield prop;
for (const p of prop.anyOf ?? []) yield* nestedCfProps(p as CfProperty);
for (const p of prop.oneOf ?? []) yield* nestedCfProps(p as CfProperty);
for (const p of prop.allOf ?? []) yield* nestedCfProps(p as CfProperty);
if ("properties" in prop) {
for (const p of Object.values(prop.properties ?? {})) {
yield* nestedCfProps(p as CfProperty);
}
}
if ("patternProperties" in prop) {
for (const p of Object.values(prop.patternProperties ?? {})) {
yield* nestedCfProps(p as CfProperty);
}
}
if ("items" in prop) yield* nestedCfProps(prop.items as CfProperty);
}

async function readSchemas(path: string): Promise<CfSchema[]> {
const fullPath = await Deno.realPath(path);
logger.debug("Loading database from Cloudformation schema", { fullPath });
const result = [];
Expand Down
17 changes: 17 additions & 0 deletions bin/clover/src/commands/generateSiSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { PkgSpec } from "../bindings/PkgSpec.ts";
import { ignoreSpecsWithoutHandlers } from "../pipeline-steps/ignoreSpecsWithoutHandlers.ts";
import { SchemaVariantSpec } from "../bindings/SchemaVariantSpec.ts";
import { SchemaSpec } from "../bindings/SchemaSpec.ts";
import { bfsPropTree, ExpandedPropSpec } from "../spec/props.ts";
import { PropSpec } from "../bindings/PropSpec.ts";

const logger = _logger.ns("siSpecs").seal();
const SI_SPEC_DIR = "si-specs";
Expand Down Expand Up @@ -135,9 +137,24 @@ function unexpandSchema(
variants: expanded.variants.map(unexpandVariant),
};
}

function unexpandVariant(
expanded: ExpandedSchemaVariantSpec,
): SchemaVariantSpec {
const { cfSchema: _, ...variant } = expanded;
bfsPropTree([
variant.domain,
variant.resourceValue,
variant.secrets,
variant.secretDefinition,
], unexpandProperty);
return variant;
}

function unexpandProperty(expanded: ExpandedPropSpec): PropSpec {
const deleteable = expanded as Partial<ExpandedPropSpec>;
delete deleteable.metadata;
delete deleteable.joiValidation;
delete deleteable.cfProp;
return expanded;
}
6 changes: 5 additions & 1 deletion bin/clover/src/pipeline-steps/addDefaultPropsAndSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export function addDefaultPropsAndSockets(
const { domain } = schemaVariant;

// Extra prop
const extraProp = createObjectProp("extra", domain.metadata.propPath);
const extraProp = createObjectProp(
"extra",
domain.metadata.propPath,
undefined,
);

// Create PropUsageMap
{
Expand Down
3 changes: 0 additions & 3 deletions bin/clover/src/pipeline-steps/generateAssetFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ function generatePropBuilderString(
) +
inner +
`${indent(indent_level)}.build()`;
// Now that we've emitted it, we don't need prop.joiValidation anymore. Don't bother
// putting it in the spec (we already have validationFormat).
delete prop.joiValidation;
return result;
}
}
Expand Down
4 changes: 2 additions & 2 deletions bin/clover/src/pipeline-steps/generateSubAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ export function generateSubAssets(
actionFuncs: [],
leafFunctions: [],
managementFuncs: [],
resourceValue: createDefaultProp("resource_value"),
resourceValue: createDefaultProp("resource_value", undefined),
sockets: [newSpecOutputSocket],
secrets: createDefaultProp("secrets"),
secrets: createDefaultProp("secrets", undefined),
uniqueId: variantId,
};

Expand Down
Loading

0 comments on commit 2282328

Please sign in to comment.