Skip to content

Commit a38a06c

Browse files
committed
more tests
1 parent 4a0c40f commit a38a06c

File tree

3 files changed

+2118
-17
lines changed

3 files changed

+2118
-17
lines changed

packages/sdk/src/ts-schema-generator.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export class TsSchemaGenerator {
318318
);
319319
}
320320

321-
const defaultValue = this.getMappedDefault(field);
321+
const defaultValue = this.getFieldMappedDefault(field);
322322
if (defaultValue !== undefined) {
323323
if (typeof defaultValue === 'object' && !Array.isArray(defaultValue)) {
324324
if ('call' in defaultValue) {
@@ -435,39 +435,44 @@ export class TsSchemaGenerator {
435435
}
436436
}
437437

438-
private getMappedDefault(
438+
private getFieldMappedDefault(
439439
field: DataModelField,
440440
): string | number | boolean | unknown[] | { call: string; args: any[] } | { authMember: string[] } | undefined {
441441
const defaultAttr = getAttribute(field, '@default');
442442
if (!defaultAttr) {
443443
return undefined;
444444
}
445-
446445
const defaultValue = defaultAttr.args[0]?.value;
447446
invariant(defaultValue, 'Expected a default value');
447+
return this.getMappedValue(defaultValue, field.type);
448+
}
448449

449-
if (isLiteralExpr(defaultValue)) {
450-
const lit = (defaultValue as LiteralExpr).value;
451-
return field.type.type === 'Boolean'
450+
private getMappedValue(
451+
expr: Expression,
452+
fieldType: DataModelFieldType,
453+
): string | number | boolean | unknown[] | { call: string; args: any[] } | { authMember: string[] } | undefined {
454+
if (isLiteralExpr(expr)) {
455+
const lit = (expr as LiteralExpr).value;
456+
return fieldType.type === 'Boolean'
452457
? (lit as boolean)
453-
: ['Int', 'Float', 'Decimal', 'BigInt'].includes(field.type.type!)
458+
: ['Int', 'Float', 'Decimal', 'BigInt'].includes(fieldType.type!)
454459
? Number(lit)
455460
: lit;
456-
} else if (isArrayExpr(defaultValue)) {
457-
return defaultValue.items.map((item) => this.getLiteral(item));
458-
} else if (isReferenceExpr(defaultValue) && isEnumField(defaultValue.target.ref)) {
459-
return defaultValue.target.ref.name;
460-
} else if (isInvocationExpr(defaultValue)) {
461+
} else if (isArrayExpr(expr)) {
462+
return expr.items.map((item) => this.getMappedValue(item, fieldType));
463+
} else if (isReferenceExpr(expr) && isEnumField(expr.target.ref)) {
464+
return expr.target.ref.name;
465+
} else if (isInvocationExpr(expr)) {
461466
return {
462-
call: defaultValue.function.$refText,
463-
args: defaultValue.args.map((arg) => this.getLiteral(arg.value)),
467+
call: expr.function.$refText,
468+
args: expr.args.map((arg) => this.getLiteral(arg.value)),
464469
};
465-
} else if (this.isAuthMemberAccess(defaultValue)) {
470+
} else if (this.isAuthMemberAccess(expr)) {
466471
return {
467-
authMember: this.getMemberAccessChain(defaultValue),
472+
authMember: this.getMemberAccessChain(expr),
468473
};
469474
} else {
470-
throw new Error(`Unsupported default value type for field ${field.name}`);
475+
throw new Error(`Unsupported default value type for ${expr.$type}`);
471476
}
472477
}
473478

0 commit comments

Comments
 (0)