♻️ Quality: internal/structured without smells (3 → 0)
- typeSchema: parentheses unwrapped first, arraySchema, mapSchema and unsupported; typeString as a one-variable switch - document.go: the root, the $defs and the descriptions Checked: Schema and Instructions on 12 sources (every supported type, nested parentheses, each refusal) byte-identical old vs new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
b315334 parent: 75a8140 added
internal/structured/document.go +104 -0 | new file mode 100644 | ||
| @@ -0,0 +1,104 @@ | ||
| 1 | +package structured | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "fmt" | |
| 5 | + "go/ast" | |
| 6 | + "strings" | |
| 7 | +) | |
| 8 | + | |
| 9 | +// document builds the whole schema: the root's own schema at the top level — | |
| 10 | +// a struct root is inlined rather than referenced, the plainest shape for a | |
| 11 | +// server's grammar converter — plus `$defs` for every type the root reaches. | |
| 12 | +func (c *compiler) document(root *ast.ValueSpec) (map[string]any, error) { | |
| 13 | + rootType := root.Type | |
| 14 | + if rootType == nil { | |
| 15 | + rootType = rootLiteral(root).Type | |
| 16 | + } | |
| 17 | + schema, err := c.rootSchema(rootType) | |
| 18 | + if err != nil { | |
| 19 | + return nil, err | |
| 20 | + } | |
| 21 | + if d := describe(root.Doc, root.Comment); d != "" { | |
| 22 | + schema["description"] = d | |
| 23 | + } | |
| 24 | + defs, err := c.definitions() | |
| 25 | + if err != nil { | |
| 26 | + return nil, err | |
| 27 | + } | |
| 28 | + if len(defs) > 0 { | |
| 29 | + schema["$defs"] = defs | |
| 30 | + } | |
| 31 | + return schema, nil | |
| 32 | +} | |
| 33 | + | |
| 34 | +// rootSchema inlines a declared root type, any other type is translated as is. | |
| 35 | +func (c *compiler) rootSchema(expr ast.Expr) (map[string]any, error) { | |
| 36 | + if ident, ok := expr.(*ast.Ident); ok && c.types[ident.Name] != nil { | |
| 37 | + return c.declared(c.types[ident.Name]) | |
| 38 | + } | |
| 39 | + return c.typeSchema(expr) | |
| 40 | +} | |
| 41 | + | |
| 42 | +// definitions translates every type the root reaches, in declaration order. | |
| 43 | +// Translating one may reach others, hence the loop until nothing new is used. | |
| 44 | +func (c *compiler) definitions() (map[string]any, error) { | |
| 45 | + defs := map[string]any{} | |
| 46 | + for done := false; !done; { | |
| 47 | + done = true | |
| 48 | + for _, name := range c.order { | |
| 49 | + if !c.used[name] || defs[name] != nil { | |
| 50 | + continue | |
| 51 | + } | |
| 52 | + schema, err := c.declared(c.types[name]) | |
| 53 | + if err != nil { | |
| 54 | + return nil, err | |
| 55 | + } | |
| 56 | + defs[name] = schema | |
| 57 | + done = false | |
| 58 | + } | |
| 59 | + } | |
| 60 | + return defs, nil | |
| 61 | +} | |
| 62 | + | |
| 63 | +// declared translates one type declaration, with its doc as description. | |
| 64 | +func (c *compiler) declared(spec *ast.TypeSpec) (map[string]any, error) { | |
| 65 | + schema, err := c.typeSchema(spec.Type) | |
| 66 | + if err != nil { | |
| 67 | + return nil, err | |
| 68 | + } | |
| 69 | + if d := describe(spec.Doc, spec.Comment); d != "" { | |
| 70 | + schema["description"] = d | |
| 71 | + } | |
| 72 | + return schema, nil | |
| 73 | +} | |
| 74 | + | |
| 75 | +// describe joins the comments written above and beside a declaration. | |
| 76 | +func describe(groups ...*ast.CommentGroup) string { | |
| 77 | + var parts []string | |
| 78 | + for _, g := range groups { | |
| 79 | + if text := strings.TrimSpace(g.Text()); text != "" { | |
| 80 | + parts = append(parts, strings.Join(strings.Fields(text), " ")) | |
| 81 | + } | |
| 82 | + } | |
| 83 | + return strings.Join(parts, " ") | |
| 84 | +} | |
| 85 | + | |
| 86 | +// shiftLines takes the header's line back off a parser error, so that its | |
| 87 | +// positions count from the first line of the YAML block. A parser error | |
| 88 | +// reads "structuredOutput:5:3: expected …" (several joined by newlines). | |
| 89 | +func shiftLines(msg string) string { | |
| 90 | + lines := strings.Split(msg, "\n") | |
| 91 | + for i, line := range lines { | |
| 92 | + var row, col int | |
| 93 | + var rest string | |
| 94 | + prefix := sourceName + ":" | |
| 95 | + if !strings.HasPrefix(line, prefix) { | |
| 96 | + continue | |
| 97 | + } | |
| 98 | + if n, _ := fmt.Sscanf(line[len(prefix):], "%d:%d:", &row, &col); n == 2 { | |
| 99 | + _, rest, _ = strings.Cut(line[len(prefix):], ": ") | |
| 100 | + lines[i] = fmt.Sprintf("%s%d:%d: %s", prefix, row-1, col, rest) | |
| 101 | + } | |
| 102 | + } | |
| 103 | + return strings.Join(lines, "\n") | |
| 104 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | +package structured | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "go/ast" | ||
| 6 | + "strings" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +// document builds the whole schema: the root's own schema at the top level — | ||
| 10 | +// a struct root is inlined rather than referenced, the plainest shape for a | ||
| 11 | +// server's grammar converter — plus `$defs` for every type the root reaches. | ||
| 12 | +func (c *compiler) document(root *ast.ValueSpec) (map[string]any, error) { | ||
| 13 | + rootType := root.Type | ||
| 14 | + if rootType == nil { | ||
| 15 | + rootType = rootLiteral(root).Type | ||
| 16 | + } | ||
| 17 | + schema, err := c.rootSchema(rootType) | ||
| 18 | + if err != nil { | ||
| 19 | + return nil, err | ||
| 20 | + } | ||
| 21 | + if d := describe(root.Doc, root.Comment); d != "" { | ||
| 22 | + schema["description"] = d | ||
| 23 | + } | ||
| 24 | + defs, err := c.definitions() | ||
| 25 | + if err != nil { | ||
| 26 | + return nil, err | ||
| 27 | + } | ||
| 28 | + if len(defs) > 0 { | ||
| 29 | + schema["$defs"] = defs | ||
| 30 | + } | ||
| 31 | + return schema, nil | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +// rootSchema inlines a declared root type, any other type is translated as is. | ||
| 35 | +func (c *compiler) rootSchema(expr ast.Expr) (map[string]any, error) { | ||
| 36 | + if ident, ok := expr.(*ast.Ident); ok && c.types[ident.Name] != nil { | ||
| 37 | + return c.declared(c.types[ident.Name]) | ||
| 38 | + } | ||
| 39 | + return c.typeSchema(expr) | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +// definitions translates every type the root reaches, in declaration order. | ||
| 43 | +// Translating one may reach others, hence the loop until nothing new is used. | ||
| 44 | +func (c *compiler) definitions() (map[string]any, error) { | ||
| 45 | + defs := map[string]any{} | ||
| 46 | + for done := false; !done; { | ||
| 47 | + done = true | ||
| 48 | + for _, name := range c.order { | ||
| 49 | + if !c.used[name] || defs[name] != nil { | ||
| 50 | + continue | ||
| 51 | + } | ||
| 52 | + schema, err := c.declared(c.types[name]) | ||
| 53 | + if err != nil { | ||
| 54 | + return nil, err | ||
| 55 | + } | ||
| 56 | + defs[name] = schema | ||
| 57 | + done = false | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + return defs, nil | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +// declared translates one type declaration, with its doc as description. | ||
| 64 | +func (c *compiler) declared(spec *ast.TypeSpec) (map[string]any, error) { | ||
| 65 | + schema, err := c.typeSchema(spec.Type) | ||
| 66 | + if err != nil { | ||
| 67 | + return nil, err | ||
| 68 | + } | ||
| 69 | + if d := describe(spec.Doc, spec.Comment); d != "" { | ||
| 70 | + schema["description"] = d | ||
| 71 | + } | ||
| 72 | + return schema, nil | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +// describe joins the comments written above and beside a declaration. | ||
| 76 | +func describe(groups ...*ast.CommentGroup) string { | ||
| 77 | + var parts []string | ||
| 78 | + for _, g := range groups { | ||
| 79 | + if text := strings.TrimSpace(g.Text()); text != "" { | ||
| 80 | + parts = append(parts, strings.Join(strings.Fields(text), " ")) | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + return strings.Join(parts, " ") | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +// shiftLines takes the header's line back off a parser error, so that its | ||
| 87 | +// positions count from the first line of the YAML block. A parser error | ||
| 88 | +// reads "structuredOutput:5:3: expected …" (several joined by newlines). | ||
| 89 | +func shiftLines(msg string) string { | ||
| 90 | + lines := strings.Split(msg, "\n") | ||
| 91 | + for i, line := range lines { | ||
| 92 | + var row, col int | ||
| 93 | + var rest string | ||
| 94 | + prefix := sourceName + ":" | ||
| 95 | + if !strings.HasPrefix(line, prefix) { | ||
| 96 | + continue | ||
| 97 | + } | ||
| 98 | + if n, _ := fmt.Sscanf(line[len(prefix):], "%d:%d:", &row, &col); n == 2 { | ||
| 99 | + _, rest, _ = strings.Cut(line[len(prefix):], ": ") | ||
| 100 | + lines[i] = fmt.Sprintf("%s%d:%d: %s", prefix, row-1, col, rest) | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + return strings.Join(lines, "\n") | ||
| 104 | +} | ||
modified
internal/structured/structured.go +0 -97 | @@ -149,103 +149,6 @@ func rootLiteral(spec *ast.ValueSpec) *ast.CompositeLit { | ||
| 149 | 149 | return lit |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | -// document builds the whole schema: the root's own schema at the top level — | |
| 153 | -// a struct root is inlined rather than referenced, the plainest shape for a | |
| 154 | -// server's grammar converter — plus `$defs` for every type the root reaches. | |
| 155 | -func (c *compiler) document(root *ast.ValueSpec) (map[string]any, error) { | |
| 156 | - rootType := root.Type | |
| 157 | - if rootType == nil { | |
| 158 | - rootType = rootLiteral(root).Type | |
| 159 | - } | |
| 160 | - schema, err := c.rootSchema(rootType) | |
| 161 | - if err != nil { | |
| 162 | - return nil, err | |
| 163 | - } | |
| 164 | - if d := describe(root.Doc, root.Comment); d != "" { | |
| 165 | - schema["description"] = d | |
| 166 | - } | |
| 167 | - defs, err := c.definitions() | |
| 168 | - if err != nil { | |
| 169 | - return nil, err | |
| 170 | - } | |
| 171 | - if len(defs) > 0 { | |
| 172 | - schema["$defs"] = defs | |
| 173 | - } | |
| 174 | - return schema, nil | |
| 175 | -} | |
| 176 | - | |
| 177 | -// rootSchema inlines a declared root type, any other type is translated as is. | |
| 178 | -func (c *compiler) rootSchema(expr ast.Expr) (map[string]any, error) { | |
| 179 | - if ident, ok := expr.(*ast.Ident); ok && c.types[ident.Name] != nil { | |
| 180 | - return c.declared(c.types[ident.Name]) | |
| 181 | - } | |
| 182 | - return c.typeSchema(expr) | |
| 183 | -} | |
| 184 | - | |
| 185 | -// definitions translates every type the root reaches, in declaration order. | |
| 186 | -// Translating one may reach others, hence the loop until nothing new is used. | |
| 187 | -func (c *compiler) definitions() (map[string]any, error) { | |
| 188 | - defs := map[string]any{} | |
| 189 | - for done := false; !done; { | |
| 190 | - done = true | |
| 191 | - for _, name := range c.order { | |
| 192 | - if !c.used[name] || defs[name] != nil { | |
| 193 | - continue | |
| 194 | - } | |
| 195 | - schema, err := c.declared(c.types[name]) | |
| 196 | - if err != nil { | |
| 197 | - return nil, err | |
| 198 | - } | |
| 199 | - defs[name] = schema | |
| 200 | - done = false | |
| 201 | - } | |
| 202 | - } | |
| 203 | - return defs, nil | |
| 204 | -} | |
| 205 | - | |
| 206 | -// declared translates one type declaration, with its doc as description. | |
| 207 | -func (c *compiler) declared(spec *ast.TypeSpec) (map[string]any, error) { | |
| 208 | - schema, err := c.typeSchema(spec.Type) | |
| 209 | - if err != nil { | |
| 210 | - return nil, err | |
| 211 | - } | |
| 212 | - if d := describe(spec.Doc, spec.Comment); d != "" { | |
| 213 | - schema["description"] = d | |
| 214 | - } | |
| 215 | - return schema, nil | |
| 216 | -} | |
| 217 | - | |
| 218 | -// describe joins the comments written above and beside a declaration. | |
| 219 | -func describe(groups ...*ast.CommentGroup) string { | |
| 220 | - var parts []string | |
| 221 | - for _, g := range groups { | |
| 222 | - if text := strings.TrimSpace(g.Text()); text != "" { | |
| 223 | - parts = append(parts, strings.Join(strings.Fields(text), " ")) | |
| 224 | - } | |
| 225 | - } | |
| 226 | - return strings.Join(parts, " ") | |
| 227 | -} | |
| 228 | - | |
| 229 | -// shiftLines takes the header's line back off a parser error, so that its | |
| 230 | -// positions count from the first line of the YAML block. A parser error | |
| 231 | -// reads "structuredOutput:5:3: expected …" (several joined by newlines). | |
| 232 | -func shiftLines(msg string) string { | |
| 233 | - lines := strings.Split(msg, "\n") | |
| 234 | - for i, line := range lines { | |
| 235 | - var row, col int | |
| 236 | - var rest string | |
| 237 | - prefix := sourceName + ":" | |
| 238 | - if !strings.HasPrefix(line, prefix) { | |
| 239 | - continue | |
| 240 | - } | |
| 241 | - if n, _ := fmt.Sscanf(line[len(prefix):], "%d:%d:", &row, &col); n == 2 { | |
| 242 | - _, rest, _ = strings.Cut(line[len(prefix):], ": ") | |
| 243 | - lines[i] = fmt.Sprintf("%s%d:%d: %s", prefix, row-1, col, rest) | |
| 244 | - } | |
| 245 | - } | |
| 246 | - return strings.Join(lines, "\n") | |
| 247 | -} | |
| 248 | - | |
| 249 | 152 | // Instructions is the paragraph appended to the system prompt of an agent |
| 250 | 153 | // with a structuredOutput: the declarations themselves, comments included. |
| 251 | 154 | // |
| @@ -149,103 +149,6 @@ func rootLiteral(spec *ast.ValueSpec) *ast.CompositeLit { | |||
| 149 | return lit | 149 | return lit |
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | -// document builds the whole schema: the root's own schema at the top level — | ||
| 153 | -// a struct root is inlined rather than referenced, the plainest shape for a | ||
| 154 | -// server's grammar converter — plus `$defs` for every type the root reaches. | ||
| 155 | -func (c *compiler) document(root *ast.ValueSpec) (map[string]any, error) { | ||
| 156 | - rootType := root.Type | ||
| 157 | - if rootType == nil { | ||
| 158 | - rootType = rootLiteral(root).Type | ||
| 159 | - } | ||
| 160 | - schema, err := c.rootSchema(rootType) | ||
| 161 | - if err != nil { | ||
| 162 | - return nil, err | ||
| 163 | - } | ||
| 164 | - if d := describe(root.Doc, root.Comment); d != "" { | ||
| 165 | - schema["description"] = d | ||
| 166 | - } | ||
| 167 | - defs, err := c.definitions() | ||
| 168 | - if err != nil { | ||
| 169 | - return nil, err | ||
| 170 | - } | ||
| 171 | - if len(defs) > 0 { | ||
| 172 | - schema["$defs"] = defs | ||
| 173 | - } | ||
| 174 | - return schema, nil | ||
| 175 | -} | ||
| 176 | - | ||
| 177 | -// rootSchema inlines a declared root type, any other type is translated as is. | ||
| 178 | -func (c *compiler) rootSchema(expr ast.Expr) (map[string]any, error) { | ||
| 179 | - if ident, ok := expr.(*ast.Ident); ok && c.types[ident.Name] != nil { | ||
| 180 | - return c.declared(c.types[ident.Name]) | ||
| 181 | - } | ||
| 182 | - return c.typeSchema(expr) | ||
| 183 | -} | ||
| 184 | - | ||
| 185 | -// definitions translates every type the root reaches, in declaration order. | ||
| 186 | -// Translating one may reach others, hence the loop until nothing new is used. | ||
| 187 | -func (c *compiler) definitions() (map[string]any, error) { | ||
| 188 | - defs := map[string]any{} | ||
| 189 | - for done := false; !done; { | ||
| 190 | - done = true | ||
| 191 | - for _, name := range c.order { | ||
| 192 | - if !c.used[name] || defs[name] != nil { | ||
| 193 | - continue | ||
| 194 | - } | ||
| 195 | - schema, err := c.declared(c.types[name]) | ||
| 196 | - if err != nil { | ||
| 197 | - return nil, err | ||
| 198 | - } | ||
| 199 | - defs[name] = schema | ||
| 200 | - done = false | ||
| 201 | - } | ||
| 202 | - } | ||
| 203 | - return defs, nil | ||
| 204 | -} | ||
| 205 | - | ||
| 206 | -// declared translates one type declaration, with its doc as description. | ||
| 207 | -func (c *compiler) declared(spec *ast.TypeSpec) (map[string]any, error) { | ||
| 208 | - schema, err := c.typeSchema(spec.Type) | ||
| 209 | - if err != nil { | ||
| 210 | - return nil, err | ||
| 211 | - } | ||
| 212 | - if d := describe(spec.Doc, spec.Comment); d != "" { | ||
| 213 | - schema["description"] = d | ||
| 214 | - } | ||
| 215 | - return schema, nil | ||
| 216 | -} | ||
| 217 | - | ||
| 218 | -// describe joins the comments written above and beside a declaration. | ||
| 219 | -func describe(groups ...*ast.CommentGroup) string { | ||
| 220 | - var parts []string | ||
| 221 | - for _, g := range groups { | ||
| 222 | - if text := strings.TrimSpace(g.Text()); text != "" { | ||
| 223 | - parts = append(parts, strings.Join(strings.Fields(text), " ")) | ||
| 224 | - } | ||
| 225 | - } | ||
| 226 | - return strings.Join(parts, " ") | ||
| 227 | -} | ||
| 228 | - | ||
| 229 | -// shiftLines takes the header's line back off a parser error, so that its | ||
| 230 | -// positions count from the first line of the YAML block. A parser error | ||
| 231 | -// reads "structuredOutput:5:3: expected …" (several joined by newlines). | ||
| 232 | -func shiftLines(msg string) string { | ||
| 233 | - lines := strings.Split(msg, "\n") | ||
| 234 | - for i, line := range lines { | ||
| 235 | - var row, col int | ||
| 236 | - var rest string | ||
| 237 | - prefix := sourceName + ":" | ||
| 238 | - if !strings.HasPrefix(line, prefix) { | ||
| 239 | - continue | ||
| 240 | - } | ||
| 241 | - if n, _ := fmt.Sscanf(line[len(prefix):], "%d:%d:", &row, &col); n == 2 { | ||
| 242 | - _, rest, _ = strings.Cut(line[len(prefix):], ": ") | ||
| 243 | - lines[i] = fmt.Sprintf("%s%d:%d: %s", prefix, row-1, col, rest) | ||
| 244 | - } | ||
| 245 | - } | ||
| 246 | - return strings.Join(lines, "\n") | ||
| 247 | -} | ||
| 248 | - | ||
| 249 | // Instructions is the paragraph appended to the system prompt of an agent | 152 | // Instructions is the paragraph appended to the system prompt of an agent |
| 250 | // with a structuredOutput: the declarations themselves, comments included. | 153 | // with a structuredOutput: the declarations themselves, comments included. |
| 251 | // | 154 | // |
modified
internal/structured/types.go +39 -20 | @@ -27,27 +27,45 @@ var basicTypes = map[string]string{ | ||
| 27 | 27 | // struct { … } → object (see structSchema) |
| 28 | 28 | // *T → T, optional (see field) |
| 29 | 29 | func (c *compiler) typeSchema(expr ast.Expr) (map[string]any, error) { |
| 30 | + for paren, ok := expr.(*ast.ParenExpr); ok; paren, ok = expr.(*ast.ParenExpr) { | |
| 31 | + expr = paren.X | |
| 32 | + } | |
| 30 | 33 | switch t := expr.(type) { |
| 31 | 34 | case *ast.Ident: |
| 32 | 35 | return c.named(t) |
| 33 | 36 | case *ast.ArrayType: |
| 34 | - if t.Len != nil { | |
| 35 | - return nil, c.errorf(t, "fixed-size arrays are not supported, use a slice") | |
| 36 | - } | |
| 37 | - return c.container(map[string]any{"type": "array"}, "items", t.Elt) | |
| 37 | + return c.arraySchema(t) | |
| 38 | 38 | case *ast.MapType: |
| 39 | - if key, ok := t.Key.(*ast.Ident); !ok || key.Name != "string" { | |
| 40 | - return nil, c.errorf(t, "map keys must be string: JSON object keys are strings") | |
| 41 | - } | |
| 42 | - return c.container(map[string]any{"type": "object"}, "additionalProperties", t.Value) | |
| 39 | + return c.mapSchema(t) | |
| 43 | 40 | case *ast.StructType: |
| 44 | 41 | return c.structSchema(t) |
| 45 | - case *ast.StarExpr: | |
| 46 | - return nil, c.errorf(t, "a pointer is only allowed as a struct field's type, to make it optional") | |
| 47 | - case *ast.ParenExpr: | |
| 48 | - return c.typeSchema(t.X) | |
| 49 | 42 | } |
| 50 | - return nil, c.errorf(expr, "unsupported type %s", typeString(expr)) | |
| 43 | + return nil, c.unsupported(expr) | |
| 44 | +} | |
| 45 | + | |
| 46 | +// arraySchema translates a slice; a fixed-size array is refused. | |
| 47 | +func (c *compiler) arraySchema(t *ast.ArrayType) (map[string]any, error) { | |
| 48 | + if t.Len != nil { | |
| 49 | + return nil, c.errorf(t, "fixed-size arrays are not supported, use a slice") | |
| 50 | + } | |
| 51 | + return c.container(map[string]any{"type": "array"}, "items", t.Elt) | |
| 52 | +} | |
| 53 | + | |
| 54 | +// mapSchema translates a map with string keys, the only keys JSON has. | |
| 55 | +func (c *compiler) mapSchema(t *ast.MapType) (map[string]any, error) { | |
| 56 | + if key, ok := t.Key.(*ast.Ident); !ok || key.Name != "string" { | |
| 57 | + return nil, c.errorf(t, "map keys must be string: JSON object keys are strings") | |
| 58 | + } | |
| 59 | + return c.container(map[string]any{"type": "object"}, "additionalProperties", t.Value) | |
| 60 | +} | |
| 61 | + | |
| 62 | +// unsupported says why a type expression has no schema: a pointer outside a | |
| 63 | +// struct field, or a type JSON cannot carry. | |
| 64 | +func (c *compiler) unsupported(expr ast.Expr) error { | |
| 65 | + if star, ok := expr.(*ast.StarExpr); ok { | |
| 66 | + return c.errorf(star, "a pointer is only allowed as a struct field's type, to make it optional") | |
| 67 | + } | |
| 68 | + return c.errorf(expr, "unsupported type %s", typeString(expr)) | |
| 51 | 69 | } |
| 52 | 70 | |
| 53 | 71 | // named translates a type name: a predeclared type or a declared one. |
| @@ -165,19 +183,20 @@ func jsonTag(tag *ast.BasicLit) (name string, omitEmpty, skip bool) { | ||
| 165 | 183 | |
| 166 | 184 | // typeString names an unsupported type in an error message. |
| 167 | 185 | func typeString(expr ast.Expr) string { |
| 186 | + name := "(expression)" | |
| 168 | 187 | switch t := expr.(type) { |
| 169 | 188 | case *ast.SelectorExpr: |
| 170 | - return typeString(t.X) + "." + t.Sel.Name | |
| 189 | + name = typeString(t.X) + "." + t.Sel.Name | |
| 171 | 190 | case *ast.Ident: |
| 172 | - return t.Name | |
| 191 | + name = t.Name | |
| 173 | 192 | case *ast.InterfaceType: |
| 174 | - return "interface" | |
| 193 | + name = "interface" | |
| 175 | 194 | case *ast.ChanType: |
| 176 | - return "chan" | |
| 195 | + name = "chan" | |
| 177 | 196 | case *ast.FuncType: |
| 178 | - return "func" | |
| 197 | + name = "func" | |
| 179 | 198 | case *ast.IndexExpr, *ast.IndexListExpr: |
| 180 | - return "(generic instantiation)" | |
| 199 | + name = "(generic instantiation)" | |
| 181 | 200 | } |
| 182 | - return "(expression)" | |
| 201 | + return name | |
| 183 | 202 | } |
| @@ -27,27 +27,45 @@ var basicTypes = map[string]string{ | |||
| 27 | // struct { … } → object (see structSchema) | 27 | // struct { … } → object (see structSchema) |
| 28 | // *T → T, optional (see field) | 28 | // *T → T, optional (see field) |
| 29 | func (c *compiler) typeSchema(expr ast.Expr) (map[string]any, error) { | 29 | func (c *compiler) typeSchema(expr ast.Expr) (map[string]any, error) { |
| 30 | + for paren, ok := expr.(*ast.ParenExpr); ok; paren, ok = expr.(*ast.ParenExpr) { | ||
| 31 | + expr = paren.X | ||
| 32 | + } | ||
| 30 | switch t := expr.(type) { | 33 | switch t := expr.(type) { |
| 31 | case *ast.Ident: | 34 | case *ast.Ident: |
| 32 | return c.named(t) | 35 | return c.named(t) |
| 33 | case *ast.ArrayType: | 36 | case *ast.ArrayType: |
| 34 | - if t.Len != nil { | 37 | + return c.arraySchema(t) |
| 35 | - return nil, c.errorf(t, "fixed-size arrays are not supported, use a slice") | ||
| 36 | - } | ||
| 37 | - return c.container(map[string]any{"type": "array"}, "items", t.Elt) | ||
| 38 | case *ast.MapType: | 38 | case *ast.MapType: |
| 39 | - if key, ok := t.Key.(*ast.Ident); !ok || key.Name != "string" { | 39 | + return c.mapSchema(t) |
| 40 | - return nil, c.errorf(t, "map keys must be string: JSON object keys are strings") | ||
| 41 | - } | ||
| 42 | - return c.container(map[string]any{"type": "object"}, "additionalProperties", t.Value) | ||
| 43 | case *ast.StructType: | 40 | case *ast.StructType: |
| 44 | return c.structSchema(t) | 41 | return c.structSchema(t) |
| 45 | - case *ast.StarExpr: | ||
| 46 | - return nil, c.errorf(t, "a pointer is only allowed as a struct field's type, to make it optional") | ||
| 47 | - case *ast.ParenExpr: | ||
| 48 | - return c.typeSchema(t.X) | ||
| 49 | } | 42 | } |
| 50 | - return nil, c.errorf(expr, "unsupported type %s", typeString(expr)) | 43 | + return nil, c.unsupported(expr) |
| 44 | +} | ||
| 45 | + | ||
| 46 | +// arraySchema translates a slice; a fixed-size array is refused. | ||
| 47 | +func (c *compiler) arraySchema(t *ast.ArrayType) (map[string]any, error) { | ||
| 48 | + if t.Len != nil { | ||
| 49 | + return nil, c.errorf(t, "fixed-size arrays are not supported, use a slice") | ||
| 50 | + } | ||
| 51 | + return c.container(map[string]any{"type": "array"}, "items", t.Elt) | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +// mapSchema translates a map with string keys, the only keys JSON has. | ||
| 55 | +func (c *compiler) mapSchema(t *ast.MapType) (map[string]any, error) { | ||
| 56 | + if key, ok := t.Key.(*ast.Ident); !ok || key.Name != "string" { | ||
| 57 | + return nil, c.errorf(t, "map keys must be string: JSON object keys are strings") | ||
| 58 | + } | ||
| 59 | + return c.container(map[string]any{"type": "object"}, "additionalProperties", t.Value) | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +// unsupported says why a type expression has no schema: a pointer outside a | ||
| 63 | +// struct field, or a type JSON cannot carry. | ||
| 64 | +func (c *compiler) unsupported(expr ast.Expr) error { | ||
| 65 | + if star, ok := expr.(*ast.StarExpr); ok { | ||
| 66 | + return c.errorf(star, "a pointer is only allowed as a struct field's type, to make it optional") | ||
| 67 | + } | ||
| 68 | + return c.errorf(expr, "unsupported type %s", typeString(expr)) | ||
| 51 | } | 69 | } |
| 52 | 70 | ||
| 53 | // named translates a type name: a predeclared type or a declared one. | 71 | // named translates a type name: a predeclared type or a declared one. |
| @@ -165,19 +183,20 @@ func jsonTag(tag *ast.BasicLit) (name string, omitEmpty, skip bool) { | |||
| 165 | 183 | ||
| 166 | // typeString names an unsupported type in an error message. | 184 | // typeString names an unsupported type in an error message. |
| 167 | func typeString(expr ast.Expr) string { | 185 | func typeString(expr ast.Expr) string { |
| 186 | + name := "(expression)" | ||
| 168 | switch t := expr.(type) { | 187 | switch t := expr.(type) { |
| 169 | case *ast.SelectorExpr: | 188 | case *ast.SelectorExpr: |
| 170 | - return typeString(t.X) + "." + t.Sel.Name | 189 | + name = typeString(t.X) + "." + t.Sel.Name |
| 171 | case *ast.Ident: | 190 | case *ast.Ident: |
| 172 | - return t.Name | 191 | + name = t.Name |
| 173 | case *ast.InterfaceType: | 192 | case *ast.InterfaceType: |
| 174 | - return "interface" | 193 | + name = "interface" |
| 175 | case *ast.ChanType: | 194 | case *ast.ChanType: |
| 176 | - return "chan" | 195 | + name = "chan" |
| 177 | case *ast.FuncType: | 196 | case *ast.FuncType: |
| 178 | - return "func" | 197 | + name = "func" |
| 179 | case *ast.IndexExpr, *ast.IndexListExpr: | 198 | case *ast.IndexExpr, *ast.IndexListExpr: |
| 180 | - return "(generic instantiation)" | 199 | + name = "(generic instantiation)" |
| 181 | } | 200 | } |
| 182 | - return "(expression)" | 201 | + return name |
| 183 | } | 202 | } |