/
type.go
122 lines (108 loc) · 3.76 KB
/
type.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vom
// TODO(toddw): Add tests.
import "v.io/v23/vdl"
// TODO(toddw): Provide type management routines
// Bootstrap mappings between type, id and kind.
var (
bootstrapWireTypes map[*vdl.Type]struct{}
bootstrapIDToType map[TypeId]*vdl.Type
bootstrapTypeToID map[*vdl.Type]TypeId
bootstrapKindToID map[vdl.Kind]TypeId
typeIDType = vdl.TypeOf(TypeId(0))
wireTypeType = vdl.TypeOf((*wireType)(nil))
wireNamedType = vdl.TypeOf(wireNamed{})
wireEnumType = vdl.TypeOf(wireEnum{})
wireArrayType = vdl.TypeOf(wireArray{})
wireListType = vdl.TypeOf(wireList{})
wireSetType = vdl.TypeOf(wireSet{})
wireMapType = vdl.TypeOf(wireMap{})
wireFieldType = vdl.TypeOf(wireField{})
wireFieldListType = vdl.TypeOf([]wireField{})
wireStructType = vdl.TypeOf(wireStruct{})
wireUnionType = vdl.TypeOf(wireUnion{})
wireOptionalType = vdl.TypeOf(wireOptional{})
wireByteListType = vdl.TypeOf([]byte{})
wireStringListType = vdl.TypeOf([]string{})
)
func init() {
bootstrapWireTypes = make(map[*vdl.Type]struct{})
// The basic wire types for type definition.
for _, tt := range []*vdl.Type{
typeIDType,
wireTypeType,
wireFieldType,
wireFieldListType,
} {
bootstrapWireTypes[tt] = struct{}{}
}
// The extra wire types for each kind of type definition. The field indices
// in wireType should not be changed.
wtTypes := []*vdl.Type{
wireNamedType,
wireEnumType,
wireArrayType,
wireListType,
wireSetType,
wireMapType,
wireStructType,
wireUnionType,
wireOptionalType,
}
if len(wtTypes) != wireTypeType.NumField() {
panic("vom: wireType definition changed")
}
for ix, tt := range wtTypes {
if tt != wireTypeType.Field(ix).Type {
panic("vom: wireType definition changed")
}
bootstrapWireTypes[tt] = struct{}{}
}
bootstrapIDToType = make(map[TypeId]*vdl.Type)
bootstrapTypeToID = make(map[*vdl.Type]TypeId)
bootstrapKindToID = make(map[vdl.Kind]TypeId)
// The basic bootstrap types can be converted between type, id and kind.
for id, tt := range map[TypeId]*vdl.Type{
WireIdBool: vdl.BoolType,
WireIdByte: vdl.ByteType,
WireIdString: vdl.StringType,
WireIdUint16: vdl.Uint16Type,
WireIdUint32: vdl.Uint32Type,
WireIdUint64: vdl.Uint64Type,
WireIdInt8: vdl.Int8Type,
WireIdInt16: vdl.Int16Type,
WireIdInt32: vdl.Int32Type,
WireIdInt64: vdl.Int64Type,
WireIdFloat32: vdl.Float32Type,
WireIdFloat64: vdl.Float64Type,
WireIdTypeObject: vdl.TypeObjectType,
WireIdAny: vdl.AnyType,
} {
bootstrapIDToType[id] = tt
bootstrapTypeToID[tt] = id
bootstrapKindToID[tt.Kind()] = id
}
// The extra bootstrap types can be converted between type and id.
for id, tt := range map[TypeId]*vdl.Type{
WireIdByteList: wireByteListType,
WireIdStringList: wireStringListType,
} {
bootstrapIDToType[id] = tt
bootstrapTypeToID[tt] = id
}
}
// A generic interface for all wireType types.
type wireTypeGeneric interface {
TypeName() string
}
func (wt wireTypeNamedT) TypeName() string { return wt.Value.Name }
func (wt wireTypeEnumT) TypeName() string { return wt.Value.Name }
func (wt wireTypeArrayT) TypeName() string { return wt.Value.Name }
func (wt wireTypeListT) TypeName() string { return wt.Value.Name }
func (wt wireTypeSetT) TypeName() string { return wt.Value.Name }
func (wt wireTypeMapT) TypeName() string { return wt.Value.Name }
func (wt wireTypeStructT) TypeName() string { return wt.Value.Name }
func (wt wireTypeUnionT) TypeName() string { return wt.Value.Name }
func (wt wireTypeOptionalT) TypeName() string { return wt.Value.Name }