-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
291 lines (255 loc) · 6.38 KB
/
Copy pathexample_test.go
File metadata and controls
291 lines (255 loc) · 6.38 KB
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
package gobspect_test
// Runnable examples for the gobspect API. Each example hosts one snippet
// region consumed by the documentation site.
import (
"bytes"
"encoding/gob"
"fmt"
"log"
gobspect "github.com/codepuke/gobspect"
)
// Example_streamValues demonstrates the core decoding loop: stream a gob
// payload and print each decoded value.
func Example_streamValues() {
// snippet:start stream-values
// Encode two values the usual way.
type User struct {
Name string
Age int
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(User{Name: "alice", Age: 30})
enc.Encode(User{Name: "bob", Age: 25})
// Decode the stream without the original type.
ins := gobspect.New()
for v, err := range ins.Stream(&buf).Values() {
if err != nil {
log.Fatal(err)
}
fmt.Println(gobspect.Format(v))
}
// snippet:end
// Output:
// User{
// Name: "alice"
// Age: 30
// }
// User{
// Name: "bob"
// Age: 25
// }
}
// Example_streamTypes demonstrates reading the type definitions collected
// while decoding a stream.
func Example_streamTypes() {
// snippet:start stream-types
type Point struct {
X, Y int
}
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Point{X: 1, Y: 2})
ins := gobspect.New()
s := ins.Stream(&buf)
if _, err := s.Collect(); err != nil {
log.Fatal(err)
}
// s.Types() now holds every type definition the stream declared.
for _, ti := range s.Types() {
fmt.Printf("%s (%s)\n", ti.Name, ti.Kind)
for _, f := range ti.Fields {
fmt.Printf(" field %s\n", f.Name)
}
}
// snippet:end
// Output:
// Point (struct)
// field X
// field Y
}
// Example_schemaExtract demonstrates extracting a Go-like schema from a
// stream without keeping the decoded values.
func Example_schemaExtract() {
// snippet:start schema-extract
type Customer struct {
Name string
}
type Order struct {
ID int
Customer Customer
Tags []string
}
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Order{
ID: 7,
Customer: Customer{Name: "alice"},
Tags: []string{"rush"},
})
ins := gobspect.New()
schema, err := ins.Stream(&buf).Schema()
if err != nil {
log.Fatal(err)
}
fmt.Println(schema.String())
// snippet:end
// Output:
// type Customer struct {
// Name string
// }
//
// type Order struct {
// ID int
// Customer Customer
// Tags []string
// }
}
// Example_formatOptions demonstrates adjusting Format output with options.
func Example_formatOptions() {
type Blob struct {
Name string
Data []byte
}
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Blob{Name: "img", Data: []byte{0xde, 0xad, 0xbe, 0xef}})
ins := gobspect.New()
values, err := ins.Stream(&buf).Collect()
if err != nil {
log.Fatal(err)
}
v := values[0]
// snippet:start format-options
// Default rendering.
fmt.Println(gobspect.Format(v))
// Wider indentation and base64-encoded byte slices.
fmt.Println(gobspect.Format(v,
gobspect.WithIndent(" "),
gobspect.WithBytesFormat(gobspect.BytesBase64),
))
// Go-literal byte rendering.
fmt.Println(gobspect.Format(v, gobspect.WithBytesFormat(gobspect.BytesLiteral)))
// snippet:end
// Output:
// Blob{
// Name: "img"
// Data: deadbeef
// }
// Blob{
// Name: "img"
// Data: 3q2+7w==
// }
// Blob{
// Name: "img"
// Data: []byte{0xde, 0xad, 0xbe, 0xef}
// }
}
// Example_redactOutput demonstrates hiding sensitive fields in formatted
// output by field name and by type name.
func Example_redactOutput() {
// snippet:start redact-output
type Login struct {
Username string
Password string
}
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Login{Username: "alice", Password: "hunter2"})
ins := gobspect.New()
values, err := ins.Stream(&buf).Collect()
if err != nil {
log.Fatal(err)
}
// Redact by field or map-key name.
fmt.Println(gobspect.Format(values[0],
gobspect.WithRedactKeys(gobspect.RedactConfig{Keys: []string{"Password"}}),
))
// Redact every value of a named type.
fmt.Println(gobspect.Format(values[0],
gobspect.WithRedactTypes(gobspect.RedactTypesConfig{Types: []string{"Login"}, TextLength: 3}),
))
// snippet:end
// Output:
// Login{
// Username: "alice"
// Password: *********
// }
// ***
}
// Example_toJSON demonstrates converting a decoded value to JSON.
func Example_toJSON() {
// snippet:start to-json
type Point struct {
X, Y int
}
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Point{X: 3, Y: 7})
ins := gobspect.New()
values, err := ins.Stream(&buf).Collect()
if err != nil {
log.Fatal(err)
}
// Type IDs are scoped to the stream that produced the value; zero the
// ID here so the JSON is reproducible.
sv := values[0].(gobspect.StructValue)
sv.GobTypeID = 0
b, err := gobspect.ToJSONIndent(sv, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
// snippet:end
// Output:
// {
// "fields": [
// {
// "name": "X",
// "value": {
// "kind": "int",
// "v": 3
// }
// },
// {
// "name": "Y",
// "value": {
// "kind": "int",
// "v": 7
// }
// }
// ],
// "kind": "struct",
// "typeId": 0,
// "typeName": "Point"
// }
}
// sessionToken is a GobEncoder type whose blob gobspect cannot decode on its
// own; Example_registerDecoder registers a custom decoder for it.
type sessionToken struct{ payload []byte }
func (t sessionToken) GobEncode() ([]byte, error) { return t.payload, nil }
func (t *sessionToken) GobDecode(b []byte) error {
t.payload = append([]byte(nil), b...)
return nil
}
// Example_registerDecoder demonstrates registering a custom decoder for an
// opaque GobEncoder type.
func Example_registerDecoder() {
// snippet:start register-decoder
// sessionToken implements GobEncoder; wrap it in an interface field so
// its type name travels on the wire.
type Envelope struct{ Token any }
gob.RegisterName("sessionToken", sessionToken{})
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(Envelope{Token: sessionToken{payload: []byte("tok-12345")}})
ins := gobspect.New()
// The key is the type's name from the gob wire format.
ins.RegisterDecoder("sessionToken", func(data []byte) (any, error) {
return "session:" + string(data), nil
})
values, err := ins.Stream(&buf).Collect()
if err != nil {
log.Fatal(err)
}
fmt.Println(gobspect.Format(values[0]))
// snippet:end
// Output:
// Envelope{
// Token: (sessionToken) session:tok-12345
// }
}