/
util.go
169 lines (147 loc) · 3.36 KB
/
util.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
package runtime
import (
"bytes"
"fmt"
)
// Error implements error for ExceptionDetails.
func (r ExceptionDetails) Error() string {
var desc string
if r.Exception.Description != nil {
desc = ": " + *r.Exception.Description
}
return fmt.Sprintf("runtime.ExceptionDetails: %s exception at %d:%d%s", r.Text, r.LineNumber, r.ColumnNumber, desc)
}
var _ error = (*ExceptionDetails)(nil)
// String returns a human readable string of a runtime object.
func (r RemoteObject) String() string {
switch r.Type {
case "undefined":
return "undefined"
case "object":
switch {
case r.Preview != nil:
return r.Preview.String()
}
default:
if r.UnserializableValue != nil {
return string(*r.UnserializableValue)
}
}
if len(r.Value) == 0 && r.Description != nil {
return *r.Description
}
return string(r.Value)
}
// String returns a human readable string of the object preview.
func (r ObjectPreview) String() string {
var desc string
if r.Description != nil {
desc = *r.Description
}
switch r.Type {
case "object":
var stype string
if r.Subtype != nil {
stype = *r.Subtype
}
switch stype {
case "null":
return "null"
case "array", "typedarray":
return r.arrayString(desc)
case "date", "regexp":
return desc
default:
if val, ok := primitiveValue(r.Properties); ok {
return fmt.Sprintf("%s(%s)", desc, val)
}
}
return r.objectString(desc)
case "string":
return fmt.Sprintf("%q", desc)
default:
return desc
}
}
func (r ObjectPreview) objectString(desc string) string {
var b bytes.Buffer
b.WriteString(desc)
b.WriteString(" {")
for _, prop := range r.Properties {
b.WriteString(prop.String())
b.WriteString(", ")
}
for _, entry := range r.Entries {
b.WriteString(entry.String())
b.WriteString(", ")
}
if r.Overflow {
b.WriteString("...")
} else if b.Len() >= 2 && (len(r.Properties) > 0 || len(r.Entries) > 0) {
b.Truncate(b.Len() - 2)
}
b.WriteByte('}')
return b.String()
}
func (r ObjectPreview) arrayString(desc string) string {
var b bytes.Buffer
if desc != "" {
b.WriteString(desc)
b.WriteByte(' ')
}
b.WriteByte('[')
for _, prop := range r.Properties {
b.WriteString(prop.string(false))
b.WriteString(", ")
}
if b.Len() >= 2 && len(r.Properties) > 0 {
b.Truncate(b.Len() - 2)
}
b.WriteByte(']')
return b.String()
}
// String returns a human readable string of the property.
func (r PropertyPreview) String() string {
return r.string(true)
}
func (r PropertyPreview) string(showName bool) string {
var b bytes.Buffer
if showName {
b.WriteString(r.Name)
b.WriteString(": ")
}
if r.Value != nil {
if r.Type == "string" {
fmt.Fprintf(&b, "%q", *r.Value)
} else {
b.WriteString(*r.Value)
}
}
if r.ValuePreview != nil {
b.WriteString(r.ValuePreview.String())
}
return b.String()
}
// String returns a human readable string of the entry preview.
func (r EntryPreview) String() string {
var b bytes.Buffer
if r.Key != nil {
b.WriteString(r.Key.String())
b.WriteString(" => ")
}
b.WriteString(r.Value.String())
return b.String()
}
const primitiveValueKey = "[[PrimitiveValue]]"
func primitiveValue(props []PropertyPreview) (string, bool) {
for _, prop := range props {
if prop.Name == primitiveValueKey && prop.Value != nil {
val := *prop.Value
if prop.Type == "string" {
val = fmt.Sprintf("%q", val)
}
return val, true
}
}
return "", false
}