* For a shared definition that is an `anyOf` discriminated union with a * string-const discriminator property (e.g. `Attachment` with `type: "file" | * "directory" | ...`), return the synthesized Go discriminator enum name and * per-variant const names that the api/rpc generator emits via * `emi
(
unionTypeName: string,
schema: JSONSchema7,
definitions: Record<string, unknown>
)
| 3567 | * not match the const-discriminator pattern. |
| 3568 | */ |
| 3569 | function collectGoSharedAnyOfDiscriminatorAliasNames( |
| 3570 | unionTypeName: string, |
| 3571 | schema: JSONSchema7, |
| 3572 | definitions: Record<string, unknown> |
| 3573 | ): { enumName: string; constNames: string[] } | undefined { |
| 3574 | const variants = Array.isArray(schema.anyOf) ? schema.anyOf : undefined; |
| 3575 | if (!variants || variants.length === 0) return undefined; |
| 3576 | |
| 3577 | const resolvedVariants: JSONSchema7[] = []; |
| 3578 | for (const variant of variants) { |
| 3579 | const resolved = resolveSharedAnyOfVariant(variant, definitions); |
| 3580 | if (!resolved || !resolved.properties) return undefined; |
| 3581 | resolvedVariants.push(resolved); |
| 3582 | } |
| 3583 | |
| 3584 | const firstVariant = resolvedVariants[0]; |
| 3585 | for (const [propName, propSchemaRaw] of Object.entries(firstVariant.properties!)) { |
| 3586 | if (typeof propSchemaRaw !== "object" || propSchemaRaw === null) continue; |
| 3587 | const firstPropSchema = propSchemaRaw as JSONSchema7; |
| 3588 | if (typeof firstPropSchema.const !== "string") continue; |
| 3589 | |
| 3590 | const collectedValues: string[] = []; |
| 3591 | let valid = true; |
| 3592 | for (const variant of resolvedVariants) { |
| 3593 | if (!(variant.required || []).includes(propName)) { valid = false; break; } |
| 3594 | const variantProp = variant.properties?.[propName]; |
| 3595 | if (typeof variantProp !== "object" || variantProp === null) { valid = false; break; } |
| 3596 | const variantConst = (variantProp as JSONSchema7).const; |
| 3597 | if (typeof variantConst !== "string") { valid = false; break; } |
| 3598 | collectedValues.push(variantConst); |
| 3599 | } |
| 3600 | if (!valid || collectedValues.length === 0) continue; |
| 3601 | |
| 3602 | const enumName = `${unionTypeName}${toGoFieldName(propName)}`; |
| 3603 | const constNames = [...new Set(collectedValues)].map( |
| 3604 | (value) => `${enumName}${goEnumConstSuffix(value)}` |
| 3605 | ); |
| 3606 | return { enumName, constNames }; |
| 3607 | } |
| 3608 | return undefined; |
| 3609 | } |
| 3610 | |
| 3611 | function resolveSharedAnyOfVariant( |
| 3612 | variant: JSONSchema7 | boolean, |
no test coverage detected
searching dependent graphs…