Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { parseGoSchemas } from './go-struct-parser.ts'
4
5const file = (text: string) => [{ name: 'test.go', text }]
6
7describe('go struct parser', () => {
8 it('maps primitives, pointers and omitempty-derived requiredness', () => {
9 const { schemas, warnings } = parseGoSchemas(
10 file(`
11type Req struct {
12 Model string \`json:"model"\`
13 Temperature *float32 \`json:"temperature,omitempty"\`
14 MaxTokens *int \`json:"max_tokens,omitempty"\`
15 Stream bool \`json:"stream"\`
16}
17`),
18 ['Req'],
19 )
20 expect(warnings).toEqual([])
21 expect(schemas.Req).toEqual({
22 type: 'object',
23 properties: {
24 model: { type: 'string' },
25 // A pointer is not itself optional — omitempty is the signal.
26 temperature: { type: 'number' },
27 max_tokens: { type: 'integer' },
28 stream: { type: 'boolean' },
29 },
30 required: ['model', 'stream'],
31 })
32 })
33
34 it('turns named string types plus their const block into enums', () => {
35 const { schemas } = parseGoSchemas(
36 file(`
37type ThinkingType string
38
39const (
40 ThinkingTypeEnabled ThinkingType = "enabled"
41 ThinkingTypeDisabled ThinkingType = "disabled"
42 ThinkingTypeAuto ThinkingType = "auto"
43)
44
45type Thinking struct {
46 Type ThinkingType \`json:"type"\`
47}
48`),
49 ['Thinking'],
50 )
51 expect(schemas.Thinking).toMatchObject({
52 properties: { type: { $ref: '#/components/schemas/ThinkingType' } },
53 })
54 expect(schemas.ThinkingType).toEqual({
55 type: 'string',
56 enum: ['enabled', 'disabled', 'auto'],
57 })
58 })
59
60 it('handles slices, maps, interface{} and []byte', () => {
61 const { schemas } = parseGoSchemas(
62 file(`
63type Req struct {
64 Messages []*Message \`json:"messages"\`
65 Stop []string \`json:"stop,omitempty"\`
66 LogitBias map[string]int \`json:"logit_bias,omitempty"\`
67 Anything interface{} \`json:"anything,omitempty"\`
68 Raw []byte \`json:"raw,omitempty"\`
69}
70
71type Message struct {
72 Role string \`json:"role"\`
73}
74`),
75 ['Req'],
76 )
77 expect(schemas.Req).toMatchObject({
78 properties: {
79 messages: {
80 type: 'array',
81 items: { $ref: '#/components/schemas/Message' },
82 },
83 stop: { type: 'array', items: { type: 'string' } },
84 logit_bias: {
85 type: 'object',
86 additionalProperties: { type: 'integer' },
87 },
88 // interface{} carries no constraint rather than a wrong one.
89 anything: {},
90 // []byte marshals to a base64 string, not an array.
91 raw: { type: 'string' },
92 },
93 })
94 expect(schemas.Message).toBeDefined()
95 })
96
97 it('skips json:"-" and untagged embedded fields', () => {
98 const { schemas } = parseGoSchemas(
99 file(`
100type ExtraBody map[string]interface{}
101
102type Req struct {
103 Model string \`json:"model"\`
104 ExtraBody \`json:"-"\`
105 HttpHeader
106}
107`),
108 ['Req'],
109 )
110 expect(
111 Object.keys((schemas.Req as { properties: object }).properties),
112 ).toEqual(['model'])
113 })
114
115 it('resolves aliases and named composite types', () => {
116 const { schemas } = parseGoSchemas(
117 file(`
118type FunctionDefine = FunctionDefinition
119
120type FunctionDefinition struct {
121 Name string \`json:"name"\`
122}
123
124type Extra map[string]string
125
126type Req struct {
127 Fn FunctionDefine \`json:"fn"\`
128 Extra Extra \`json:"extra,omitempty"\`
129}
130`),
131 ['Req'],
132 )
133 expect(schemas.Req).toMatchObject({
134 properties: {
135 fn: { $ref: '#/components/schemas/FunctionDefinition' },
136 extra: { type: 'object', additionalProperties: { type: 'string' } },
137 },
138 })
139 })
140
141 it('lifts doc comments into descriptions', () => {
142 const { schemas } = parseGoSchemas(
143 file(`
144// Req opens a generation task.
145type Req struct {
146 // Whether to burn a watermark into the output.
147 Watermark *bool \`json:"watermark,omitempty"\`
148}
149`),
150 ['Req'],
151 )
152 expect(schemas.Req).toMatchObject({
153 description: 'Req opens a generation task.',
154 properties: {
155 watermark: {
156 type: 'boolean',
157 description: 'Whether to burn a watermark into the output.',
158 },
159 },
160 })
161 })
162
163 it('emits only the closure reachable from the roots', () => {
164 const { schemas } = parseGoSchemas(
165 file(`
166type Wanted struct {
167 Child Child \`json:"child"\`
168}
169
170type Child struct {
171 Name string \`json:"name"\`
172}
173
174type Unrelated struct {
175 Nope string \`json:"nope"\`
176}
177`),
178 ['Wanted'],
179 )
180 expect(Object.keys(schemas).sort()).toEqual(['Child', 'Wanted'])
181 })
182
183 it('warns rather than throwing on unmapped types and missing roots', () => {
184 const { schemas, warnings } = parseGoSchemas(
185 file(`
186type Req struct {
187 When somepkg.Timestamp \`json:"when"\`
188}
189`),
190 ['Req', 'DoesNotExist'],
191 )
192 expect(schemas.Req).toMatchObject({ properties: { when: {} } })
193 expect(warnings).toContain(
194 "go-parser: Req.when: unmapped type 'somepkg.Timestamp'",
195 )
196 expect(warnings).toContain("go-parser: root type 'DoesNotExist' not found")
197 })
198})