blob: 8c52c5bb56cbbfc39785fd05e9df0bea24ef4ea0 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang232d6492015-02-25 18:04:54 -08005package opconst
6
7// TODO(toddw): Merge with vdl/testutil_test.go.
8
9import (
10 "fmt"
11 "strings"
12 "testing"
13
14 "v.io/v23/vdl"
15)
16
17// CallAndRecover calls the function f and returns the result of recover().
18// This minimizes the scope of the deferred recover, to ensure f is actually the
19// function that paniced.
20func CallAndRecover(f func()) (result interface{}) {
21 defer func() {
22 result = recover()
23 }()
24 f()
25 return
26}
27
28func expectErr(t *testing.T, err error, wantstr string, format string, args ...interface{}) bool {
29 gotstr := fmt.Sprint(err)
30 msg := fmt.Sprintf(format, args...)
31 if wantstr != "" && !strings.Contains(gotstr, wantstr) {
32 t.Errorf(`%s got error %q, want substr %q`, msg, gotstr, wantstr)
33 return false
34 }
35 if wantstr == "" && err != nil {
36 t.Errorf(`%s got error %q, want nil`, msg, gotstr)
37 return false
38 }
39 return true
40}
41
42func expectPanic(t *testing.T, f func(), wantstr string, format string, args ...interface{}) {
43 got := CallAndRecover(f)
44 gotstr := fmt.Sprint(got)
45 msg := fmt.Sprintf(format, args...)
46 if wantstr != "" && !strings.Contains(gotstr, wantstr) {
47 t.Errorf(`%s got panic %q, want substr %q`, msg, gotstr, wantstr)
48 }
49 if wantstr == "" && got != nil {
50 t.Errorf(`%s got panic %q, want nil`, msg, gotstr)
51 }
52}
53
54func expectMismatchedKind(t *testing.T, f func()) {
55 expectPanic(t, f, "mismatched kind", "")
56}
57
58// Define a bunch of regular Go types used in tests.
59type (
60 // Scalars
61 nInterface interface{}
62 nType *vdl.Type
63 nBool bool
64 nUint8 uint8
65 nUint16 uint16
66 nUint32 uint32
67 nUint64 uint64
68 nUint uint
69 nUintptr uintptr
70 nInt8 int8
71 nInt16 int16
72 nInt32 int32
73 nInt64 int64
74 nInt int
75 nFloat32 float32
76 nFloat64 float64
77 nComplex64 complex64
78 nComplex128 complex128
79 nString string
80 // Arrays
81 nArray3Interface [3]nInterface
82 nArray3TypeObject [3]*vdl.Type
83 nArray3Bool [3]bool
84 nArray3Uint8 [3]uint8
85 nArray3Uint16 [3]uint16
86 nArray3Uint32 [3]uint32
87 nArray3Uint64 [3]uint64
88 nArray3Uint [3]uint
89 nArray3Uintptr [3]uintptr
90 nArray3Int8 [3]int8
91 nArray3Int16 [3]int16
92 nArray3Int32 [3]int32
93 nArray3Int64 [3]int64
94 nArray3Int [3]int
95 nArray3Float32 [3]float32
96 nArray3Float64 [3]float64
97 nArray3Complex64 [3]complex64
98 nArray3Complex128 [3]complex128
99 nArray3String [3]string
100 // Structs
101 nStructInterface struct{ X nInterface }
102 nStructTypeObject struct{ X *vdl.Type }
103 nStructBool struct{ X bool }
104 nStructUint8 struct{ X uint8 }
105 nStructUint16 struct{ X uint16 }
106 nStructUint32 struct{ X uint32 }
107 nStructUint64 struct{ X uint64 }
108 nStructUint struct{ X uint }
109 nStructUintptr struct{ X uintptr }
110 nStructInt8 struct{ X int8 }
111 nStructInt16 struct{ X int16 }
112 nStructInt32 struct{ X int32 }
113 nStructInt64 struct{ X int64 }
114 nStructInt struct{ X int }
115 nStructFloat32 struct{ X float32 }
116 nStructFloat64 struct{ X float64 }
117 nStructComplex64 struct{ X complex64 }
118 nStructComplex128 struct{ X complex128 }
119 nStructString struct{ X string }
120 // Slices
121 nSliceInterface []nInterface
122 nSliceTypeObject []*vdl.Type
123 nSliceBool []bool
124 nSliceUint8 []uint8
125 nSliceUint16 []uint16
126 nSliceUint32 []uint32
127 nSliceUint64 []uint64
128 nSliceUint []uint
129 nSliceUintptr []uintptr
130 nSliceInt8 []int8
131 nSliceInt16 []int16
132 nSliceInt32 []int32
133 nSliceInt64 []int64
134 nSliceInt []int
135 nSliceFloat32 []float32
136 nSliceFloat64 []float64
137 nSliceComplex64 []complex64
138 nSliceComplex128 []complex128
139 nSliceString []string
140 // Sets
141 nSetInterface map[nInterface]struct{}
142 nSetTypeObject map[*vdl.Type]struct{}
143 nSetBool map[bool]struct{}
144 nSetUint8 map[uint8]struct{}
145 nSetUint16 map[uint16]struct{}
146 nSetUint32 map[uint32]struct{}
147 nSetUint64 map[uint64]struct{}
148 nSetUint map[uint]struct{}
149 nSetUintptr map[uintptr]struct{}
150 nSetInt8 map[int8]struct{}
151 nSetInt16 map[int16]struct{}
152 nSetInt32 map[int32]struct{}
153 nSetInt64 map[int64]struct{}
154 nSetInt map[int]struct{}
155 nSetFloat32 map[float32]struct{}
156 nSetFloat64 map[float64]struct{}
157 nSetComplex64 map[complex64]struct{}
158 nSetComplex128 map[complex128]struct{}
159 nSetString map[string]struct{}
160 // Maps
161 nMapInterface map[nInterface]nInterface
162 nMapTypeObject map[*vdl.Type]*vdl.Type
163 nMapBool map[bool]bool
164 nMapUint8 map[uint8]uint8
165 nMapUint16 map[uint16]uint16
166 nMapUint32 map[uint32]uint32
167 nMapUint64 map[uint64]uint64
168 nMapUint map[uint]uint
169 nMapUintptr map[uintptr]uintptr
170 nMapInt8 map[int8]int8
171 nMapInt16 map[int16]int16
172 nMapInt32 map[int32]int32
173 nMapInt64 map[int64]int64
174 nMapInt map[int]int
175 nMapFloat32 map[float32]float32
176 nMapFloat64 map[float64]float64
177 nMapComplex64 map[complex64]complex64
178 nMapComplex128 map[complex128]complex128
179 nMapString map[string]string
180 // Recursive types
181 nRecurseSelf struct{ X []nRecurseSelf }
182 nRecurseA struct{ B []nRecurseB }
183 nRecurseB struct{ A []nRecurseA }
184
185 // Composite types representing sets of numbers.
186 nMapUint64Empty map[nUint64]struct{}
187 nMapInt64Empty map[nUint64]struct{}
188 nMapFloat64Empty map[nUint64]struct{}
189 nMapComplex64Empty map[nUint64]struct{}
190 nMapUint64Bool map[nUint64]nBool
191 nMapInt64Bool map[nInt64]nBool
192 nMapFloat64Bool map[nFloat64]nBool
193 nMapComplex64Bool map[nComplex64]nBool
194 // Composite types representing sets of strings.
195 nMapStringEmpty map[nString]struct{}
196 nMapStringBool map[nString]nBool
197 nStructXYZBool struct{ X, Y, Z nBool }
198 nStructWXBool struct{ W, X nBool }
199 // Composite types representing maps of strings to numbers.
200 nMapStringUint64 map[nString]nUint64
201 nMapStringInt64 map[nString]nInt64
202 nMapStringFloat64 map[nString]nFloat64
203 nMapStringComplex64 map[nString]nComplex64
204 nStructVWXUint64 struct{ V, W, X nUint64 }
205 nStructVWXInt64 struct{ V, W, X nInt64 }
206 nStructVWXFloat64 struct{ V, W, X nFloat64 }
207 nStructVWXComplex64 struct{ V, W, X nComplex64 }
208 nStructUVUint64 struct{ U, V nUint64 }
209 nStructUVInt64 struct{ U, V nInt64 }
210 nStructUVFloat64 struct{ U, V nFloat64 }
211 nStructUVComplex64 struct{ U, V nComplex64 }
212 // Types that cannot be converted to sets. We represent sets as
213 // map[key]struct{} on the Go side, but don't allow map[key]nEmpty.
214 nEmpty struct{}
215 nMapStringnEmpty map[nString]nEmpty
216 nStructXYZEmpty struct{ X, Y, Z struct{} }
217 nStructXYZnEmpty struct{ X, Y, Z nEmpty }
218)
219
220func recurseSelfType() *vdl.Type {
221 var builder vdl.TypeBuilder
222 n := builder.Named("v.io/v23/vdl.nRecurseSelf")
223 n.AssignBase(builder.Struct().AppendField("X", builder.List().AssignElem(n)))
224 builder.Build()
225 t, err := n.Built()
226 if err != nil {
227 panic(err)
228 }
229 return t
230}
231
232func recurseABTypes() [2]*vdl.Type {
233 var builder vdl.TypeBuilder
234 a := builder.Named("v.io/v23/vdl.nRecurseA")
235 b := builder.Named("v.io/v23/vdl.nRecurseB")
236 a.AssignBase(builder.Struct().AppendField("B", builder.List().AssignElem(b)))
237 b.AssignBase(builder.Struct().AppendField("A", builder.List().AssignElem(a)))
238 builder.Build()
239 aT, err := a.Built()
240 if err != nil {
241 panic(err)
242 }
243 bT, err := b.Built()
244 if err != nil {
245 panic(err)
246 }
247 return [2]*vdl.Type{aT, bT}
248}
249
250func recurseAType() *vdl.Type { return recurseABTypes()[0] }
251func recurseBType() *vdl.Type { return recurseABTypes()[1] }
252
253// Define a bunch of *Type types used in tests.
254var (
255 // Named scalar types
256 boolTypeN = vdl.NamedType("nBool", vdl.BoolType)
257 nByteType = vdl.NamedType("nByte", vdl.ByteType)
258 uint16TypeN = vdl.NamedType("nUint16", vdl.Uint16Type)
259 uint32TypeN = vdl.NamedType("nUint32", vdl.Uint32Type)
260 uint64TypeN = vdl.NamedType("nUint64", vdl.Uint64Type)
261 int16TypeN = vdl.NamedType("nInt16", vdl.Int16Type)
262 int32TypeN = vdl.NamedType("nInt32", vdl.Int32Type)
263 int64TypeN = vdl.NamedType("nInt64", vdl.Int64Type)
264 float32TypeN = vdl.NamedType("nFloat32", vdl.Float32Type)
265 float64TypeN = vdl.NamedType("nFloat64", vdl.Float64Type)
266 complex64TypeN = vdl.NamedType("nComplex64", vdl.Complex64Type)
267 complex128TypeN = vdl.NamedType("nComplex128", vdl.Complex128Type)
268 stringTypeN = vdl.NamedType("nString", vdl.StringType)
269
270 // Composite types representing strings and bytes.
271 bytesType = vdl.ListType(vdl.ByteType)
272 bytesTypeN = vdl.NamedType("nBytes", bytesType)
273 bytes3Type = vdl.ArrayType(3, vdl.ByteType)
274 bytes3TypeN = vdl.NamedType("nBytes3", bytes3Type)
275 // Composite types representing sequences of numbers.
276 array3Uint64Type = vdl.ArrayType(3, vdl.Uint64Type)
277 array3Uint64TypeN = vdl.NamedType("nArray3Uint64", vdl.ArrayType(3, uint64TypeN))
278 array3Int64Type = vdl.ArrayType(3, vdl.Int64Type)
279 array3Int64TypeN = vdl.NamedType("nArray3Int64", vdl.ArrayType(3, int64TypeN))
280 array3Float64Type = vdl.ArrayType(3, vdl.Float64Type)
281 array3Float64TypeN = vdl.NamedType("nArray3Float64", vdl.ArrayType(3, float64TypeN))
282 array3Complex64Type = vdl.ArrayType(3, vdl.Complex64Type)
283 array3Complex64TypeN = vdl.NamedType("nArray3Complex64", vdl.ArrayType(3, complex64TypeN))
284 listUint64Type = vdl.ListType(vdl.Uint64Type)
285 listUint64TypeN = vdl.NamedType("nListUint64", vdl.ListType(uint64TypeN))
286 listInt64Type = vdl.ListType(vdl.Int64Type)
287 listInt64TypeN = vdl.NamedType("nListInt64", vdl.ListType(int64TypeN))
288 listFloat64Type = vdl.ListType(vdl.Float64Type)
289 listFloat64TypeN = vdl.NamedType("nListFloat64", vdl.ListType(float64TypeN))
290 listComplex64Type = vdl.ListType(vdl.Complex64Type)
291 listComplex64TypeN = vdl.NamedType("nListComplex64", vdl.ListType(complex64TypeN))
292 // Composite types representing sets of numbers.
293 setUint64Type = vdl.SetType(vdl.Uint64Type)
294 setUint64TypeN = vdl.NamedType("nSetUint64", vdl.SetType(uint64TypeN))
295 setInt64Type = vdl.SetType(vdl.Int64Type)
296 setInt64TypeN = vdl.NamedType("nSetInt64", vdl.SetType(int64TypeN))
297 setFloat64Type = vdl.SetType(vdl.Float64Type)
298 setFloat64TypeN = vdl.NamedType("nSetFloat64", vdl.SetType(float64TypeN))
299 setComplex64Type = vdl.SetType(vdl.Complex64Type)
300 setComplex64TypeN = vdl.NamedType("nSetComplex64", vdl.SetType(complex64TypeN))
301 mapUint64BoolType = vdl.MapType(vdl.Uint64Type, vdl.BoolType)
302 mapUint64BoolTypeN = vdl.NamedType("nMapUint64Bool", vdl.MapType(uint64TypeN, boolTypeN))
303 mapInt64BoolType = vdl.MapType(vdl.Int64Type, vdl.BoolType)
304 mapInt64BoolTypeN = vdl.NamedType("nMapInt64Bool", vdl.MapType(int64TypeN, boolTypeN))
305 mapFloat64BoolType = vdl.MapType(vdl.Float64Type, vdl.BoolType)
306 mapFloat64BoolTypeN = vdl.NamedType("nMapFloat64Bool", vdl.MapType(float64TypeN, boolTypeN))
307 mapComplex64BoolType = vdl.MapType(vdl.Complex64Type, vdl.BoolType)
308 mapComplex64BoolTypeN = vdl.NamedType("nMapComplex64Bool", vdl.MapType(complex64TypeN, boolTypeN))
309 // Composite types representing sets of strings.
310 setStringType = vdl.SetType(vdl.StringType)
311 setStringTypeN = vdl.NamedType("nSetString", vdl.SetType(stringTypeN))
312 mapStringBoolType = vdl.MapType(vdl.StringType, vdl.BoolType)
313 mapStringBoolTypeN = vdl.NamedType("nMapStringBool", vdl.MapType(stringTypeN, boolTypeN))
Jiri Simsad9a7b3c2015-08-12 16:38:27 -0700314 structXYZBoolType = vdl.StructType(vdl.Field{Name: "X", Type: vdl.BoolType}, vdl.Field{Name: "Y", Type: vdl.BoolType}, vdl.Field{Name: "Z", Type: vdl.BoolType})
315 structXYZBoolTypeN = vdl.NamedType("nStructXYZBool", vdl.StructType(vdl.Field{Name: "X", Type: boolTypeN}, vdl.Field{Name: "Y", Type: boolTypeN}, vdl.Field{Name: "Z", Type: boolTypeN}))
316 structWXBoolType = vdl.StructType(vdl.Field{Name: "W", Type: vdl.BoolType}, vdl.Field{Name: "X", Type: vdl.BoolType})
317 structWXBoolTypeN = vdl.NamedType("nStructWXBool", vdl.StructType(vdl.Field{Name: "W", Type: boolTypeN}, vdl.Field{Name: "X", Type: boolTypeN}))
Todd Wang232d6492015-02-25 18:04:54 -0800318 // Composite types representing maps of strings to numbers.
319 mapStringUint64Type = vdl.MapType(vdl.StringType, vdl.Uint64Type)
320 mapStringUint64TypeN = vdl.NamedType("nMapStringUint64", vdl.MapType(stringTypeN, uint64TypeN))
321 mapStringInt64Type = vdl.MapType(vdl.StringType, vdl.Int64Type)
322 mapStringInt64TypeN = vdl.NamedType("nMapStringInt64", vdl.MapType(stringTypeN, int64TypeN))
323 mapStringFloat64Type = vdl.MapType(vdl.StringType, vdl.Float64Type)
324 mapStringFloat64TypeN = vdl.NamedType("nMapStringFloat64", vdl.MapType(stringTypeN, float64TypeN))
325 mapStringComplex64Type = vdl.MapType(vdl.StringType, vdl.Complex64Type)
326 mapStringComplex64TypeN = vdl.NamedType("nMapStringComplex64", vdl.MapType(stringTypeN, complex64TypeN))
Jiri Simsad9a7b3c2015-08-12 16:38:27 -0700327 structVWXUint64Type = vdl.StructType(vdl.Field{Name: "V", Type: vdl.Uint64Type}, vdl.Field{Name: "W", Type: vdl.Uint64Type}, vdl.Field{Name: "X", Type: vdl.Uint64Type})
328 structVWXUint64TypeN = vdl.NamedType("nStructVWXUint64", vdl.StructType(vdl.Field{Name: "V", Type: uint64TypeN}, vdl.Field{Name: "W", Type: uint64TypeN}, vdl.Field{Name: "X", Type: uint64TypeN}))
329 structVWXInt64Type = vdl.StructType(vdl.Field{Name: "V", Type: vdl.Int64Type}, vdl.Field{Name: "W", Type: vdl.Int64Type}, vdl.Field{Name: "X", Type: vdl.Int64Type})
330 structVWXInt64TypeN = vdl.NamedType("nStructVWXInt64", vdl.StructType(vdl.Field{Name: "V", Type: int64TypeN}, vdl.Field{Name: "W", Type: int64TypeN}, vdl.Field{Name: "X", Type: int64TypeN}))
331 structVWXFloat64Type = vdl.StructType(vdl.Field{Name: "V", Type: vdl.Float64Type}, vdl.Field{Name: "W", Type: vdl.Float64Type}, vdl.Field{Name: "X", Type: vdl.Float64Type})
332 structVWXFloat64TypeN = vdl.NamedType("nStructVWXFloat64", vdl.StructType(vdl.Field{Name: "V", Type: float64TypeN}, vdl.Field{Name: "W", Type: float64TypeN}, vdl.Field{Name: "X", Type: float64TypeN}))
333 structVWXComplex64Type = vdl.StructType(vdl.Field{Name: "V", Type: vdl.Complex64Type}, vdl.Field{Name: "W", Type: vdl.Complex64Type}, vdl.Field{Name: "X", Type: vdl.Complex64Type})
334 structVWXComplex64TypeN = vdl.NamedType("nStructVWXComplex64", vdl.StructType(vdl.Field{Name: "V", Type: complex64TypeN}, vdl.Field{Name: "W", Type: complex64TypeN}, vdl.Field{Name: "X", Type: complex64TypeN}))
335 structUVUint64Type = vdl.StructType(vdl.Field{Name: "U", Type: vdl.Uint64Type}, vdl.Field{Name: "V", Type: vdl.Uint64Type})
336 structUVUint64TypeN = vdl.NamedType("nStructUVUint64", vdl.StructType(vdl.Field{Name: "U", Type: uint64TypeN}, vdl.Field{Name: "V", Type: uint64TypeN}))
337 structUVInt64Type = vdl.StructType(vdl.Field{Name: "U", Type: vdl.Int64Type}, vdl.Field{Name: "V", Type: vdl.Int64Type})
338 structUVInt64TypeN = vdl.NamedType("nStructUVInt64", vdl.StructType(vdl.Field{Name: "U", Type: int64TypeN}, vdl.Field{Name: "V", Type: int64TypeN}))
339 structUVFloat64Type = vdl.StructType(vdl.Field{Name: "U", Type: vdl.Float64Type}, vdl.Field{Name: "V", Type: vdl.Float64Type})
340 structUVFloat64TypeN = vdl.NamedType("nStructUVFloat64", vdl.StructType(vdl.Field{Name: "U", Type: float64TypeN}, vdl.Field{Name: "V", Type: float64TypeN}))
341 structUVComplex64Type = vdl.StructType(vdl.Field{Name: "U", Type: vdl.Complex64Type}, vdl.Field{Name: "V", Type: vdl.Complex64Type})
342 structUVComplex64TypeN = vdl.NamedType("nStructUVComplex64", vdl.StructType(vdl.Field{Name: "U", Type: complex64TypeN}, vdl.Field{Name: "V", Type: complex64TypeN}))
Todd Wang232d6492015-02-25 18:04:54 -0800343
Jiri Simsad9a7b3c2015-08-12 16:38:27 -0700344 structAIntType = vdl.StructType(vdl.Field{Name: "A", Type: vdl.Int64Type})
Todd Wang232d6492015-02-25 18:04:54 -0800345 structAIntTypeN = vdl.NamedType("nStructA", structAIntType)
346
347 // Types that cannot be converted to sets. Although we represent sets as
348 // map[key]struct{} on the Go side, we don't allow these as general
349 // conversions for val.Value.
350 emptyType = vdl.StructType()
351 emptyTypeN = vdl.NamedType("nEmpty", vdl.StructType())
352 mapStringEmptyType = vdl.MapType(vdl.StringType, emptyType)
353 mapStringEmptyTypeN = vdl.NamedType("nMapStringEmpty", vdl.MapType(stringTypeN, emptyTypeN))
Jiri Simsad9a7b3c2015-08-12 16:38:27 -0700354 structXYZEmptyType = vdl.StructType(vdl.Field{Name: "X", Type: emptyType}, vdl.Field{Name: "Y", Type: emptyType}, vdl.Field{Name: "Z", Type: emptyType})
355 structXYZEmptyTypeN = vdl.NamedType("nStructXYZEmpty", vdl.StructType(vdl.Field{Name: "X", Type: emptyTypeN}, vdl.Field{Name: "Y", Type: emptyTypeN}, vdl.Field{Name: "Z", Type: emptyTypeN}))
Todd Wang232d6492015-02-25 18:04:54 -0800356)
357
358func anyValue(x *vdl.Value) *vdl.Value { return vdl.ZeroValue(vdl.AnyType).Assign(x) }
359func boolValue(t *vdl.Type, x bool) *vdl.Value { return vdl.ZeroValue(t).AssignBool(x) }
Todd Wang232d6492015-02-25 18:04:54 -0800360func uintValue(t *vdl.Type, x uint64) *vdl.Value { return vdl.ZeroValue(t).AssignUint(x) }
361func intValue(t *vdl.Type, x int64) *vdl.Value { return vdl.ZeroValue(t).AssignInt(x) }
362func floatValue(t *vdl.Type, x float64) *vdl.Value { return vdl.ZeroValue(t).AssignFloat(x) }
363func complexValue(t *vdl.Type, x complex128) *vdl.Value { return vdl.ZeroValue(t).AssignComplex(x) }
364func stringValue(t *vdl.Type, x string) *vdl.Value { return vdl.ZeroValue(t).AssignString(x) }
365func bytesValue(t *vdl.Type, x string) *vdl.Value { return vdl.ZeroValue(t).AssignBytes([]byte(x)) }
366func bytes3Value(t *vdl.Type, x string) *vdl.Value { return vdl.ZeroValue(t).CopyBytes([]byte(x)) }
367
368func setStringValue(t *vdl.Type, x ...string) *vdl.Value {
369 res := vdl.ZeroValue(t)
370 for _, vx := range x {
371 key := vdl.ZeroValue(t.Key()).AssignString(vx)
372 res.AssignSetKey(key)
373 }
374 return res
375}
376
377type sb struct {
378 s string
379 b bool
380}
381
382func mapStringBoolValue(t *vdl.Type, x ...sb) *vdl.Value {
383 res := vdl.ZeroValue(t)
384 for _, sb := range x {
385 key := vdl.ZeroValue(t.Key()).AssignString(sb.s)
386 val := vdl.ZeroValue(t.Elem()).AssignBool(sb.b)
387 res.AssignMapIndex(key, val)
388 }
389 return res
390}
391
392func mapStringEmptyValue(t *vdl.Type, x ...string) *vdl.Value {
393 res := vdl.ZeroValue(t)
394 for _, vx := range x {
395 key := vdl.ZeroValue(t.Key()).AssignString(vx)
396 val := vdl.ZeroValue(t.Elem())
397 res.AssignMapIndex(key, val)
398 }
399 return res
400}
401
402func structBoolValue(t *vdl.Type, x ...sb) *vdl.Value {
403 res := vdl.ZeroValue(t)
404 for _, sb := range x {
405 _, index := t.FieldByName(sb.s)
406 res.StructField(index).AssignBool(sb.b)
407 }
408 return res
409}
410
411func assignNum(v *vdl.Value, num float64) *vdl.Value {
412 switch v.Kind() {
Benjamin Prosnitzc177af12015-11-13 12:55:29 -0800413 case vdl.Byte, vdl.Uint16, vdl.Uint32, vdl.Uint64:
Todd Wang232d6492015-02-25 18:04:54 -0800414 v.AssignUint(uint64(num))
Benjamin Prosnitzc177af12015-11-13 12:55:29 -0800415 case vdl.Int8, vdl.Int16, vdl.Int32, vdl.Int64:
Todd Wang232d6492015-02-25 18:04:54 -0800416 v.AssignInt(int64(num))
417 case vdl.Float32, vdl.Float64:
418 v.AssignFloat(num)
419 case vdl.Complex64, vdl.Complex128:
420 v.AssignComplex(complex(num, 0))
421 default:
422 panic(fmt.Errorf("val: assignNum unhandled %v", v.Type()))
423 }
424 return v
425}
426
427func seqNumValue(t *vdl.Type, x ...float64) *vdl.Value {
428 res := vdl.ZeroValue(t)
429 if t.Kind() == vdl.List {
430 res.AssignLen(len(x))
431 }
432 for index, n := range x {
433 assignNum(res.Index(index), n)
434 }
435 return res
436}
437
438func setNumValue(t *vdl.Type, x ...float64) *vdl.Value {
439 res := vdl.ZeroValue(t)
440 for _, n := range x {
441 res.AssignSetKey(assignNum(vdl.ZeroValue(t.Key()), n))
442 }
443 return res
444}
445
446type nb struct {
447 n float64
448 b bool
449}
450
451func mapNumBoolValue(t *vdl.Type, x ...nb) *vdl.Value {
452 res := vdl.ZeroValue(t)
453 for _, nb := range x {
454 key := assignNum(vdl.ZeroValue(t.Key()), nb.n)
455 val := vdl.ZeroValue(t.Elem()).AssignBool(nb.b)
456 res.AssignMapIndex(key, val)
457 }
458 return res
459}
460
461type sn struct {
462 s string
463 n float64
464}
465
466func mapStringNumValue(t *vdl.Type, x ...sn) *vdl.Value {
467 res := vdl.ZeroValue(t)
468 for _, sn := range x {
469 key := vdl.ZeroValue(t.Key()).AssignString(sn.s)
470 val := assignNum(vdl.ZeroValue(t.Elem()), sn.n)
471 res.AssignMapIndex(key, val)
472 }
473 return res
474}
475
476func structNumValue(t *vdl.Type, x ...sn) *vdl.Value {
477 res := vdl.ZeroValue(t)
478 for _, sn := range x {
479 _, index := t.FieldByName(sn.s)
480 assignNum(res.StructField(index), sn.n)
481 }
482 return res
483}