...
1
2
3
4
5
6
7
8 package impl
9
10 import (
11 "fmt"
12 "reflect"
13 "sync"
14 )
15
16 const UnsafeEnabled = false
17
18
19 type Pointer interface{}
20
21
22
23 type offset struct {
24 index int
25 export exporter
26 }
27
28
29 func offsetOf(f reflect.StructField, x exporter) offset {
30 if len(f.Index) != 1 {
31 panic("embedded structs are not supported")
32 }
33 if f.PkgPath == "" {
34 return offset{index: f.Index[0]}
35 }
36 if x == nil {
37 panic("exporter must be provided for unexported field")
38 }
39 return offset{index: f.Index[0], export: x}
40 }
41
42
43 func (f offset) IsValid() bool { return f.index >= 0 }
44
45
46 var invalidOffset = offset{index: -1}
47
48
49 var zeroOffset = offset{index: 0}
50
51
52 type pointer struct{ v reflect.Value }
53
54
55 func pointerOf(p Pointer) pointer {
56 return pointerOfIface(p)
57 }
58
59
60 func pointerOfValue(v reflect.Value) pointer {
61 return pointer{v: v}
62 }
63
64
65 func pointerOfIface(v interface{}) pointer {
66 return pointer{v: reflect.ValueOf(v)}
67 }
68
69
70 func (p pointer) IsNil() bool {
71 return p.v.IsNil()
72 }
73
74
75
76 func (p pointer) Apply(f offset) pointer {
77 if f.export != nil {
78 if v := reflect.ValueOf(f.export(p.v.Interface(), f.index)); v.IsValid() {
79 return pointer{v: v}
80 }
81 }
82 return pointer{v: p.v.Elem().Field(f.index).Addr()}
83 }
84
85
86
87 func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
88 if got := p.v.Type().Elem(); got != t {
89 panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
90 }
91 return p.v
92 }
93
94
95
96 func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
97 return p.AsValueOf(t).Interface()
98 }
99
100 func (p pointer) Bool() *bool { return p.v.Interface().(*bool) }
101 func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) }
102 func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) }
103 func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) }
104 func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) }
105 func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) }
106 func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) }
107 func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) }
108 func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) }
109 func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) }
110 func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) }
111 func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) }
112 func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) }
113 func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) }
114 func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) }
115 func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) }
116 func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) }
117 func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
118 func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) }
119 func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) }
120 func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
121 func (p pointer) String() *string { return p.v.Interface().(*string) }
122 func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
123 func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
124 func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
125 func (p pointer) BytesPtr() **[]byte { return p.v.Interface().(**[]byte) }
126 func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
127 func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.v.Interface().(*WeakFields)) }
128 func (p pointer) Extensions() *map[int32]ExtensionField {
129 return p.v.Interface().(*map[int32]ExtensionField)
130 }
131
132 func (p pointer) Elem() pointer {
133 return pointer{v: p.v.Elem()}
134 }
135
136
137
138 func (p pointer) PointerSlice() []pointer {
139
140 if p.v.IsNil() {
141 return nil
142 }
143 n := p.v.Elem().Len()
144 s := make([]pointer, n)
145 for i := 0; i < n; i++ {
146 s[i] = pointer{v: p.v.Elem().Index(i)}
147 }
148 return s
149 }
150
151
152 func (p pointer) AppendPointerSlice(v pointer) {
153 sp := p.v.Elem()
154 sp.Set(reflect.Append(sp, v.v))
155 }
156
157
158 func (p pointer) SetPointer(v pointer) {
159 p.v.Elem().Set(v.v)
160 }
161
162 func growSlice(p pointer, addCap int) {
163
164 in := p.v.Elem()
165 out := reflect.MakeSlice(in.Type(), in.Len(), in.Len()+addCap)
166 reflect.Copy(out, in)
167 p.v.Elem().Set(out)
168 }
169
170 func (p pointer) growBoolSlice(addCap int) {
171 growSlice(p, addCap)
172 }
173
174 func (p pointer) growInt32Slice(addCap int) {
175 growSlice(p, addCap)
176 }
177
178 func (p pointer) growUint32Slice(addCap int) {
179 growSlice(p, addCap)
180 }
181
182 func (p pointer) growInt64Slice(addCap int) {
183 growSlice(p, addCap)
184 }
185
186 func (p pointer) growUint64Slice(addCap int) {
187 growSlice(p, addCap)
188 }
189
190 func (p pointer) growFloat64Slice(addCap int) {
191 growSlice(p, addCap)
192 }
193
194 func (p pointer) growFloat32Slice(addCap int) {
195 growSlice(p, addCap)
196 }
197
198 func (Export) MessageStateOf(p Pointer) *messageState { panic("not supported") }
199 func (ms *messageState) pointer() pointer { panic("not supported") }
200 func (ms *messageState) messageInfo() *MessageInfo { panic("not supported") }
201 func (ms *messageState) LoadMessageInfo() *MessageInfo { panic("not supported") }
202 func (ms *messageState) StoreMessageInfo(mi *MessageInfo) { panic("not supported") }
203
204 type atomicNilMessage struct {
205 once sync.Once
206 m messageReflectWrapper
207 }
208
209 func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
210 m.once.Do(func() {
211 m.m.p = pointerOfIface(reflect.Zero(mi.GoReflectType).Interface())
212 m.m.mi = mi
213 })
214 return &m.m
215 }
216
View as plain text