@@ -46,64 +46,71 @@ type config map[string]*typeConfig
46
46
47
47
// declarations configures the set of declarations to write.
48
48
//
49
- // Top level declarations are only created if they are configured with a
50
- // non-empty Name. Otherwise, they are discarded, though their fields may be
49
+ // Top level declarations are created unless configured with Name=="-",
50
+ // in which case they are discarded, though their fields may be
51
51
// extracted to types if they have a nested field configuration.
52
+ // If Name == "", the map key is used as the type name.
52
53
var declarations = config {
53
- "Annotations" : {Name : "Annotations" },
54
+ "Annotations" : {},
54
55
"CallToolRequest" : {
56
+ Name : "-" ,
55
57
Fields : config {"Params" : {Name : "CallToolParams" }},
56
58
},
57
- "CallToolResult" : {Name : "CallToolResult" },
59
+ "CallToolResult" : {},
58
60
"CancelledNotification" : {
61
+ Name : "-" ,
59
62
Fields : config {"Params" : {Name : "CancelledParams" }},
60
63
},
61
- "ClientCapabilities" : {Name : "ClientCapabilities" },
64
+ "ClientCapabilities" : {},
62
65
"GetPromptRequest" : {
66
+ Name : "-" ,
63
67
Fields : config {"Params" : {Name : "GetPromptParams" }},
64
68
},
65
- "GetPromptResult" : {Name : "GetPromptResult" },
66
- "Implementation" : {Name : "Implementation" },
69
+ "GetPromptResult" : {},
70
+ "Implementation" : {},
67
71
"InitializeRequest" : {
72
+ Name : "-" ,
68
73
Fields : config {"Params" : {Name : "InitializeParams" }},
69
74
},
70
- "InitializeResult" : {Name : "InitializeResult" },
75
+ "InitializeResult" : {},
71
76
"InitializedNotification" : {
77
+ Name : "-" ,
72
78
Fields : config {"Params" : {Name : "InitializedParams" }},
73
79
},
74
80
"ListPromptsRequest" : {
81
+ Name : "-" ,
75
82
Fields : config {"Params" : {Name : "ListPromptsParams" }},
76
83
},
77
- "ListPromptsResult" : {Name : "ListPromptsResult" },
84
+ "ListPromptsResult" : {},
78
85
"ListRootsRequest" : {
86
+ Name : "-" ,
79
87
Fields : config {"Params" : {Name : "ListRootsParams" }},
80
88
},
81
- "ListRootsResult" : {Name : "ListRootsResult" },
89
+ "ListRootsResult" : {},
82
90
"ListToolsRequest" : {
91
+ Name : "-" ,
83
92
Fields : config {"Params" : {Name : "ListToolsParams" }},
84
93
},
85
- "ListToolsResult" : {Name : "ListToolsResult" },
86
- "Prompt" : {Name : "Prompt" },
87
- "PromptMessage" : {Name : "PromptMessage" },
88
- "PromptArgument" : {Name : "PromptArgument" },
89
- "ProgressToken" : {Substitute : "any" }, // null|number|string
90
- "RequestId" : {Substitute : "any" }, // null|number|string
91
- "Role" : {Name : "Role" },
92
- "Root" : {Name : "Root" },
94
+ "ListToolsResult" : {},
95
+ "Prompt" : {},
96
+ "PromptMessage" : {},
97
+ "PromptArgument" : {},
98
+ "ProgressToken" : {Name : "-" , Substitute : "any" }, // null|number|string
99
+ "RequestId" : {Name : "-" , Substitute : "any" }, // null|number|string
100
+ "Role" : {},
101
+ "Root" : {},
93
102
94
103
"ServerCapabilities" : {
95
- Name : "ServerCapabilities" ,
96
104
Fields : config {
97
105
"Prompts" : {Name : "PromptCapabilities" },
98
106
"Resources" : {Name : "ResourceCapabilities" },
99
107
"Tools" : {Name : "ToolCapabilities" },
100
108
},
101
109
},
102
110
"Tool" : {
103
- Name : "Tool" ,
104
111
Fields : config {"InputSchema" : {Substitute : "*jsonschema.Schema" }},
105
112
},
106
- "ToolAnnotations" : {Name : "ToolAnnotations" },
113
+ "ToolAnnotations" : {},
107
114
}
108
115
109
116
func main () {
@@ -128,7 +135,7 @@ func main() {
128
135
if config == nil {
129
136
continue
130
137
}
131
- if err := writeDecl (* config , def , named ); err != nil {
138
+ if err := writeDecl (name , * config , def , named ); err != nil {
132
139
log .Fatal (err )
133
140
}
134
141
}
@@ -199,19 +206,22 @@ func loadSchema(schemaFile string) (data []byte, err error) {
199
206
return data , nil
200
207
}
201
208
202
- func writeDecl (config typeConfig , def * jsonschema.Schema , named map [string ]* bytes.Buffer ) error {
209
+ func writeDecl (configName string , config typeConfig , def * jsonschema.Schema , named map [string ]* bytes.Buffer ) error {
203
210
var w io.Writer = io .Discard
204
- if name := config .Name ; name != "" {
205
- if _ , ok := named [name ]; ok {
211
+ if typeName := config .Name ; typeName != "-" {
212
+ if typeName == "" {
213
+ typeName = configName
214
+ }
215
+ if _ , ok := named [typeName ]; ok {
206
216
return nil
207
217
}
208
218
buf := new (bytes.Buffer )
209
219
w = buf
210
- named [name ] = buf
220
+ named [typeName ] = buf
211
221
if def .Description != "" {
212
222
fmt .Fprintf (buf , "%s\n " , toComment (def .Description ))
213
223
}
214
- fmt .Fprintf (buf , "type %s " , name )
224
+ fmt .Fprintf (buf , "type %s " , typeName )
215
225
}
216
226
if err := writeType (w , & config , def , named ); err != nil {
217
227
return err // Better error here?
@@ -234,12 +244,12 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
234
244
// definition is missing, *but only if w is not io.Discard*. That's not a
235
245
// great API: see if we can do something more explicit than io.Discard.
236
246
if cfg , ok := declarations [name ]; ok {
237
- if cfg .Name == "" && cfg .Substitute == "" {
247
+ if cfg .Name == "- " && cfg .Substitute == "" {
238
248
panic (fmt .Sprintf ("referenced type %q cannot be referred to (no name or substitution)" , name ))
239
249
}
240
250
if cfg .Substitute != "" {
241
251
name = cfg .Substitute
242
- } else {
252
+ } else if cfg . Name != "" {
243
253
name = cfg .Name
244
254
}
245
255
}
@@ -311,14 +321,18 @@ func writeType(w io.Writer, config *typeConfig, def *jsonschema.Schema, named ma
311
321
if r .Substitute != "" {
312
322
fmt .Fprintf (w , r .Substitute )
313
323
} else {
314
- assert (r .Name != "" , "missing ExtractTo" )
315
- if err := writeDecl (* r , fieldDef , named ); err != nil {
324
+ assert (r .Name != "-" , "missing ExtractTo" )
325
+ typename := export
326
+ if r .Name != "" {
327
+ typename = r .Name
328
+ }
329
+ if err := writeDecl (typename , * r , fieldDef , named ); err != nil {
316
330
return err
317
331
}
318
332
if needPointer {
319
333
fmt .Fprintf (w , "*" )
320
334
}
321
- fmt .Fprintf (w , r . Name )
335
+ fmt .Fprintf (w , typename )
322
336
}
323
337
} else {
324
338
if needPointer {
@@ -410,7 +424,12 @@ func exportName(s string) string {
410
424
// at once, because the replacement will change the indices.)
411
425
for {
412
426
if loc := re .FindStringIndex (s ); loc != nil {
413
- s = s [:loc [0 ]] + replacement + s [loc [1 ]:]
427
+ // Don't replace the rune after the initialism, if any.
428
+ end := loc [1 ]
429
+ if end < len (s ) {
430
+ end --
431
+ }
432
+ s = s [:loc [0 ]] + replacement + s [end :]
414
433
} else {
415
434
break
416
435
}
@@ -421,9 +440,10 @@ func exportName(s string) string {
421
440
422
441
// Map from initialism to the regexp that matches it.
423
442
var initialisms = map [string ]* regexp.Regexp {
424
- "Id" : nil ,
425
- "Url" : nil ,
426
- "Uri" : nil ,
443
+ "Id" : nil ,
444
+ "Url" : nil ,
445
+ "Uri" : nil ,
446
+ "Mime" : nil ,
427
447
}
428
448
429
449
func init () {
0 commit comments