Skip to content

Commit 0db73b7

Browse files
committed
add class for composite types
1 parent 9015de6 commit 0db73b7

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

src/server/templates/dart.ts

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,98 @@ ${this.operations.indexOf('Update') !== -1 ? this.generateUpdateDeclaration() :
568568
}
569569
}
570570

571+
class ClassDartConstructForCompositeType implements DartType, Declarable {
572+
postgresType: PostgresType
573+
ptdMap: PostgresToDartMap
574+
name: string
575+
576+
constructor(postgresType: PostgresType, ptdMap: PostgresToDartMap) {
577+
this.postgresType = postgresType
578+
this.ptdMap = ptdMap
579+
this.name = `${formatForDartClassName(this.postgresType.schema)}${formatForDartClassName(this.postgresType.name)}`
580+
}
581+
582+
generateType(): string {
583+
return this.name
584+
}
585+
586+
generateDeclaration(): string {
587+
return `class ${this.name} implements JsonSerializable {${this.postgresType.attributes
588+
.map(
589+
(attr) => `
590+
final ${new NullDartType(this.ptdMap[attr.type_id][1]).generateType()} ${formatForDartPropertyName(attr.name)};`
591+
)
592+
.join('')}
593+
594+
const ${this.name}({${this.postgresType.attributes
595+
.map((attr) => {
596+
return `
597+
this.${formatForDartPropertyName(attr.name)}`
598+
})
599+
.join(',')}
600+
});
601+
602+
static Map<String, dynamic> _generateMap({${this.postgresType.attributes
603+
.map((attr) => {
604+
return `
605+
${new NullDartType(this.ptdMap[attr.type_id][1]).generateType()} ${formatForDartPropertyName(attr.name)}`
606+
})
607+
.join(',')}
608+
}) => {${this.postgresType.attributes
609+
.map((attr) => {
610+
return `
611+
if (${formatForDartPropertyName(attr.name)} != null) '${attr.name}': ${formatForDartPropertyName(attr.name)}${this.ptdMap[attr.type_id][1].generateJsonEncoding()}`
612+
})
613+
.join(',')}
614+
};
615+
616+
@override
617+
Map<String, dynamic> toJson() => _generateMap(${this.postgresType.attributes
618+
.map((attr) => {
619+
return `
620+
${formatForDartPropertyName(attr.name)}: ${formatForDartPropertyName(attr.name)}`
621+
})
622+
.join(',')}
623+
);
624+
625+
${this.name} copyWith({${this.postgresType.attributes
626+
.map((attr) => {
627+
return `
628+
${new NullDartType(this.ptdMap[attr.type_id][1]).generateType()} ${formatForDartPropertyName(attr.name)}`
629+
})
630+
.join(',')}
631+
}) {
632+
return ${this.name}(${this.postgresType.attributes
633+
.map((attr) => {
634+
return `
635+
${formatForDartPropertyName(attr.name)}: ${formatForDartPropertyName(attr.name)} ?? this.${formatForDartPropertyName(attr.name)}`
636+
})
637+
.join(',')}
638+
);
639+
}
640+
641+
@override
642+
factory ${this.name}.fromJson(Map<String, dynamic> jsonObject) {
643+
return ${this.name}(${this.postgresType.attributes
644+
.map((attr) => {
645+
return `
646+
${formatForDartPropertyName(attr.name)}: ${new NullDartType(this.ptdMap[attr.type_id][1]).generateJsonDecoding(`jsonObject['${attr.name}']`)}`
647+
})
648+
.join(',')}
649+
);
650+
}
651+
}`
652+
}
653+
654+
generateJsonEncoding(): string {
655+
return '.toJson()'
656+
}
657+
658+
generateJsonDecoding(inputParameter: string): string {
659+
return `${this.name}.fromJson(${inputParameter})`
660+
}
661+
}
662+
571663
type PostgresToDartMap = Record<number | string, [PostgresType | undefined, DartType]>
572664

573665
const PGTYPE_TO_DARTTYPE_MAP: Record<string, DartType> = {
@@ -653,6 +745,12 @@ function buildDartTypeFromPostgresType(
653745
return postgresType.name.startsWith('_') ? new ListDartType(enumConstruct) : enumConstruct
654746
}
655747

748+
// Composite type
749+
if (postgresType.attributes.length > 0) {
750+
const compositeType = new ClassDartConstructForCompositeType(postgresType, ptdMap)
751+
return postgresType.name.startsWith('_') ? new ListDartType(compositeType) : compositeType
752+
}
753+
656754
return new BuiltinDartType('dynamic')
657755
}
658756

@@ -815,7 +913,10 @@ export const apply = ({ schemas, tables, views, columns, types }: GeneratorMetad
815913
const newDartType = buildDartTypeFromPostgresType(t, ptdMap)
816914
ptdMap[t.id] = [t, newDartType]
817915
ptdMap[t.name] = [t, newDartType]
818-
if (newDartType instanceof EnumDartConstruct) {
916+
if (
917+
newDartType instanceof EnumDartConstruct ||
918+
newDartType instanceof ClassDartConstructForCompositeType
919+
) {
819920
declarableTypes.push(newDartType)
820921
}
821922
}

0 commit comments

Comments
 (0)