Skip to content

Commit 720b2a0

Browse files
feat(typegen): go: format
1 parent dafec1f commit 720b2a0

File tree

1 file changed

+76
-21
lines changed
  • src/server/templates

1 file changed

+76
-21
lines changed

src/server/templates/go.ts

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,48 @@ export const apply = ({
3232
let output = `
3333
package database
3434
35-
${tables.map((table) => generateTableStruct(schemas.find((schema) => schema.name === table.schema)!, table, columnsByTableId[table.id], types)).join('\n\n')}
35+
${tables
36+
.map((table) =>
37+
generateTableStruct(
38+
schemas.find((schema) => schema.name === table.schema)!,
39+
table,
40+
columnsByTableId[table.id],
41+
types
42+
)
43+
)
44+
.join('\n\n')}
3645
37-
${views.map((view) => generateTableStruct(schemas.find((schema) => schema.name === view.schema)!, view, columnsByTableId[view.id], types)).join('\n\n')}
46+
${views
47+
.map((view) =>
48+
generateTableStruct(
49+
schemas.find((schema) => schema.name === view.schema)!,
50+
view,
51+
columnsByTableId[view.id],
52+
types
53+
)
54+
)
55+
.join('\n\n')}
3856
39-
${materializedViews.map((materializedView) => generateTableStruct(schemas.find((schema) => schema.name === materializedView.schema)!, materializedView, columnsByTableId[materializedView.id], types)).join('\n\n')}
57+
${materializedViews
58+
.map((materializedView) =>
59+
generateTableStruct(
60+
schemas.find((schema) => schema.name === materializedView.schema)!,
61+
materializedView,
62+
columnsByTableId[materializedView.id],
63+
types
64+
)
65+
)
66+
.join('\n\n')}
4067
41-
${compositeTypes.map((compositeType) => generateCompositeTypeStruct(schemas.find((schema) => schema.name === compositeType.schema)!, compositeType, types)).join('\n\n')}
68+
${compositeTypes
69+
.map((compositeType) =>
70+
generateCompositeTypeStruct(
71+
schemas.find((schema) => schema.name === compositeType.schema)!,
72+
compositeType,
73+
types
74+
)
75+
)
76+
.join('\n\n')}
4277
`.trim()
4378

4479
return output
@@ -62,7 +97,12 @@ function formatForGoTypeName(name: string): string {
6297
.join('')
6398
}
6499

65-
function generateTableStruct(schema: PostgresSchema, table: PostgresTable | PostgresView | PostgresMaterializedView, columns: PostgresColumn[], types: PostgresType[]): string {
100+
function generateTableStruct(
101+
schema: PostgresSchema,
102+
table: PostgresTable | PostgresView | PostgresMaterializedView,
103+
columns: PostgresColumn[],
104+
types: PostgresType[]
105+
): string {
66106
// Storing columns as a tuple of [formattedName, type, name] rather than creating the string
67107
// representation of the line allows us to pre-format the entries. Go formats
68108
// struct fields to be aligned, e.g.:
@@ -77,25 +117,33 @@ function generateTableStruct(schema: PostgresSchema, table: PostgresTable | Post
77117
column.name,
78118
])
79119

80-
const [maxFormattedNameLength, maxTypeLength] = columnEntries.reduce(([maxFormattedName, maxType], [formattedName, type]) => {
81-
return [Math.max(maxFormattedName, formattedName.length), Math.max(maxType, type.length)]
82-
}, [0, 0])
120+
const [maxFormattedNameLength, maxTypeLength] = columnEntries.reduce(
121+
([maxFormattedName, maxType], [formattedName, type]) => {
122+
return [Math.max(maxFormattedName, formattedName.length), Math.max(maxType, type.length)]
123+
},
124+
[0, 0]
125+
)
83126

84127
// Pad the formatted name and type to align the struct fields, then join
85128
// create the final string representation of the struct fields.
86129
const formattedColumnEntries = columnEntries.map(([formattedName, type, name]) => {
87-
return ` ${formattedName.padEnd(maxFormattedNameLength)} ${type.padEnd(maxTypeLength)} \`json:"${name}"\``
130+
return ` ${formattedName.padEnd(maxFormattedNameLength)} ${type.padEnd(
131+
maxTypeLength
132+
)} \`json:"${name}"\``
88133
})
89134

90-
91135
return `
92136
type ${formatForGoTypeName(schema.name)}${formatForGoTypeName(table.name)} struct {
93137
${formattedColumnEntries.join('\n')}
94138
}
95139
`.trim()
96140
}
97141

98-
function generateCompositeTypeStruct(schema: PostgresSchema, type: PostgresType, types: PostgresType[]): string {
142+
function generateCompositeTypeStruct(
143+
schema: PostgresSchema,
144+
type: PostgresType,
145+
types: PostgresType[]
146+
): string {
99147
// Use the type_id of the attributes to find the types of the attributes
100148
const typeWithRetrievedAttributes = {
101149
...type,
@@ -105,22 +153,29 @@ function generateCompositeTypeStruct(schema: PostgresSchema, type: PostgresType,
105153
...attribute,
106154
type,
107155
}
108-
})
156+
}),
109157
}
110-
const attributeEntries: [string, string, string][] = typeWithRetrievedAttributes.attributes.map((attribute) => [
111-
formatForGoTypeName(attribute.name),
112-
pgTypeToGoType(attribute.type!.format),
113-
attribute.name,
114-
])
158+
const attributeEntries: [string, string, string][] = typeWithRetrievedAttributes.attributes.map(
159+
(attribute) => [
160+
formatForGoTypeName(attribute.name),
161+
pgTypeToGoType(attribute.type!.format),
162+
attribute.name,
163+
]
164+
)
115165

116-
const [maxFormattedNameLength, maxTypeLength] = attributeEntries.reduce(([maxFormattedName, maxType], [formattedName, type]) => {
117-
return [Math.max(maxFormattedName, formattedName.length), Math.max(maxType, type.length)]
118-
}, [0, 0])
166+
const [maxFormattedNameLength, maxTypeLength] = attributeEntries.reduce(
167+
([maxFormattedName, maxType], [formattedName, type]) => {
168+
return [Math.max(maxFormattedName, formattedName.length), Math.max(maxType, type.length)]
169+
},
170+
[0, 0]
171+
)
119172

120173
// Pad the formatted name and type to align the struct fields, then join
121174
// create the final string representation of the struct fields.
122175
const formattedAttributeEntries = attributeEntries.map(([formattedName, type, name]) => {
123-
return ` ${formattedName.padEnd(maxFormattedNameLength)} ${type.padEnd(maxTypeLength)} \`json:"${name}"\``
176+
return ` ${formattedName.padEnd(maxFormattedNameLength)} ${type.padEnd(
177+
maxTypeLength
178+
)} \`json:"${name}"\``
124179
})
125180

126181
return `

0 commit comments

Comments
 (0)