UNPKG

@typespec/http-server-js

Version:

TypeSpec HTTP server code generator for JavaScript

55 lines 2.26 kB
import { getEncode } from "@typespec/compiler"; import { getJsScalar } from "../common/scalar.js"; const ENCODE_SOURCES = { ModelProperty: (t) => t.type, Scalar: (t) => t, }; /** * Resolves the chain of `@encode` encoders that apply to a given property with a given canonical (logical) type. * * @param ctx - The context to use for resolving encoders. * @param module - The module that the property is defined in. * @param encodeSource - The original property to resolve encoders for. * @param canonicalType - The canonical type of the property -- this might be different from the type of the property itself. * @returns A resolved encoding chain describing the final canonical type, the ultimate target type of the chain, and the encoders that apply to the property in order. */ export function resolveEncodingChain(ctx, module, encodeSource, canonicalType) { let encoders = []; let targetType = canonicalType; for (const [kind, select] of Object.entries(ENCODE_SOURCES)) { while (encodeSource.kind === kind) { const s = select(encodeSource); const encoding = getEncode(ctx.program, encodeSource); if (!encoding) break; targetType = encodeSource = encoding.type; if (s.kind !== "Scalar") { // Decay because we don't know how to encode anything other than a scalar. // Should be unreachable? decay(encoding.type); } else { const sourceJsScalar = getJsScalar(ctx, module, s, encodeSource); const encoder = sourceJsScalar.getEncoding(encoding); if (encoder) { encoders.push(encoder); } else { // Decay because we don't know what the encoding is. // Invalidate the entire chain and set the current encoding.type as the canonical type. decay(encoding.type); } } } } return { canonicalType, targetType, encoders, }; function decay(t) { encoders = []; canonicalType = encodeSource = t; } } //# sourceMappingURL=encoding.js.map