Skip to content

Commit

Permalink
include
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Jun 9, 2023
1 parent 3181589 commit 59a263e
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion packages/compiler/ts-gen/decorator-gen.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
Decorator,
FunctionParameter,
IntrinsicScalarName,
Model,
Namespace,
Program,
Scalar,
Type,
ValueType,
ignoreDiagnostics,
isUnknownType,
navigateTypesInNamespace,
Expand Down Expand Up @@ -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");
}
Expand All @@ -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 {
Expand Down

0 comments on commit 59a263e

Please sign in to comment.