From 59a263ed56d60229ef5a47f34bc0203b77daf25f Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Fri, 9 Jun 2023 13:18:21 -0700 Subject: [PATCH] include --- packages/compiler/ts-gen/decorator-gen.ts | 71 ++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/compiler/ts-gen/decorator-gen.ts b/packages/compiler/ts-gen/decorator-gen.ts index fcfcd1b044..fa7a4134a4 100644 --- a/packages/compiler/ts-gen/decorator-gen.ts +++ b/packages/compiler/ts-gen/decorator-gen.ts @@ -1,10 +1,13 @@ import { Decorator, FunctionParameter, + IntrinsicScalarName, Model, Namespace, Program, + Scalar, Type, + ValueType, ignoreDiagnostics, isUnknownType, navigateTypesInNamespace, @@ -58,7 +61,11 @@ export function generateDecoratorTSSignature( return `${param.name}${optional}: ${getTSParmeterType(param.type, isTarget)}`; } - function getTSParmeterType(type: Type, isTarget?: boolean): string { + function getTSParmeterType(type: Type | ValueType, isTarget?: boolean): string { + if (type.kind === "Value") { + console.log("Target", type); + return getValueTSType(type.target); + } if (isTarget && isUnknownType(type)) { return useCompilerType("Type"); } @@ -67,6 +74,68 @@ export function generateDecoratorTSSignature( } return "unknown"; } + + function getValueTSType(type: Type): string { + switch (type.kind) { + case "Boolean": + return `${type.value}`; + case "String": + return `"${type.value}"`; + case "Number": + return `${type.value}`; + case "Scalar": + return getScalarTSType(type); + } + return "unknown"; + } + + function getScalarTSType(scalar: Scalar): string { + const isStd = program.checker.isStdType(scalar); + if (isStd) { + return getStdScalarTSType(scalar); + } else if (scalar.baseScalar) { + return getScalarTSType(scalar.baseScalar); + } else { + return "unknown"; + } + } + + function getStdScalarTSType(scalar: Scalar & { name: IntrinsicScalarName }): string { + switch (scalar.name) { + case "numeric": + case "integer": + case "int8": + case "int16": + case "int32": + case "int64": + case "safeint": + case "uint8": + case "uint16": + case "uint32": + case "uint64": + case "float": + case "float64": + case "float32": + case "decimal": + case "decimal128": + return "number"; + case "string": + case "url": + return "string"; + case "boolean": + return "boolean"; + case "plainDate": + case "utcDateTime": + case "offsetDateTime": + case "plainTime": + case "duration": + case "bytes": + return "unknown"; + default: + const _assertNever: never = scalar.name; + return "unknown"; + } + } } function isReflectionType(type: Type): type is Model {