Debian Codegraph @

lib/proto/generate/patch.go

raw
1 package main
2
3 import (
4 "fmt"
5
6 "github.com/ysmood/gson"
7 )
8
9 func patch(json gson.JSON) {
10 k := func(k, v string) gson.Query {
11 return func(obj interface{}) (val interface{}, has bool) {
12 for _, el := range obj.([]interface{}) {
13 res := el.(map[string]interface{})[k]
14 if res == v {
15 return el, true
16 }
17 }
18 panic("not found")
19 }
20 }
21 index := func(obj interface{}, k, v string) string {
22 for i, el := range obj.([]interface{}) {
23 res := el.(map[string]interface{})[k]
24 if res == v {
25 return fmt.Sprintf("%d", i)
26 }
27 }
28 panic("not found")
29 }
30
31 getTypes := func(domain string) gson.JSON {
32 res, _ := json.Gets("domains", k("domain", domain), "types")
33 return res
34 }
35
36 // TargetTargetInfoType
37 j, _ := getTypes("Target").Gets(k("id", "TargetInfo"), "properties", k("name", "type"))
38 j.Set("enum", []string{
39 "page", "background_page", "service_worker", "shared_worker", "browser", "other",
40 })
41
42 // PageLifecycleEventName
43 j, _ = json.Gets("domains", k("domain", "Page"), "events", k("name", "lifecycleEvent"), "parameters", k("name", "name"))
44 j.Set("enum", []string{
45 "init", "firstPaint", "firstContentfulPaint", "firstImagePaint", "firstMeaningfulPaintCandidate",
46 "DOMContentLoaded", "load", "networkAlmostIdle", "firstMeaningfulPaint", "networkIdle",
47 })
48
49 // replace these with better type definition
50 j, _ = getTypes("Input").Gets(k("id", "TimeSinceEpoch"))
51 j.Set("skip", true)
52 j, _ = getTypes("Network").Gets(k("id", "TimeSinceEpoch"))
53 j.Set("skip", true)
54 j, _ = getTypes("Network").Gets(k("id", "MonotonicTime"))
55 j.Set("skip", true)
56
57 // fix Cookie.Expires
58 j, _ = getTypes("Network").Gets(k("id", "Cookie"), "properties")
59 j.Set(index(j.Val(), "name", "expires"), map[string]interface{}{
60 "$ref": "TimeSinceEpoch",
61 "description": "Cookie expiration date",
62 "name": "expires",
63 })
64
65 // deltaX and deltaY are not optional for mouseWheel events
66 j, _ = json.Gets("domains", k("domain", "Input"), "commands", k("name", "dispatchMouseEvent"), "parameters")
67 jj, _ := j.Gets(k("name", "deltaX"))
68 jj.Del("optional")
69 jj, _ = j.Gets(k("name", "deltaY"))
70 jj.Del("optional")
71
72 // removing the optional for the body as we need to distinguish between no body and empty body
73 // with that fix we can send an 'empty body' using `SetBody([]byte{})`
74 // and 'no body' by not calling using 'SetBody()' on the response
75 j, _ = json.Gets("domains", k("domain", "Fetch"), "commands", k("name", "fulfillRequest"), "parameters")
76 jj, _ = j.Gets(k("name", "body"))
77 jj.Del("optional")
78 }