Skip to content

Commit

Permalink
refactor: adjust code for runtime enums
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBlomberg committed Mar 17, 2024
1 parent 73d81a2 commit 3a5fc56
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 60 deletions.
5 changes: 4 additions & 1 deletion src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export class Generator {

const serializer =
options.serializer ??
new Serializer({ typeOnlyImports: options.typeOnlyImports, camelCase: !!options.camelCase });
new Serializer({
camelCase: !!options.camelCase,
typeOnlyImports: options.typeOnlyImports,
});
const data = serializer.serialize(nodes);

const relativeOutDir = options.outFile
Expand Down
13 changes: 1 addition & 12 deletions src/serializer/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ export const testSerializer = () => {
new ColumnMetadata({
comment: 'Hello!\nThis is a comment.',
dataType: 'json',
hasDefaultValue: false,
isAutoIncrementing: false,
isNullable: false,
name: 'json',
}),
],
Expand All @@ -291,7 +288,6 @@ export const testSerializer = () => {
],
enums,
),
runtimeEnums: false,
});

strictEqual(
Expand Down Expand Up @@ -338,21 +334,14 @@ export const testSerializer = () => {
[
new TableMetadata({
columns: [
new ColumnMetadata({
dataType: 'json',
hasDefaultValue: false,
isAutoIncrementing: false,
isNullable: false,
name: 'json',
}),
new ColumnMetadata({ dataType: 'json', name: 'json' }),
],
name: 'foo',
schema: 'public',
}),
],
enums,
),
runtimeEnums: false,
});

strictEqual(
Expand Down
78 changes: 40 additions & 38 deletions src/serializer/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import { toCamelCase } from '../transformer';
const IDENTIFIER_REGEXP = /^[$A-Z_a-z][\w$]*$/;

export type SerializerOptions = {
typeOnlyImports?: boolean;
camelCase?: boolean;
typeOnlyImports?: boolean;
};

/**
* Creates a TypeScript output string from a codegen AST.
*/
export class Serializer {
readonly typeOnlyImports: boolean;
readonly camelCase: boolean;
readonly typeOnlyImports: boolean;

constructor(options: SerializerOptions = {}) {
this.typeOnlyImports = options.typeOnlyImports ?? true;
this.camelCase = options.camelCase ?? false;
this.typeOnlyImports = options.typeOnlyImports ?? true;
}

serialize(nodes: StatementNode[]) {
Expand Down Expand Up @@ -70,41 +70,6 @@ export class Serializer {
return data;
}

serializeRuntimeEnum(node: RuntimeEnumDeclarationNode) {
let data = 'enum ';
data += node.name;
data += ' {\n';

const args =
node.body.type === NodeType.UNION_EXPRESSION
? node.body.args
: [node.body];
const sortedArgs = args.sort((a, b) =>
(a as LiteralNode<string>).value.localeCompare(
(b as LiteralNode<string>).value,
),
);

for (const arg of sortedArgs) {
if (arg.type === NodeType.LITERAL && typeof arg.value === 'string') {
const serializedArg = this.serializeLiteral(arg);
const enumValueName = this.camelCase
? toCamelCase(arg.value)
: arg.value;
data += ' ';
data += enumValueName;
data += ' = ';
data += serializedArg;
data += ',';
data += '\n';
}
}

data += '}';

return data;
}

serializeAliasDeclaration(node: AliasDeclarationNode) {
const expression =
node.body.type === NodeType.TEMPLATE ? node.body.expression : node.body;
Expand Down Expand Up @@ -360,6 +325,43 @@ export class Serializer {
return data;
}

serializeRuntimeEnum(node: RuntimeEnumDeclarationNode) {
let data = 'enum ';

data += node.name;
data += ' {\n';

const args =
node.body.type === NodeType.UNION_EXPRESSION
? node.body.args
: [node.body];

args.sort((a, b) => {
return (a as LiteralNode<string>).value.localeCompare(
(b as LiteralNode<string>).value,
);
});

for (const arg of args) {
if (arg.type === NodeType.LITERAL && typeof arg.value === 'string') {
const serializedArg = this.serializeLiteral(arg);
const enumValueName = this.camelCase
? toCamelCase(arg.value)
: arg.value;
data += ' ';
data += enumValueName;
data += ' = ';
data += serializedArg;
data += ',';
data += '\n';
}
}

data += '}';

return data;
}

serializeUnionExpression(node: UnionExpressionNode) {
let data = '';
let i = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/transformer/symbol-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export type SymbolNameMap = {

export type SymbolNode =
| { node: ExpressionNode | TemplateNode; type: SymbolType.DEFINITION }
| { node: ModuleReferenceNode; type: SymbolType.MODULE_REFERENCE }
| { node: ExpressionNode; type: SymbolType.RUNTIME_ENUM_DEFINITION }
| { node: ModuleReferenceNode; type: SymbolType.MODULE_REFERENCE }
| { type: SymbolType.TABLE };

export const enum SymbolType {
DEFINITION = 'Definition',
RUNTIME_ENUM_DEFINITION = 'RuntimeEnumDefinition',
MODULE_REFERENCE = 'ModuleReference',
RUNTIME_ENUM_DEFINITION = 'RuntimeEnumDefinition',
TABLE = 'Table',
}

Expand Down
14 changes: 7 additions & 7 deletions src/transformer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export type TransformContext = {
};

export type TransformOptions = {
camelCase: boolean;
camelCase?: boolean;
defaultSchema?: string;
dialect: Dialect;
metadata: DatabaseMetadata;
runtimeEnums: boolean;
runtimeEnums?: boolean;
};

/**
Expand Down Expand Up @@ -146,7 +146,7 @@ export class Transformer {

#createContext(options: TransformOptions): TransformContext {
return {
camelCase: options.camelCase,
camelCase: !!options.camelCase,
defaultScalar:
options.dialect.adapter.defaultScalar ?? new IdentifierNode('unknown'),
defaultSchema:
Expand All @@ -161,7 +161,7 @@ export class Transformer {
...options.dialect.adapter.imports,
},
metadata: options.metadata,
runtimeEnums: options.runtimeEnums,
runtimeEnums: !!options.runtimeEnums,
scalars: {
...options.dialect.adapter.scalars,
},
Expand Down Expand Up @@ -201,9 +201,9 @@ export class Transformer {
}
}

return runtimeEnumDefinitionNodes.sort((a, b) =>
a.argument.name.localeCompare(b.argument.name),
);
return runtimeEnumDefinitionNodes.sort((a, b) => {
return a.argument.name.localeCompare(b.argument.name);
});
}

#createDefinitionNodes(context: TransformContext) {
Expand Down

0 comments on commit 3a5fc56

Please sign in to comment.