forked from google/go-dap
/
gentypes.go
638 lines (564 loc) · 18.9 KB
/
gentypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// gentypes generates Go types from debugProtocol.json
//
// Usage:
//
// $ gentypes <path to debugProtocol.json>
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"unicode"
)
var (
uFlag = flag.Bool("u", false, "updates the debugProtocol.json file before generating the code")
oFlag = flag.String("o", "", "specifies the output file name. If unspecified, outputs to stdout")
)
// parseRef parses the value of a "$ref" key.
// For example "#definitions/ProtocolMessage" => "ProtocolMessage".
func parseRef(refValue interface{}) string {
refContents := refValue.(string)
if !strings.HasPrefix(refContents, "#/definitions/") {
log.Fatal("want ref to start with '#/definitions/', got ", refValue)
}
return replaceGoTypename(refContents[14:])
}
// goFieldName converts a property name from its JSON representation to an
// exported Go field name.
// For example "__some_property_name" => "SomePropertyName".
func goFieldName(jsonPropName string) string {
clean := strings.ReplaceAll(jsonPropName, "_", " ")
titled := strings.Title(clean)
return strings.ReplaceAll(titled, " ", "")
}
// parsePropertyType takes the JSON value of a property field and extracts
// the Go type of the property. For example, given this map:
//
// {
// "type": "string",
// "description": "The command to execute."
// },
//
// It will emit "string".
func parsePropertyType(propValue map[string]interface{}) string {
if ref, ok := propValue["$ref"]; ok {
return parseRef(ref)
}
if _, ok := propValue["oneOf"]; ok {
return "interface{}"
}
propType, ok := propValue["type"]
if !ok {
log.Fatal("property with no type or ref:", propValue)
}
switch propType.(type) {
case string:
switch propType {
case "string":
return "string"
case "number":
return "int"
case "integer":
return "int"
case "boolean":
return "bool"
case "array":
propItems, ok := propValue["items"]
if !ok {
log.Fatal("missing items type for property of array type:", propValue)
}
propItemsMap := propItems.(map[string]interface{})
return "[]" + parsePropertyType(propItemsMap)
case "object":
// When the type of a property is "object", we'll emit a map with a string
// key and a value type that depends on the type of the
// additionalProperties field.
additionalProps, ok := propValue["additionalProperties"]
if !ok {
log.Fatal("missing additionalProperties field when type=object:", propValue)
}
valueType := parsePropertyType(additionalProps.(map[string]interface{}))
return fmt.Sprintf("map[string]%v", valueType)
case "interface{}":
return "interface{}"
default:
log.Fatalf("unknown property type value %v in %v", propType, propValue)
}
case []interface{}:
return "interface{}"
default:
log.Fatal("unknown property type", propType)
}
panic("unreachable")
}
// maybeParseInheritance helps parse types that inherit from other types.
// A type description can have an "allOf" key, which means it inherits from
// another type description. Returns the name of the base type specified in
// allOf, and the description of the inheriting type.
//
// Example:
//
// "allOf": [ { "$ref": "#/definitions/ProtocolMessage" },
// {... type description ...} ]
//
// Returns base type ProtocolMessage and a map representing type description.
// If there is no "allOf", returns an empty baseTypeName and descMap itself.
func maybeParseInheritance(descMap map[string]json.RawMessage) (baseTypeName string, typeDescJson map[string]json.RawMessage) {
allOfListJson, ok := descMap["allOf"]
if !ok {
return "", descMap
}
var allOfSliceOfJson []json.RawMessage
if err := json.Unmarshal(allOfListJson, &allOfSliceOfJson); err != nil {
log.Fatal(err)
}
if len(allOfSliceOfJson) != 2 {
log.Fatal("want 2 elements in allOf list, got", allOfSliceOfJson)
}
var baseTypeRef map[string]interface{}
if err := json.Unmarshal(allOfSliceOfJson[0], &baseTypeRef); err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(allOfSliceOfJson[1], &typeDescJson); err != nil {
log.Fatal(err)
}
return parseRef(baseTypeRef["$ref"]), typeDescJson
}
// emitToplevelType emits a single type into a string. It takes the type name
// and a serialized json object representing the type. The json representation
// will have fields: "type", "properties" etc.
func emitToplevelType(typeName string, descJson json.RawMessage) string {
var b strings.Builder
var baseType string
// We don't parse the description all the way to map[string]interface{}
// because we have to retain the original JSON-order of properties (in this
// type as well as any nested types like "body").
var descMap map[string]json.RawMessage
if err := json.Unmarshal(descJson, &descMap); err != nil {
log.Fatal(err)
}
baseType, descMap = maybeParseInheritance(descMap)
typeJson, ok := descMap["type"]
if !ok {
log.Fatal("want description to have 'type', got ", descMap)
}
var descTypeString string
if err := json.Unmarshal(typeJson, &descTypeString); err != nil {
log.Fatal(err)
}
var comment string
descriptionJson, ok := descMap["description"]
if ok {
if err := json.Unmarshal(descriptionJson, &comment); err != nil {
log.Fatal(err)
}
}
if len(comment) > 0 {
comment = commentOutEachLine(fmt.Sprintf("%s: %s", typeName, comment))
fmt.Fprint(&b, comment)
}
if descTypeString == "string" {
fmt.Fprintf(&b, "type %s string\n", typeName)
return b.String()
} else if descTypeString == "object" {
fmt.Fprintf(&b, "type %s struct {\n", typeName)
if len(baseType) > 0 {
fmt.Fprintf(&b, "\t%s\n\n", baseType)
}
} else {
log.Fatal("want description type to be object or string, got ", descTypeString)
}
var propsMapOfJson map[string]json.RawMessage
if propsJson, ok := descMap["properties"]; ok {
if err := json.Unmarshal(propsJson, &propsMapOfJson); err != nil {
log.Fatal(err)
}
} else {
b.WriteString("}\n")
return b.String()
}
propsNamesInOrder, err := keysInOrder(descMap["properties"])
if err != nil {
log.Fatal(err)
}
// Stores the properties that are required.
requiredMap := make(map[string]bool)
if requiredJson, ok := descMap["required"]; ok {
var required []interface{}
if err := json.Unmarshal(requiredJson, &required); err != nil {
log.Fatal(err)
}
for _, r := range required {
requiredMap[r.(string)] = true
}
}
// Some types will have a "body" which should be emitted as a separate type.
// Since we can't emit a whole new Go type while in the middle of emitting
// another type, we save it for later and emit it after the current type is
// done.
bodyType := ""
for _, propName := range propsNamesInOrder {
// The JSON schema is designed for the TypeScript type system, where a
// subclass can redefine a field in a superclass with a refined type (such
// as specific values for a field). To ensure we emit Go structs that can
// be unmarshaled from JSON messages properly, we must limit each field
// to appear only once in hierarchical types.
if propName == "type" && (typeName == "Request" || typeName == "Response" || typeName == "Event") {
continue
}
if propName == "command" && typeName != "Request" && typeName != "Response" {
continue
}
if propName == "event" && typeName != "Event" {
continue
}
if propName == "arguments" && typeName == "Request" {
continue
}
var propDesc map[string]interface{}
if err := json.Unmarshal(propsMapOfJson[propName], &propDesc); err != nil {
log.Fatal(err)
}
if propName == "body" {
if typeName == "Response" || typeName == "Event" {
continue
}
var bodyTypeName string
if ref, ok := propDesc["$ref"]; ok {
bodyTypeName = parseRef(ref)
} else {
bodyTypeName = typeName + "Body"
bodyType = emitToplevelType(bodyTypeName, propsMapOfJson["body"])
}
if requiredMap["body"] {
fmt.Fprintf(&b, "\t%s %s `json:\"body\"`\n", "Body", bodyTypeName)
} else {
fmt.Fprintf(&b, "\t%s %s `json:\"body,omitempty\"`\n", "Body", bodyTypeName)
}
} else if propName == "arguments" && (typeName == "LaunchRequest" || typeName == "AttachRequest") {
// Special case for LaunchRequest or AttachRequest arguments, which are implementation
// defined and don't have pre-set field names in the specification.
fmt.Fprintln(&b, "\tArguments json.RawMessage `json:\"arguments\"`")
} else {
// Go type of this property.
goType := parsePropertyType(propDesc)
jsonTag := fmt.Sprintf("`json:\"%s", propName)
if requiredMap[propName] {
jsonTag += "\"`"
} else if typeName == "ContinueResponseBody" && propName == "allThreadsContinued" {
// This one special field must not have the omitempty tag, despite being
// optional. If this attribute is missing the client will (according to
// the specification) assume a value of 'true' for backward
// compatibility. See: https://github.com/google/go-dap/issues/39
jsonTag += "\"`"
} else if typeName == "InitializeRequestArguments" && (propName == "linesStartAt1" || propName == "columnsStartAt1") {
// These two special fields must not have the omitempty tag, despite being
// optional. If this attribute is missing the server will (according to
// the specification) assume a value of 'true'.
jsonTag += "\"`"
} else if typeName == "ErrorMessage" && propName == "showUser" {
// For launch/attach errors, vscode will treat omitted values the same way as true,
// so to supress visible reporting, we must report false explicitly.
jsonTag += "\"`"
} else {
jsonTag += ",omitempty\"`"
}
fmt.Fprintf(&b, "\t%s %s %s\n", goFieldName(propName), goType, jsonTag)
}
}
b.WriteString("}\n")
if len(bodyType) > 0 {
b.WriteString("\n")
b.WriteString(bodyType)
}
return b.String()
}
// keysInOrder returns the keys in json object in b, in their original order.
// Based on https://github.com/golang/go/issues/27179#issuecomment-415559968
func keysInOrder(b []byte) ([]string, error) {
d := json.NewDecoder(bytes.NewReader(b))
t, err := d.Token()
if err != nil {
return nil, err
}
if t != json.Delim('{') {
return nil, errors.New("expected start of object")
}
var keys []string
for {
t, err := d.Token()
if err != nil {
return nil, err
}
if t == json.Delim('}') {
return keys, nil
}
keys = append(keys, t.(string))
if err := skipValue(d); err != nil {
return nil, err
}
}
}
// replaceGoTypename replaces conflicting type names in the JSON schema with
// proper Go type names.
func replaceGoTypename(typeName string) string {
// Since we have a top-level interface named Message, we replace the DAP
// message type Message with ErrorMessage.
if typeName == "Message" {
return "ErrorMessage"
}
return typeName
}
var errEnd = errors.New("invalid end of array or object")
func skipValue(d *json.Decoder) error {
t, err := d.Token()
if err != nil {
return err
}
switch t {
case json.Delim('['), json.Delim('{'):
for {
if err := skipValue(d); err != nil {
if err == errEnd {
break
}
return err
}
}
case json.Delim(']'), json.Delim('}'):
return errEnd
}
return nil
}
// commentOutEachLine returns s such that a Go comment marker ("//") is
// prepended to each line.
func commentOutEachLine(s string) string {
parts := strings.Split(s, "\n")
var sb strings.Builder
for _, p := range parts {
fmt.Fprintf(&sb, "// %s\n", p)
}
return sb.String()
}
// emitMethodsForType may emit methods for typeName into sb.
func emitMethodsForType(sb *strings.Builder, typeName string) {
if typeName == "ProtocolMessage" {
fmt.Fprintf(sb, "func (m *%s) GetSeq() int {return m.Seq}\n", typeName)
}
if strings.HasSuffix(typeName, "Request") && typeName != "Request" {
fmt.Fprintf(sb, "func (r *%s) GetRequest() *Request {return &r.Request}\n", typeName)
}
if strings.HasSuffix(typeName, "Response") && typeName != "Response" {
fmt.Fprintf(sb, "func (r *%s) GetResponse() *Response {return &r.Response}\n", typeName)
}
if strings.HasSuffix(typeName, "Event") && typeName != "Event" {
fmt.Fprintf(sb, "func (e *%s) GetEvent() *Event {return &e.Event}\n", typeName)
}
if typeName == "LaunchRequest" || typeName == "AttachRequest" {
fmt.Fprintf(sb, "func (r *%s) GetArguments() json.RawMessage { return r.Arguments }\n", typeName)
}
}
func emitCtor(sb *strings.Builder, reqs, resps, events []string) {
fmt.Fprint(sb, `
// Mapping of request commands and corresponding struct constructors that
// can be passed to json.Unmarshal.
var requestCtor = map[string]messageCtor{`)
for _, r := range reqs {
req := strings.TrimSuffix(firstToLower(r), "Request")
var msg string
if req == "initialize" {
msg = `
Arguments: InitializeRequestArguments{
// Set the default values specified here: https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Initialize.
LinesStartAt1: true,
ColumnsStartAt1: true,
PathFormat: "path",
},
`
}
fmt.Fprintf(sb, "\n\t\"%s\":\tfunc() Message { return &%s{%s} },", req, r, msg)
}
fmt.Fprint(sb, "\n}")
fmt.Fprint(sb, `
// Mapping of response commands and corresponding struct constructors that
// can be passed to json.Unmarshal.
var responseCtor = map[string]messageCtor{`)
for _, r := range resps {
resp := strings.TrimSuffix(firstToLower(r), "Response")
fmt.Fprintf(sb, "\n\t\"%s\":\tfunc() Message { return &%s{} },", resp, r)
}
fmt.Fprint(sb, "\n}")
fmt.Fprint(sb, `
// Mapping of event ids and corresponding struct constructors that
// can be passed to json.Unmarshal.
var eventCtor = map[string]messageCtor{`)
for _, e := range events {
ev := strings.TrimSuffix(firstToLower(e), "Event")
fmt.Fprintf(sb, "\n\t\"%s\":\tfunc() Message { return &%s{} },", ev, e)
}
fmt.Fprint(sb, "\n}\n")
}
func firstToLower(s string) string {
r := []rune(s)
return string(unicode.ToLower(r[0])) + string(r[1:])
}
const preamble = `// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by "cmd/gentypes/gentypes.go"; DO NOT EDIT.
// DAP spec: https://microsoft.github.io/debug-adapter-protocol/specification
// See cmd/gentypes/README.md for additional details.
package dap
import "encoding/json"
// Message is an interface that all DAP message types implement with pointer
// receivers. It's not part of the protocol but is used to enforce static
// typing in Go code and provide some common accessors.
//
// Note: the DAP type "Message" (which is used in the body of ErrorResponse)
// is renamed to ErrorMessage to avoid collision with this interface.
type Message interface {
GetSeq() int
}
// RequestMessage is an interface implemented by all Request-types.
type RequestMessage interface {
Message
// GetRequest provides access to the embedded Request.
GetRequest() *Request
}
// ResponseMessage is an interface implemented by all Response-types.
type ResponseMessage interface {
Message
// GetResponse provides access to the embedded Response.
GetResponse() *Response
}
// EventMessage is an interface implemented by all Event-types.
type EventMessage interface {
Message
// GetEvent provides access to the embedded Event.
GetEvent() *Event
}
// LaunchAttachRequest is an interface implemented by
// LaunchRequest and AttachRequest as they contain shared
// implementation specific arguments that are not part of
// the specification.
type LaunchAttachRequest interface {
RequestMessage
// GetArguments provides access to the Arguments map.
GetArguments() json.RawMessage
}
`
// typesExcludeList is an exclude list of type names we don't want to emit.
var typesExcludeList = map[string]bool{
// LaunchRequest and AttachRequest arguments can be arbitrary maps.
// Therefore, this type is not used anywhere.
"LaunchRequestArguments": true,
"AttachRequestArguments": true,
}
func main() {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "Path to the DAP specification json file is required.")
fmt.Fprintln(os.Stderr, "gentypes <path/to/debugProtocol.json>")
os.Exit(1)
}
inputFilename := flag.Arg(0)
if *uFlag {
if err := updateInput(inputFilename); err != nil {
log.Fatalf("Failed to update the input file: %v", err)
}
}
inputData, err := ioutil.ReadFile(inputFilename)
if err != nil {
log.Fatal(err)
}
var m map[string]json.RawMessage
if err := json.Unmarshal(inputData, &m); err != nil {
log.Fatal(err)
}
var typeMap map[string]json.RawMessage
if err := json.Unmarshal(m["definitions"], &typeMap); err != nil {
log.Fatal(err)
}
var b strings.Builder
b.WriteString(preamble)
typeNames, err := keysInOrder(m["definitions"])
if err != nil {
log.Fatal(err)
}
var requests, responses, events []string
for _, typeName := range typeNames {
if _, ok := typesExcludeList[typeName]; !ok {
b.WriteString(emitToplevelType(replaceGoTypename(typeName), typeMap[typeName]))
b.WriteString("\n")
}
emitMethodsForType(&b, replaceGoTypename(typeName))
// Add the typename to the appropriate list.
if strings.HasSuffix(typeName, "Request") && typeName != "Request" {
requests = append(requests, typeName)
}
if strings.HasSuffix(typeName, "Response") && typeName != "Response" && typeName != "ErrorResponse" {
responses = append(responses, typeName)
}
if strings.HasSuffix(typeName, "Event") && typeName != "Event" {
events = append(events, typeName)
}
}
// Emit the maps from id to response and event types.
emitCtor(&b, requests, responses, events)
wholeFile := []byte(b.String())
formatted, err := format.Source(wholeFile)
if err != nil {
log.Fatal(err)
}
if *oFlag == "" {
fmt.Print(string(formatted))
} else {
if err := ioutil.WriteFile(*oFlag, formatted, 0644); err != nil {
log.Fatalf("Failed to write the generated file: %v", err)
}
}
}
func updateInput(inputFilename string) error {
resp, err := http.Get("https://raw.githubusercontent.com/microsoft/vscode-debugadapter-node/main/debugProtocol.json")
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return ioutil.WriteFile(inputFilename, data, 0644)
}