-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
331 lines (266 loc) · 6.41 KB
/
example_test.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
package lift_test
import (
"fmt"
"io"
"math"
"strings"
"github.com/AndrewHarrisSPU/lift"
)
func ExampleT() {
r := lift.T[rune]()
b := lift.T[byte]()
fmt.Println(r == b)
// Output:
// false
}
func ExampleT_alias() {
i := lift.T[int32]()
r := lift.T[rune]()
// because rune is an alias of int32, the output is "true"
fmt.Println(i == r)
// Output:
// true
}
func ExampleTypeOf() {
t := lift.T[string]()
fmt.Println(t == lift.TypeOf(""))
fmt.Println(t == lift.TypeOf("twine"))
fmt.Println(t == lift.TypeOf([]rune{}))
// Output:
// true
// true
// false
}
func ExampleEnumIs() {
a := lift.Wrap(false)
fmt.Println(lift.EnumIs[bool](a))
// Output:
// true
}
// WRAP, UNWRAP
func ExampleWrap() {
π := lift.Wrap(math.Pi)
pi, _ := lift.Unwrap[float64](π)
fmt.Println(pi == math.Pi)
// Output:
// true
}
func ExampleWrap_equivalence() {
sym := lift.Wrap('?')
// the wrapped symbol is not == to rune's type enumeration symbol
fmt.Println(lift.T[rune]() == sym)
// EnumIs[rune] evaluates true, however
fmt.Println(lift.EnumIs[rune](sym))
// Output:
// false
// true
}
func ExampleUnwrap() {
bits := byte(0)
sym := lift.Wrap(bits)
if _, ok := lift.Unwrap[byte](sym); ok {
fmt.Print("got a byte")
}
// Output:
// got a byte
}
// This works. A pointer to a [strings.Builder] is an [io.Writer].
func ExampleUnwrapAs_interface() {
var b strings.Builder
sym := lift.Wrap(&b)
if _, ok := lift.UnwrapAs[io.Writer](sym); ok {
fmt.Printf("unwrapped a Writer!")
}
// Output:
// unwrapped a Writer!
}
// This won't work. [UnwrapAs] performs type assertion, not conversion.
func ExampleUnwrapAs_noConversion() {
type bit bool
sym := lift.Wrap(bit(true))
if bit, ok := lift.UnwrapAs[bool](sym); ok {
fmt.Printf("%v\n", bit)
} else {
fmt.Println("?")
}
// Output:
// ?
}
// MAP
func ExampleMap() {
// Map creation
m := lift.NewMap[string](
lift.Def[int]("red"),
lift.Def[float64]("blue"),
)
type vector2d [2]float64
// Storing to a Map
m.Store(
lift.Def[complex128]("C"),
lift.Def[vector2d]("R2"),
)
// Loading from a Map
intColor, _ := lift.LoadTypeOf(m, 1)
floatColor, _ := lift.LoadSym(m, lift.TypeOf(1.0))
complexDomain, _ := lift.Load[complex128](m)
vector2dDomain, _ := lift.Load[vector2d](m)
fmt.Printf("ints are %s, float64s are %s\n", intColor, floatColor)
fmt.Printf("complex128s live in %s (not %s)", complexDomain, vector2dDomain)
// Output:
// ints are red, float64s are blue
// complex128s live in C (not R2)
}
func ExampleDef() {
type text struct{}
type mauve struct{}
crayons := lift.NewMap[int](
lift.Def[text](0x222222),
lift.Def[mauve](0xa17188),
)
headline, _ := lift.Load[mauve](crayons)
paragraph, _ := lift.Load[text](crayons)
fmt.Printf("#%06x, #%06x", headline, paragraph)
// Output:
// #a17188, #222222
}
func ExampleDefSym() {
items := lift.NewMap[string](
lift.DefSym(lift.Any, "could be anything"),
)
fish := lift.Wrap(any("it's a fish!"))
anything, _ := lift.LoadSym(items, fish)
fmt.Printf(anything)
// Output:
// could be anything
}
func ExampleMap_Store() {
type Queen struct{}
black := lift.NewMap[rune]()
black.Store(
lift.Def[Queen]('♛'),
)
piece, _ := lift.Load[Queen](black)
fmt.Printf("%c", piece)
// Output:
// ♛
}
func ExampleMap_Delete() {
m := lift.NewMap[bool](
lift.Def[rune](true),
lift.Def[string](true),
)
m.Delete(lift.T[rune]())
if _, ok := lift.Load[rune](m); !ok {
fmt.Println("No entry found")
}
// Output:
// No entry found
}
func ExampleLoad() {
masks := lift.NewMap[int](
lift.Def[uint8](0xff),
)
mask, _ := lift.Load[uint8](masks)
fmt.Println(mask&0x1234_5678 == 0x78)
if _, ok := lift.Load[string](masks); !ok {
fmt.Println("oops")
}
// Output:
// true
// oops
}
func ExampleLoadKey() {
names := lift.NewMap[string](
lift.Def[bool]("Boolean"),
lift.Def[string]("string"),
)
name1, _ := lift.LoadTypeOf(names, false)
name2, _ := lift.LoadTypeOf(names, "false")
fmt.Print(name1, ", ", name2)
// Output:
// Boolean, string
}
func ExampleLoadSym() {
type hammer struct{}
type screwdriver struct{}
tools := lift.NewMap[int](
lift.Def[hammer](2),
lift.Def[screwdriver](17),
)
var count int
for _, tool := range tools.Keys() {
n, _ := lift.LoadSym(tools, tool)
count += n
}
fmt.Printf("I have %d tools", count)
// Output:
// I have 19 tools
}
func ExampleLoadTypeOf() {
fmtrs := lift.NewMap[string](
lift.Def[uint8]("%08x"),
)
x := uint8(127)
fmtr, _ := lift.LoadTypeOf(fmtrs, x)
fmt.Printf(fmtr, x)
// Output:
// 0000007f
}
// Corner cases of [Sym] around empty-ish or any-ish values are reasonable.
// [Sym] is an interface type, so the zero value of a [Sym] is nil-ish, and will cause panic.
func Example_e_emptyAnyNil() {
defer func() {
if r := recover(); r != nil {
fmt.Println("oops!", r)
}
}()
items := lift.NewMap[string](
lift.Def[struct{}]("the empty struct"),
lift.Def[lift.Empty]("the lift.Empty struct"),
lift.Def[any]("anything"),
lift.Def[lift.Sym]("the type enumeration of lift.Sym"),
)
report := func(tag string, item string) {
fmt.Printf("%-12s %s,\n", tag, item)
}
// Three kinds of the empty struct{}
empty := struct{}{}
item, _ := lift.LoadTypeOf(items, empty)
report("empty i", item)
item, _ = lift.Load[lift.Empty](items)
report("empty ii", item)
type local struct{}
item, _ = lift.LoadTypeOf(items, local{})
report("empty iii", item)
// Three kinds of any
item, _ = lift.Load[any](items)
report("any i", item)
item, _ = lift.LoadTypeOf(items, any("other thing"))
report("any ii", item)
item, _ = lift.LoadSym(items, lift.Any)
report("any iii", item)
// LoadTypeOf infers the type enumeration of lift.Sym from anything wrapped
item, _ = lift.LoadTypeOf(items, lift.Wrap(any(nil)))
report("sym i", item)
// Doesn't crash yet, as we're passing the type enumeration of lift.Sym
var NilSym lift.Sym
item, _ = lift.LoadTypeOf(items, NilSym)
report("sym ii", item)
// PANIC ENSUES - we've passed a raw nil
item, _ = lift.LoadSym(items, NilSym)
report("nil i", item)
// (unreached) PANIC ENSUES:
items.Store(
lift.DefSym(NilSym, "panic"),
)
// Output:
// empty i the empty struct,
// empty ii the lift.Empty struct,
// empty iii ,
// any i anything,
// any ii anything,
// any iii anything,
// sym i the type enumeration of lift.Sym,
// sym ii the type enumeration of lift.Sym,
// oops! runtime error: invalid memory address or nil pointer dereference
}