forked from chromedp/chromedp
/
enum.go
160 lines (127 loc) · 2.92 KB
/
enum.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
package internal
import (
"fmt"
"strconv"
)
// HandlerType are the handler targets for commands and events.
type HandlerType string
// HandlerType values.
const (
HandlerTypeBrowser HandlerType = "browser"
HandlerTypeRenderer HandlerType = "renderer"
)
// String satisfies stringer.
func (ht HandlerType) String() string {
return string(ht)
}
// MarshalJSON satisfies json.Marshaler.
func (ht HandlerType) MarshalJSON() ([]byte, error) {
return []byte(`"` + ht + `"`), nil
}
// UnmarshalJSON satisfies json.Unmarshaler.
func (ht *HandlerType) UnmarshalJSON(buf []byte) error {
s, err := strconv.Unquote(string(buf))
if err != nil {
return err
}
switch HandlerType(s) {
case HandlerTypeBrowser:
*ht = HandlerTypeBrowser
case HandlerTypeRenderer:
*ht = HandlerTypeRenderer
default:
return fmt.Errorf("unknown handler type %s", string(buf))
}
return nil
}
// TypeEnum is the Chrome domain type enum.
type TypeEnum string
// TypeEnum values.
const (
TypeAny TypeEnum = "any"
TypeArray TypeEnum = "array"
TypeBoolean TypeEnum = "boolean"
TypeInteger TypeEnum = "integer"
TypeNumber TypeEnum = "number"
TypeObject TypeEnum = "object"
TypeString TypeEnum = "string"
TypeTimestamp TypeEnum = "timestamp"
)
// String satisfies stringer.
func (te TypeEnum) String() string {
return string(te)
}
// MarshalJSON satisfies json.Marshaler.
func (te TypeEnum) MarshalJSON() ([]byte, error) {
return []byte(`"` + te + `"`), nil
}
// UnmarshalJSON satisfies json.Unmarshaler.
func (te *TypeEnum) UnmarshalJSON(buf []byte) error {
s, err := strconv.Unquote(string(buf))
if err != nil {
return err
}
switch TypeEnum(s) {
case TypeAny:
*te = TypeAny
case TypeArray:
*te = TypeArray
case TypeBoolean:
*te = TypeBoolean
case TypeInteger:
*te = TypeInteger
case TypeNumber:
*te = TypeNumber
case TypeObject:
*te = TypeObject
case TypeString:
*te = TypeString
default:
return fmt.Errorf("unknown type enum %s", string(buf))
}
return nil
}
// GoType returns the Go type for the TypeEnum.
func (te TypeEnum) GoType() string {
switch te {
case TypeAny:
return "easyjson.RawMessage"
case TypeBoolean:
return "bool"
case TypeInteger:
return "int64"
case TypeNumber:
return "float64"
case TypeString:
return "string"
case TypeTimestamp:
return "time.Time"
default:
panic(fmt.Sprintf("called GoType on non primitive type %s", te.String()))
}
return ""
}
// GoEmptyValue returns the Go empty value for the TypeEnum.
func (te TypeEnum) GoEmptyValue() string {
switch te {
case TypeBoolean:
return `false`
case TypeInteger:
return `0`
case TypeNumber:
return `0`
case TypeString:
return `""`
case TypeTimestamp:
return `time.Time{}`
}
return `nil`
}
// TimestampType are the various timestamp subtypes.
type TimestampType int
// TimestampType values.
const (
TimestampTypeMillisecond TimestampType = 1 + iota
TimestampTypeSecond
TimestampTypeMonotonic
)