Skip to content

Commit 202f650

Browse files
committed
refactor(scripts/typegen): replace local Zod serializer with guts/zod
Delete the hand-rolled zod.go serializer and use zod.AsSchemas from github.com/coder/guts/zod (PR coder/guts#85) instead. The mutation rewrites the guts AST to Zod v4 schema nodes, then SerializeInOrder emits only the wanted types in dependency order. guts v1.7.0 -> v1.7.1-0.20260529230818-2f30faf483eb (feat/zod-mutation)
1 parent b3fc81d commit 202f650

8 files changed

Lines changed: 68 additions & 562 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ all: gen build fmt lint test
66

77
gen: src/codersdk.gen.ts
88

9-
src/codersdk.gen.ts: scripts/typegen/main.go scripts/typegen/zod.go scripts/typegen/go.mod
9+
src/codersdk.gen.ts: scripts/typegen/main.go scripts/typegen/go.mod
1010
@cd scripts/typegen && go run . > ../../src/codersdk.gen.ts.tmp
1111
@mv src/codersdk.gen.ts.tmp src/codersdk.gen.ts
1212
@bun run format -- src/codersdk.gen.ts || true

dist/index.js

Lines changed: 12 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/typegen/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/buger/jsonparser v1.1.2 // indirect
1212
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1313
github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666 // indirect
14-
github.com/coder/guts v1.7.0 // indirect
14+
github.com/coder/guts v1.7.1-0.20260529230818-2f30faf483eb // indirect
1515
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
1616
github.com/coder/serpent v0.15.0 // indirect
1717
github.com/coder/websocket v1.8.14 // indirect

scripts/typegen/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666 h1:imJX12Fi
1616
github.com/coder/coder/v2 v2.34.0-rc.0.0.20260528065010-cfa343e45666/go.mod h1:5UG9p30Gqu3UoUz0fo18JzD5DBQj2S9ArDtbI8yhIr8=
1717
github.com/coder/guts v1.7.0 h1:TaZ/PR9wgN8dlbcckaWV1MxkkuEFZRwSRwBBEm8dYXs=
1818
github.com/coder/guts v1.7.0/go.mod h1:30SShdvpmsauNlsNjECRB5AppScjYk08rf2ZVpH3MFg=
19+
github.com/coder/guts v1.7.1-0.20260529230818-2f30faf483eb h1:MjlXdlmJwVf24NGrdE+2/spUKJlgNeAmNAx7RtobwoI=
20+
github.com/coder/guts v1.7.1-0.20260529230818-2f30faf483eb/go.mod h1:VAC7GjXGIoM747tMmabVQHTzb/ZtAQxGaFCiZE9g/C4=
1921
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
2022
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
2123
github.com/coder/serpent v0.15.0 h1:jobR7DnPsxzEMD0cRiailwlY+4v6HAPS/8emIgBpaIU=

scripts/typegen/main.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"maps"
1313
"os"
1414
"slices"
15-
"strings"
1615

1716
"github.com/coder/guts"
1817
"github.com/coder/guts/bindings"
1918
"github.com/coder/guts/config"
19+
"github.com/coder/guts/zod"
2020
)
2121

2222
// Types the action needs from the Coder API. Only root types that
@@ -63,38 +63,46 @@ func main() {
6363
log.Fatalf("to typescript: %v", err)
6464
}
6565

66+
// Pre-Zod mutations that reshape enums and optional fields.
6667
ts.ApplyMutations(
6768
config.EnumAsTypes,
6869
config.SimplifyOmitEmpty,
6970
)
7071

71-
// Collect all nodes then filter to wanted types plus any
72-
// types they reference that are also in the full set.
72+
// Compute wanted types and dependency order before Zod
73+
// rewrites the AST (collectRefs works on Interface/Alias).
7374
allNodes := make(map[string]bindings.Node)
7475
ts.ForEach(func(name string, node bindings.Node) {
7576
allNodes[name] = node
7677
})
77-
78-
// Resolve transitive references so we don't emit broken
79-
// schema references.
8078
included := resolveTransitive(allNodes, wantedTypes)
79+
order := topoSort(included)
8180

82-
// Topological sort: emit dependencies before dependents.
83-
names := topoSort(included)
84-
85-
var b strings.Builder
86-
b.WriteString("// Code generated by 'make gen'. DO NOT EDIT.\n")
87-
b.WriteString("import { z } from \"zod\";\n\n")
81+
// Rewrite Interface/Alias into Zod schemas, then export.
82+
ts.ApplyMutations(
83+
zod.AsSchemas,
84+
config.ExportTypes,
85+
)
8886

89-
for _, name := range names {
90-
s := serializeNode(name, included[name])
91-
if s != "" {
92-
b.WriteString(s)
93-
b.WriteString("\n")
87+
output, err := ts.SerializeInOrder(func(nodes map[string]bindings.Node) []bindings.Node {
88+
var result []bindings.Node
89+
for _, name := range order {
90+
// AsSchemas creates FooSchema (VariableStatement) and
91+
// Foo (Alias with z.infer<typeof FooSchema>).
92+
if schema, ok := nodes[name+"Schema"]; ok {
93+
result = append(result, schema)
94+
}
95+
if typeNode, ok := nodes[name]; ok {
96+
result = append(result, typeNode)
97+
}
9498
}
99+
return result
100+
})
101+
if err != nil {
102+
log.Fatalf("serialize: %v", err)
95103
}
96104

97-
_, _ = fmt.Fprint(os.Stdout, b.String())
105+
_, _ = fmt.Fprint(os.Stdout, output)
98106
}
99107

100108
// resolveTransitive starts from the seeds and pulls in any

0 commit comments

Comments
 (0)