-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.go
1310 lines (1175 loc) · 31.5 KB
/
main.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The cdpgen command generates the package cdp from the provided protocol definitions.
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"
"github.com/mafredri/cdp/cmd/cdpgen/proto"
)
// Global constants.
const (
OptionalPropPrefix = ""
realEnum = false
)
var (
nonPtrMap = make(map[string]bool)
)
func panicErr(err error) {
if err != nil {
panic(err)
}
}
func mkdir(name string) error {
err := os.Mkdir(name, 0755)
if os.IsExist(err) {
return nil
}
return err
}
func main() {
var (
destPkg string
browserProtoJSON string
jsProtoFileJSON string
)
flag.StringVar(&destPkg, "dest-pkg", "", "Destination for generated cdp package (inside $GOPATH)")
flag.StringVar(&browserProtoJSON, "browser-proto", "./protodef/browser_protocol.json", "Path to browser protocol")
flag.StringVar(&jsProtoFileJSON, "js-proto", "./protodef/js_protocol.json", "Path to JS protocol")
flag.Parse()
if destPkg == "" {
fmt.Fprintln(os.Stderr, "error: dest-pkg must be set")
os.Exit(1)
}
var protocol, jsProtocol proto.Protocol
protocolData, err := ioutil.ReadFile(browserProtoJSON)
panicErr(err)
err = json.Unmarshal(protocolData, &protocol)
panicErr(err)
jsProtocolData, err := ioutil.ReadFile(jsProtoFileJSON)
panicErr(err)
err = json.Unmarshal(jsProtocolData, &jsProtocol)
panicErr(err)
protocol.Domains = append(protocol.Domains, jsProtocol.Domains...)
sort.Slice(protocol.Domains, func(i, j int) bool {
return protocol.Domains[i].Domain < protocol.Domains[j].Domain
})
protoDest := path.Join(destPkg, "protocol")
imports := []string{
"github.com/mafredri/cdp/rpcc",
"github.com/mafredri/cdp/protocol/internal",
"github.com/mafredri/cdp/protocol",
}
for i, d := range protocol.Domains {
dLower := strings.ToLower(d.Name())
imports = append(imports, path.Join(protoDest, dLower))
for ii, t := range d.Types {
nam := t.Name(d)
if isNonPointer(d.Domain, d, t) {
nonPtrMap[nam] = true
nonPtrMap[d.Domain+"."+nam] = true
}
// Reference the FrameId in the Frame type.
if d.Domain == "Page" && t.IDName == "Frame" {
for iii, p := range t.Properties {
if p.NameName == "id" || p.NameName == "parentId" {
p.Ref = "FrameId"
t.Properties[iii] = p
}
}
}
d.Types[ii] = t
}
protocol.Domains[i] = d
}
nonPtrMap["Timestamp"] = true
nonPtrMap["protocol.Timestamp"] = true
nonPtrMap["TimeSinceEpoch"] = true
nonPtrMap["network.TimeSinceEpoch"] = true
nonPtrMap["MonotonicTime"] = true
nonPtrMap["network.MonotonicTime"] = true
var cdp, g Generator
cdp.imports = imports
g.imports = imports
// Define the cdp Client.
cdp.pkg = "cdp"
cdp.dir = destPkg
cdp.PackageHeader("")
cdp.CdpClient(protocol.Domains)
cdp.writeFile("cdp_client.go")
// Package cdp/protocol.
g.pkg = "protocol"
g.dir = protoDest
err = mkdir(g.path())
panicErr(err)
// Generate circular dependency types inside protocol package.
for _, d := range protocol.Domains {
if d.Name() == "Page" {
d.Domain = "protocol"
g.Printf("// +build !go1.9\n\n")
g.PackageHeader("")
for _, t := range d.Types {
if t.Name(d) == "FrameID" || t.Name(d) == "ResourceType" {
t.IDName = "Page" + t.IDName
g.DomainType(d, t)
}
}
g.writeFile("page18.go")
}
}
// Package cdp/protocol/internal.
g.pkg = "internal"
g.dir = path.Join(protoDest, "internal")
for _, d := range protocol.Domains {
if d.Name() == "Page" {
d.Domain = "internal"
g.Printf("// +build go1.9\n\n")
g.PackageHeader("")
for _, t := range d.Types {
if t.Name(d) == "FrameID" || t.Name(d) == "ResourceType" {
idName := t.Name(d)
t.IDName = "Page" + t.IDName
t.Description = fmt.Sprintf("%s\n\nThis type cannot be used directly. Use page.%s instead.", t.Description, idName)
g.DomainType(d, t)
}
}
g.writeFile("page19.go")
}
}
// Generate the protocol definitions.
for _, d := range protocol.Domains {
cdp.PackageHeader("")
cdp.DomainInterface(d)
dLower := strings.ToLower(d.Domain)
g.dir = path.Join(protoDest, dLower)
g.pkg = dLower
err := mkdir(g.path())
panicErr(err)
comment := fmt.Sprintf("Package %s implements the %s domain. ", dLower, d.Name())
g.PackageHeader(fmt.Sprintf("// %s%s", comment, d.Desc(0, len(comment))))
g.DomainDefinition(d)
g.writeFile("domain.go")
if len(d.Types) > 0 {
g.PackageHeader("")
for _, t := range d.Types {
g.DomainType(d, t)
}
g.writeFile("types.go")
}
if len(d.Commands) > 0 {
g.PackageHeader("")
for _, c := range d.Commands {
if c.Redirect != "" {
continue
}
g.DomainCmd(d, c)
}
g.writeFile("command.go")
}
if len(d.Events) > 0 {
g.PackageHeader("")
for _, e := range d.Events {
g.DomainEvent(d, e)
}
g.writeFile("event.go")
}
}
cdp.writeFile(cdp.pkg + ".go")
g.dir = destPkg
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
goimports := exec.CommandContext(ctx, "goimports", "-w", g.path())
out, err := goimports.CombinedOutput()
if err != nil {
log.Printf("goimports failed: %s", out)
log.Println(err)
os.Exit(1)
}
goinstall := exec.CommandContext(ctx, "go", "install", path.Join(destPkg, "..."))
out, err = goinstall.CombinedOutput()
if err != nil {
log.Printf("install failed: %s", out)
log.Println(err)
os.Exit(1)
}
}
// Generator holds the state of the analysis. Primarily used to buffer
// the output for format.Source.
type Generator struct {
dir string
pkg string
imports []string
buf bytes.Buffer // Accumulated output.
testbuf bytes.Buffer // Accumulated test output.
hasContent bool
hasHeader bool
// For managing circular types for go 1.8/1.9.
writingType bool
isCircularType bool
typebuf bytes.Buffer // Accumulated type output.
typebuf18 bytes.Buffer
typebuf19 bytes.Buffer
}
func (g *Generator) path() string {
return path.Join(os.Getenv("GOPATH"), "src", g.dir)
}
// Printf prints to the Generator buffer.
func (g *Generator) Printf(format string, args ...interface{}) {
buf := &g.buf
if g.isCircularType {
fmt.Fprintf(&g.typebuf19, format, args...)
fmt.Fprintf(&g.typebuf18, format, args...)
return
}
if g.writingType {
buf = &g.typebuf
}
fmt.Fprintf(buf, format, args...)
}
// Printf19 prints to the type buffer for Go 1.9+.
func (g *Generator) Printf19(format string, args ...interface{}) {
fmt.Fprintf(&g.typebuf19, format, args...)
}
// Printf18 prints to the type buffer for Go 1.8-.
func (g *Generator) Printf18(format string, args ...interface{}) {
fmt.Fprintf(&g.typebuf18, format, args...)
}
func (g *Generator) beginType() {
g.writingType = true
g.isCircularType = false
}
func (g *Generator) markCircularType() {
if !g.isCircularType {
_, err := g.typebuf19.Write(g.typebuf.Bytes())
panicErr(err)
_, err = g.typebuf18.Write(g.typebuf.Bytes())
panicErr(err)
g.typebuf.Reset()
}
g.isCircularType = true
}
func (g *Generator) commitType() {
g.writingType = false
if g.isCircularType {
return
}
_, err := g.typebuf.WriteTo(&g.buf)
panicErr(err)
g.typebuf.Reset()
}
// TestPrintf prints to the Generator test buffer.
func (g *Generator) TestPrintf(format string, args ...interface{}) {
// No-op
// fmt.Fprintf(&g.testbuf, format, args...)
}
func (g *Generator) writeFile(f string) {
fp := path.Join(g.path(), f)
if !g.hasContent {
log.Printf("No content, skipping %s...", fp)
g.clear()
return
}
if g.buf.Len() == 0 {
log.Printf("Empty buffer, skipping %s...", fp)
return
}
log.Printf("Writing %s...", fp)
err := ioutil.WriteFile(fp, g.format(), 0644)
panicErr(err)
if g.testbuf.Len() > 0 {
g.buf.Reset()
g.Printf("package %s\n\n", g.pkg)
_, err = g.testbuf.WriteTo(&g.buf)
panicErr(err)
fptest := strings.Replace(fp, ".go", "_test.go", 1)
log.Printf("Writing %s...", fptest)
err = ioutil.WriteFile(fptest, g.format(), 0644)
panicErr(err)
}
g.clear()
if g.typebuf19.Len() > 0 {
g.Printf("// +build go1.9\n\n")
g.hasHeader = false
g.PackageHeader("")
_, err = g.typebuf19.WriteTo(&g.buf)
panicErr(err)
fp19 := strings.Replace(fp, ".go", "19.go", 1)
log.Printf("Writing %s...", fp19)
err = ioutil.WriteFile(fp19, g.format(), 0644)
panicErr(err)
}
g.clear()
if g.typebuf18.Len() > 0 {
g.Printf("// +build !go1.9\n\n")
g.hasHeader = false
g.PackageHeader("")
_, err = g.typebuf18.WriteTo(&g.buf)
panicErr(err)
fp18 := strings.Replace(fp, ".go", "18.go", 1)
log.Printf("Writing %s...", fp18)
err = ioutil.WriteFile(fp18, g.format(), 0644)
panicErr(err)
}
g.clear()
}
func (g *Generator) clear() {
g.hasContent = false
g.hasHeader = false
g.writingType = false
g.isCircularType = false
g.buf.Reset()
g.testbuf.Reset()
g.typebuf.Reset()
}
// format returns the gofmt-ed contents of the Generator's buffer.
func (g *Generator) format() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
return g.buf.Bytes()
}
return src
}
// CdpClient creates the cdp.Client type.
func (g *Generator) CdpClient(domains []proto.Domain) {
g.hasContent = true
var fields, newFields Generator
for _, d := range domains {
fields.Printf("\t%s %s\n", d.Name(), d.Type())
newFields.Printf("\t\t%s: %s.NewClient(conn),\n", d.Name(), strings.ToLower(d.Name()))
}
g.Printf(`
// Client represents a Chrome Debugging Protocol client that can be used to
// invoke methods or listen to events in every CDP domain. The Client consumes
// a rpcc connection, used to invoke the methods.
type Client struct {
%s
}
// NewClient returns a new Client that uses conn
// for communication with the debugging target.
func NewClient(conn *rpcc.Conn) *Client {
return &Client{
%s
}
}
`, fields.buf.Bytes(), newFields.buf.Bytes())
}
// PackageHeader writes the header for a package.
func (g *Generator) PackageHeader(comment string) {
if g.hasHeader {
return
}
g.hasHeader = true
g.Printf(`// Code generated by cdpgen. DO NOT EDIT.
%s
package %s
import (
"context"
"encoding/json"
"fmt"
%s
)
`, comment, g.pkg, quotedImports(g.imports))
}
// DomainInterface defines the domain interface.
func (g *Generator) DomainInterface(d proto.Domain) {
g.hasContent = true
comment := "The " + d.Name() + " domain. "
desc := d.Desc(0, len(comment))
if d.Deprecated {
desc = "\n//\n// Deprecated: " + desc
}
if d.Experimental {
desc += "\n//\n// Note: This domain is experimental."
}
g.Printf(`
// %[1]s%[2]s
type %[3]s interface{`, comment, desc, d.Name())
for _, c := range d.Commands {
if c.Redirect != "" {
continue
}
request := ""
reply := "error"
if len(c.Parameters) > 0 {
request = ", *" + strings.ToLower(d.Name()) + "." + c.ArgsName(d)
}
if len(c.Returns) > 0 {
reply = fmt.Sprintf("(*%s.%s, error)", strings.ToLower(d.Name()), c.ReplyName(d))
}
desc := c.Desc(true, 8, 0)
if c.Deprecated {
desc = strings.Replace(c.Desc(true, 8, 12), "Deprecated, ", "", 1)
desc = "Deprecated: " + strings.ToUpper(desc[0:1]) + desc[1:]
}
if desc != "" {
desc = "\n\t//\n\t// " + desc
}
if c.Experimental {
desc += "\n\t//\n\t// Note: This command is experimental."
}
g.Printf("\n\t// Command %s%s\n\t%s(context.Context%s) %s\n", c.Name(), desc, c.Name(), request, reply)
}
for _, e := range d.Events {
eventClient := fmt.Sprintf("%sClient", e.EventName(d))
desc := e.Desc(true, 8, 0)
if e.Deprecated {
desc = strings.Replace(e.Desc(true, 8, 12), "Deprecated, ", "", 1)
desc = "Deprecated: " + strings.ToUpper(desc[0:1]) + desc[1:]
}
if desc != "" {
desc = "\n\t//\n\t// " + desc
}
if e.Experimental {
desc += "\n//\n// Note: This event is experimental."
}
g.Printf("\n\t// Event %s%s\n\t%s(context.Context) (%s.%s, error)\n", e.Name(), desc, e.Name(), strings.ToLower(d.Name()), eventClient)
}
g.Printf("}\n")
}
// DomainDefinition defines the entire domain.
func (g *Generator) DomainDefinition(d proto.Domain) {
g.hasContent = true
comment := fmt.Sprintf("domainClient is a client for the %s domain. ", d.Name())
g.Printf(`
// %[1]s%[3]s
type domainClient struct{ conn *rpcc.Conn }
// NewClient returns a client for the %[2]s domain with the connection set to conn.
func NewClient(conn *rpcc.Conn) *domainClient {
return &domainClient{conn: conn}
}
`, comment, d.Name(), d.Desc(0, len(comment)))
for _, c := range d.Commands {
if c.Redirect != "" {
continue
}
request := ""
invokeReply := "nil"
if len(c.Parameters) > 0 {
request = ", args *" + c.ArgsName(d)
}
reply := "(err error)"
if len(c.Returns) > 0 {
reply = fmt.Sprintf("(reply *%s, err error)", c.ReplyName(d))
}
comment := fmt.Sprintf("%[1]s invokes the %[2]s method. ", c.Name(), d.Name())
g.Printf(`
// %[2]s%[5]s
func (d *domainClient) %[1]s(ctx context.Context%[3]s) %[4]s {`, c.Name(), comment, request, reply, c.Desc(true, 0, len(comment)))
if len(c.Returns) > 0 {
g.Printf(`
reply = new(%s)`, c.ReplyName(d))
invokeReply = "reply"
}
if len(c.Parameters) > 0 {
g.Printf(`
if args != nil {
err = rpcc.Invoke(ctx, %[1]q, args, %[2]s, d.conn)
} else {
err = rpcc.Invoke(ctx, %[1]q, nil, %[2]s, d.conn)
}
if err != nil {
err = &internal.OpError{Domain: %[3]q, Op: %[4]q, Err: err}
}
return
}
`, d.Domain+"."+c.NameName, invokeReply, d.Name(), c.Name())
} else {
g.Printf(`
err = rpcc.Invoke(ctx, %q, nil, %s, d.conn)
if err != nil {
err = &internal.OpError{Domain: %q, Op: %q, Err: err}
}
return
}
`, d.Domain+"."+c.NameName, invokeReply, d.Name(), c.Name())
}
// Generate method tests.
g.TestPrintf(`
func Test%[1]s_%[2]s(t *testing.T) {
conn, codec, cleanup := newTestConn(t)
defer cleanup()
dom := New%[1]s(conn)
var err error
`, d.Name(), c.Name())
assign := "err"
if len(c.Returns) > 0 {
assign = "_, err"
}
if len(c.Parameters) > 0 {
g.TestPrintf(`
// Test nil args.
%[1]s = dom.%[2]s(nil, nil)
if err != nil {
t.Error(err)
}
// Test args.
%[1]s = dom.%[2]s(nil, &%[3]s{})`, assign, c.Name(), c.ArgsName(d))
} else {
g.TestPrintf(`
%[1]s = dom.%[2]s(nil)`, assign, c.Name())
}
g.TestPrintf(`
if err != nil {
t.Error(err)
}
// Test error.
codec.respErr = errors.New("bad request")`)
if len(c.Parameters) > 0 {
g.TestPrintf(`
%[1]s = dom.%[2]s(nil, &%[3]s{})`, assign, c.Name(), c.ArgsName(d))
} else {
g.TestPrintf(`
%[1]s = dom.%[2]s(nil)`, assign, c.Name())
}
g.TestPrintf(`
if err == nil || err.(*internal.OpError).Err.(*rpcc.ResponseError).Message != codec.respErr.Error() {
t.Errorf("unexpected error; got: %%v, want bad request", err)
}`)
g.TestPrintf(`
}
`)
}
for _, e := range d.Events {
eventClient := fmt.Sprintf("%sClient", e.EventName(d))
eventClientImpl := strings.ToLower(string(eventClient[0])) + eventClient[1:]
// Implement event on domain.
g.Printf(`
func (d *domainClient) %s(ctx context.Context) (%s, error) {
s, err := rpcc.NewStream(ctx, %q, d.conn)
if err != nil {
return nil, err
}
return &%s{Stream: s}, nil
}
`, e.Name(), eventClient, d.Domain+"."+e.NameName, eventClientImpl)
g.Printf(`
type %[4]s struct { rpcc.Stream }
// GetStream returns the original Stream for use with cdp.Sync.
func (c *%[4]s) GetStream() rpcc.Stream { return c.Stream }
func (c *%[4]s) Recv() (*%[3]s, error) {
event := new(%[3]s)
if err := c.RecvMsg(event); err != nil {
return nil, &internal.OpError{Domain: %[5]q, Op: "%[6]s Recv", Err: err}
}
return event, nil
}
`, eventClient, "", e.ReplyName(d), eventClientImpl, d.Name(), e.Name())
// Generate event tests.
g.TestPrintf(`
func Test%[1]s_%[2]s(t *testing.T) {
conn, codec, cleanup := newTestConn(t)
defer cleanup()
dom := New%[1]s(conn)
stream, err := dom.%[2]s(nil)
if err != nil {
t.Fatal(err)
}
defer stream.Close()
codec.event = %[3]s.String()
codec.conn <- nil
_, err = stream.Recv()
if err != nil {
t.Error(err)
}
codec.eventArgs = []byte("invalid json")
codec.conn <- nil
_, err = stream.Recv()
if err, ok := err.(*internal.OpError); !ok {
t.Errorf("Recv() got %%v, want internal.OpError", err)
}
conn.Close()
stream, err = dom.%[2]s(nil)
if err == nil {
t.Errorf("Open stream: got nil, want error")
}
`, d.Name(), e.Name(), e.EventName(d))
g.TestPrintf(`
}
`)
}
}
// DomainType creates the type definition.
func (g *Generator) DomainType(d proto.Domain, t proto.AnyType) {
g.hasContent = true
g.beginType()
var comment string
if d.Name() == "Page" && (t.Name(d) == "FrameID" || t.Name(d) == "ResourceType") {
g.markCircularType()
comment = "//"
g.Printf19(`
// %[1]s %[2]s
//
// Provided as an alias to prevent circular dependencies.
type %[1]s = internal.Page%[1]s
`, t.Name(d), t.Desc(0, len(t.Name(d))+1))
}
desc := t.Desc(0, len(t.Name(d))+1)
if t.Deprecated {
desc = "\n//\n// Deprecated: " + t.Desc(0, 12)
}
if t.Experimental {
desc = desc + "\n//\n// Note: This type is experimental."
}
g.Printf(`
// %[1]s %[2]s
%[3]stype %[1]s `, t.Name(d), desc, comment)
typ := t.GoType(g.pkg, d)
switch typ {
case "struct":
g.domainTypeStruct(d, t)
case "enum":
g.domainTypeEnum(d, t)
case "time.Time":
g.domainTypeTime(d, t)
case "RawMessage":
g.domainTypeRawMessage(d, t)
default:
g.Printf(typ)
}
g.Printf("\n\n")
g.commitType()
}
func (g *Generator) printStructProperties(d proto.Domain, name string, props []proto.AnyType, ptrOptional, renameOptional bool) {
for _, prop := range props {
jsontag := prop.NameName
ptype := prop.GoType(g.pkg, d)
ptype19 := ptype
ptype18 := ptype
if ptype == "page.FrameID" || ptype == "page.ResourceType" {
g.markCircularType()
if g.pkg == "network" || g.pkg == "dom" {
// Domain-local type (alias) for go1.9.
ptype19 = strings.Replace(ptype19, "page.", "internal.Page", 1)
}
ptype18 = strings.Replace(ptype18, ".", "", 1)
ptype18 = "protocol." + strings.Title(ptype18)
}
if g.pkg == "page" && (ptype == "FrameID" || ptype == "ResourceType") {
g.markCircularType()
ptype18 = "protocol.Page" + ptype18
}
// Make all optional properties into pointers, unless they are slices.
if prop.Optional {
isNonPtr := nonPtrMap[ptype]
if ptrOptional && !isNonPtr && !isNonPointer(g.pkg, d, prop) {
ptype = "*" + ptype
ptype19 = "*" + ptype19
ptype18 = "*" + ptype18
}
jsontag += ",omitempty"
}
// Avoid recursive type definitions.
if ptype == name {
ptype = "*" + ptype
ptype19 = "*" + ptype19
ptype18 = "*" + ptype18
}
exportedName := prop.ExportedName(d)
if renameOptional && prop.Optional {
exportedName = OptionalPropPrefix + exportedName
}
var preDesc, postDesc string
desc := prop.Desc(8, len(exportedName)+1)
var deprecated, localEnum, experimental string
if prop.Deprecated {
desc = prop.Desc(8, 12)
if desc == "" {
desc = "This property should not be used."
}
deprecated = "//\n// Deprecated: " + desc + "\n"
desc = "is deprecated."
}
if prop.IsLocalEnum() {
var enums []string
for _, e := range prop.Enum {
enums = append(enums, fmt.Sprintf("%q", e))
}
localEnum = "//\n// Values: " + strings.Join(enums, ", ") + ".\n"
}
if prop.Experimental {
experimental = "//\n// Note: This property is experimental.\n"
}
if deprecated != "" || localEnum != "" || experimental != "" {
preDesc = "// " + exportedName + " " + desc + "\n" + deprecated + localEnum + experimental
} else {
if desc == "" {
desc = "No description."
}
postDesc = "// " + enforceSingleLine(desc)
}
if !g.isCircularType {
g.Printf("\t%s%s %s `json:\"%s\"` %s\n", preDesc, exportedName, ptype, jsontag, postDesc)
} else {
g.Printf19("\t%s%s %s `json:\"%s\"` %s\n", preDesc, exportedName, ptype19, jsontag, postDesc)
g.Printf18("\t%s%s %s `json:\"%s\"` %s\n", preDesc, exportedName, ptype18, jsontag, postDesc)
}
}
}
func enforceSingleLine(s string) string {
return strings.Replace(s, "\n//", "", -1)
}
func (g *Generator) domainTypeStruct(d proto.Domain, t proto.AnyType) {
g.Printf("struct{\n")
g.printStructProperties(d, t.Name(d), t.Properties, true, false)
g.Printf("}")
}
func (g *Generator) domainTypeTime(d proto.Domain, t proto.AnyType) {
g.Printf(`float64
// String calls (time.Time).String().
func (t %[1]s) String() string {
return t.Time().String()
}
// Time parses the Unix time with millisecond accuracy.
func (t %[1]s) Time() time.Time {
secs := int64(t)
// The Unix time in t only has ms accuracy.
ms := int64((float64(t)-float64(secs))*1000000)
return time.Unix(secs, ms*1000)
}
// MarshalJSON implements json.Marshaler. Encodes to null if t is zero.
func (t %[1]s) MarshalJSON() ([]byte, error) {
if t == 0 {
return []byte("null"), nil
}
f := float64(t)
return json.Marshal(&f)
}
// UnmarshalJSON implements json.Unmarshaler.
func (t *%[1]s) UnmarshalJSON(data []byte) error {
*t = 0
if len(data) == 0 {
return nil
}
var f float64
if err := json.Unmarshal(data, &f); err != nil {
return errors.New("%[2]s.%[1]s: " + err.Error())
}
*t = %[1]s(f)
return nil
}
var _ json.Marshaler = (*%[1]s)(nil)
var _ json.Unmarshaler = (*%[1]s)(nil)
`, t.Name(d), g.pkg)
g.TestPrintf(`
func Test%[1]s_Marshal(t *testing.T) {
var v %[1]s
// Test empty.
b, err := json.Marshal(&v)
if err != nil {
t.Errorf("Marshal() got %%v, want no error", err)
}
if string(b) != "null" {
t.Errorf("Marshal() got %%s, want null", b)
}
err = json.Unmarshal(b, &v)
if err != nil {
t.Errorf("Unmarshal() got %%v, want no error", err)
}
// Test non-empty.
v = 1
b, err = json.Marshal(&v)
if err != nil {
t.Errorf("Marshal() got %%v, want no error", err)
}
if string(b) != "1" {
t.Errorf("Marshal() got %%s, want 1", b)
}
err = json.Unmarshal(b, &v)
if err != nil {
t.Errorf("Unmarshal() got %%v, want no error", err)
}
}
`, t.Name(d))
}
func (g *Generator) domainTypeRawMessage(d proto.Domain, t proto.AnyType) {
g.Printf(`[]byte
// MarshalJSON copies behavior of json.RawMessage.
func (%[3]s %[1]s) MarshalJSON() ([]byte, error) {
if %[3]s == nil {
return []byte("null"), nil
}
return %[3]s, nil
}
// UnmarshalJSON copies behavior of json.RawMessage.
func (%[3]s *%[1]s) UnmarshalJSON(data []byte) error {
if %[3]s == nil {
return errors.New("%[2]s.%[1]s: UnmarshalJSON on nil pointer")
}
*%[3]s = append((*%[3]s)[0:0], data...)
return nil
}
var _ json.Marshaler = (*%[1]s)(nil)
var _ json.Unmarshaler = (*%[1]s)(nil)
`, t.Name(d), g.pkg, t.Recvr(d))
g.TestPrintf(`
func Test%[1]s_Marshal(t *testing.T) {
var v %[1]s
// Test empty.
b, err := json.Marshal(&v)
if err != nil {
t.Errorf("Marshal() got %%v, want no error", err)
}
if string(b) != "null" {
t.Errorf("Marshal() got %%s, want null", b)
}
err = json.Unmarshal(b, &v)
if err != nil {
t.Errorf("Unmarshal() got %%v, want no error", err)
}
// Test non-empty.
v = []byte("\"test\"")
b, err = json.Marshal(&v)
if err != nil {
t.Errorf("Marshal() got %%v, want no error", err)
}
if !bytes.Equal(v, b) {
t.Errorf("Marshal() got %%s, want %%s", b, v)
}
v = nil
err = json.Unmarshal(b, &v)
if err != nil {
t.Errorf("Unmarshal() got %%v, want no error", err)
}
if !bytes.Equal(v, b) {
t.Errorf("Unmarshal() got %%s, want %%s", b, v)
}
}
`, t.Name(d))
}
func (g *Generator) domainTypeEnum(d proto.Domain, t proto.AnyType) {
isCircular := g.isCircularType
if t.Type != "string" {
log.Panicf("unknown enum type: %s", t.Type)
}
// Verify assumption about enums never being the empty string, this
// allows us to consider optional enums as type string instead of
// *string when encoding to JSON (omitempty).
for _, e := range t.Enum {
if e == "" {
panic("enum " + t.Name(d) + " has unexpected empty enum value")
}
}
name := strings.Title(t.Name(d))
if realEnum {
g.Printf("int\n\n")
if name != "PageResourceType" {
format := `
// %s as enums.
const (
%sNotSet %s = iota`
if !isCircular {
g.Printf(format, name, name, name)
} else {
g.Printf19(format, name, name, name)
g.Printf18(format, name, name, "protocol."+d.Name()+name)
}
for _, e := range t.Enum {
g.Printf("\n\t%s%s", name, e.Name())
}
g.Printf(`
)
`)
if isCircular {
g.commitType()
g.beginType()
}
}
g.Printf(`
// Valid returns true if enum is set.
func (e %[1]s) Valid() bool {
return e >= 1 && e <= %[2]d