|
| 1 | +import { |
| 2 | + type DecoratorContext, |
| 3 | + type DecoratorFunction, |
| 4 | + type Namespace, |
| 5 | + type Program, |
| 6 | + type Type, |
| 7 | + getTypeName, |
| 8 | + validateDecoratorUniqueOnNode, |
| 9 | +} from "@typespec/compiler"; |
| 10 | + |
| 11 | +import { createStateSymbol, NAMESPACE, reportDiagnostic } from "../lib.js"; |
| 12 | +import { useStateMap } from "./state-map.js"; |
| 13 | + |
| 14 | +// This will set the namespace for decorators implemented in this file |
| 15 | +export const namespace = NAMESPACE; |
| 16 | + |
| 17 | +export interface SchemaDetails { |
| 18 | + name?: string; |
| 19 | +} |
| 20 | + |
| 21 | +export interface Schema extends SchemaDetails { |
| 22 | + type: Namespace; |
| 23 | +} |
| 24 | + |
| 25 | +const [getSchema, setSchema, getSchemaMap] = useStateMap<Namespace, Schema>( |
| 26 | + createStateSymbol("schemas"), |
| 27 | +); |
| 28 | + |
| 29 | +/** |
| 30 | + * List all the schemas defined in the TypeSpec program |
| 31 | + * @param program Program |
| 32 | + * @returns List of schemas. |
| 33 | + */ |
| 34 | +export function listSchemas(program: Program): Schema[] { |
| 35 | + return [...getSchemaMap(program).values()]; |
| 36 | +} |
| 37 | + |
| 38 | +export { |
| 39 | + /** |
| 40 | + * Get the schema information for the given namespace. |
| 41 | + * @param program Program |
| 42 | + * @param namespace Schema namespace |
| 43 | + * @returns Schema information or undefined if namespace is not a schema namespace. |
| 44 | + */ |
| 45 | + getSchema, |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * Check if the namespace is defined as a schema. |
| 50 | + * @param program Program |
| 51 | + * @param namespace Namespace |
| 52 | + * @returns Boolean |
| 53 | + */ |
| 54 | +export function isSchema(program: Program, namespace: Namespace): boolean { |
| 55 | + return getSchemaMap(program).has(namespace); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Mark the given namespace as a schema. |
| 60 | + * @param program Program |
| 61 | + * @param namespace Namespace |
| 62 | + * @param details Schema details |
| 63 | + */ |
| 64 | +export function addSchema( |
| 65 | + program: Program, |
| 66 | + namespace: Namespace, |
| 67 | + details: SchemaDetails = {}, |
| 68 | +): void { |
| 69 | + const schemaMap = getSchemaMap(program); |
| 70 | + const existing = schemaMap.get(namespace) ?? {}; |
| 71 | + setSchema(program, namespace, { ...existing, ...details, type: namespace }); |
| 72 | +} |
| 73 | + |
| 74 | +export const $schema: DecoratorFunction = ( |
| 75 | + context: DecoratorContext, |
| 76 | + target: Namespace, |
| 77 | + options: Type, |
| 78 | +) => { |
| 79 | + validateDecoratorUniqueOnNode(context, target, $schema); |
| 80 | + |
| 81 | + if (options && options.kind !== "Model") { |
| 82 | + reportDiagnostic(context.program, { |
| 83 | + code: "invalid-argument", |
| 84 | + format: { value: options.kind, expected: "Model" }, |
| 85 | + target: context.getArgumentTarget(0)!, |
| 86 | + }); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const schemaDetails: SchemaDetails = {}; |
| 91 | + const name = options?.properties.get("name")?.type; |
| 92 | + if (name) { |
| 93 | + if (name.kind === "String") { |
| 94 | + schemaDetails.name = name.value; |
| 95 | + } else { |
| 96 | + reportDiagnostic(context.program, { |
| 97 | + code: "unassignable", |
| 98 | + format: { sourceType: getTypeName(name), targetType: "String" }, |
| 99 | + target: context.getArgumentTarget(0)!, |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + addSchema(context.program, target, schemaDetails); |
| 105 | +}; |
0 commit comments