blob: 42c387abd5d2b1b81e9ffeb248b1ae9924825c4a [file] [log] [blame]
Russ Coxd4ce3f12012-09-12 10:36:26 +10001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2012 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
Russ Coxd4ce3f12012-09-12 10:36:26 +10005//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
David Symondsd110e692014-06-16 15:40:44 +100032// +build appengine,!appenginevm
Russ Coxd4ce3f12012-09-12 10:36:26 +100033
34// This file contains an implementation of proto field accesses using package reflect.
35// It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
36// be used on App Engine.
37
38package proto
39
40import (
41 "math"
42 "reflect"
43)
44
45// A structPointer is a pointer to a struct.
46type structPointer struct {
47 v reflect.Value
48}
49
50// toStructPointer returns a structPointer equivalent to the given reflect value.
51// The reflect value must itself be a pointer to a struct.
52func toStructPointer(v reflect.Value) structPointer {
53 return structPointer{v}
54}
55
56// IsNil reports whether p is nil.
57func structPointer_IsNil(p structPointer) bool {
58 return p.v.IsNil()
59}
60
61// Interface returns the struct pointer as an interface value.
62func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
63 return p.v.Interface()
64}
65
66// A field identifies a field in a struct, accessible from a structPointer.
67// In this implementation, a field is identified by the sequence of field indices
68// passed to reflect's FieldByIndex.
69type field []int
70
71// toField returns a field equivalent to the given reflect field.
72func toField(f *reflect.StructField) field {
73 return f.Index
74}
75
76// invalidField is an invalid field identifier.
77var invalidField = field(nil)
78
79// IsValid reports whether the field identifier is valid.
80func (f field) IsValid() bool { return f != nil }
81
82// field returns the given field in the struct as a reflect value.
83func structPointer_field(p structPointer, f field) reflect.Value {
84 // Special case: an extension map entry with a value of type T
85 // passes a *T to the struct-handling code with a zero field,
86 // expecting that it will be treated as equivalent to *struct{ X T },
87 // which has the same memory layout. We have to handle that case
88 // specially, because reflect will panic if we call FieldByIndex on a
89 // non-struct.
90 if f == nil {
91 return p.v.Elem()
92 }
93
94 return p.v.Elem().FieldByIndex(f)
95}
96
97// ifield returns the given field in the struct as an interface value.
98func structPointer_ifield(p structPointer, f field) interface{} {
99 return structPointer_field(p, f).Addr().Interface()
100}
101
102// Bytes returns the address of a []byte field in the struct.
103func structPointer_Bytes(p structPointer, f field) *[]byte {
104 return structPointer_ifield(p, f).(*[]byte)
105}
106
107// BytesSlice returns the address of a [][]byte field in the struct.
108func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
109 return structPointer_ifield(p, f).(*[][]byte)
110}
111
112// Bool returns the address of a *bool field in the struct.
113func structPointer_Bool(p structPointer, f field) **bool {
114 return structPointer_ifield(p, f).(**bool)
115}
116
David Symondsabd3b412014-11-28 11:43:44 +1100117// BoolVal returns the address of a bool field in the struct.
118func structPointer_BoolVal(p structPointer, f field) *bool {
119 return structPointer_ifield(p, f).(*bool)
120}
121
Russ Coxd4ce3f12012-09-12 10:36:26 +1000122// BoolSlice returns the address of a []bool field in the struct.
123func structPointer_BoolSlice(p structPointer, f field) *[]bool {
124 return structPointer_ifield(p, f).(*[]bool)
125}
126
127// String returns the address of a *string field in the struct.
128func structPointer_String(p structPointer, f field) **string {
129 return structPointer_ifield(p, f).(**string)
130}
131
David Symondsabd3b412014-11-28 11:43:44 +1100132// StringVal returns the address of a string field in the struct.
133func structPointer_StringVal(p structPointer, f field) *string {
134 return structPointer_ifield(p, f).(*string)
135}
136
Russ Coxd4ce3f12012-09-12 10:36:26 +1000137// StringSlice returns the address of a []string field in the struct.
138func structPointer_StringSlice(p structPointer, f field) *[]string {
139 return structPointer_ifield(p, f).(*[]string)
140}
141
142// ExtMap returns the address of an extension map field in the struct.
143func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
144 return structPointer_ifield(p, f).(*map[int32]Extension)
145}
146
147// SetStructPointer writes a *struct field in the struct.
148func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
149 structPointer_field(p, f).Set(q.v)
150}
151
152// GetStructPointer reads a *struct field in the struct.
153func structPointer_GetStructPointer(p structPointer, f field) structPointer {
154 return structPointer{structPointer_field(p, f)}
155}
156
157// StructPointerSlice the address of a []*struct field in the struct.
158func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
159 return structPointerSlice{structPointer_field(p, f)}
160}
161
162// A structPointerSlice represents the address of a slice of pointers to structs
163// (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
164type structPointerSlice struct {
165 v reflect.Value
166}
167
168func (p structPointerSlice) Len() int { return p.v.Len() }
169func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
170func (p structPointerSlice) Append(q structPointer) {
171 p.v.Set(reflect.Append(p.v, q.v))
172}
173
174var (
175 int32Type = reflect.TypeOf(int32(0))
176 uint32Type = reflect.TypeOf(uint32(0))
177 float32Type = reflect.TypeOf(float32(0))
178 int64Type = reflect.TypeOf(int64(0))
179 uint64Type = reflect.TypeOf(uint64(0))
180 float64Type = reflect.TypeOf(float64(0))
181)
182
183// A word32 represents a field of type *int32, *uint32, *float32, or *enum.
184// That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
185type word32 struct {
186 v reflect.Value
187}
188
189// IsNil reports whether p is nil.
190func word32_IsNil(p word32) bool {
191 return p.v.IsNil()
192}
193
194// Set sets p to point at a newly allocated word with bits set to x.
195func word32_Set(p word32, o *Buffer, x uint32) {
196 t := p.v.Type().Elem()
197 switch t {
198 case int32Type:
199 if len(o.int32s) == 0 {
200 o.int32s = make([]int32, uint32PoolSize)
201 }
202 o.int32s[0] = int32(x)
203 p.v.Set(reflect.ValueOf(&o.int32s[0]))
204 o.int32s = o.int32s[1:]
205 return
206 case uint32Type:
207 if len(o.uint32s) == 0 {
208 o.uint32s = make([]uint32, uint32PoolSize)
209 }
210 o.uint32s[0] = x
211 p.v.Set(reflect.ValueOf(&o.uint32s[0]))
212 o.uint32s = o.uint32s[1:]
213 return
214 case float32Type:
215 if len(o.float32s) == 0 {
216 o.float32s = make([]float32, uint32PoolSize)
217 }
218 o.float32s[0] = math.Float32frombits(x)
219 p.v.Set(reflect.ValueOf(&o.float32s[0]))
220 o.float32s = o.float32s[1:]
221 return
222 }
223
224 // must be enum
225 p.v.Set(reflect.New(t))
226 p.v.Elem().SetInt(int64(int32(x)))
227}
228
229// Get gets the bits pointed at by p, as a uint32.
230func word32_Get(p word32) uint32 {
231 elem := p.v.Elem()
232 switch elem.Kind() {
233 case reflect.Int32:
234 return uint32(elem.Int())
235 case reflect.Uint32:
236 return uint32(elem.Uint())
237 case reflect.Float32:
238 return math.Float32bits(float32(elem.Float()))
239 }
240 panic("unreachable")
241}
242
243// Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
244func structPointer_Word32(p structPointer, f field) word32 {
245 return word32{structPointer_field(p, f)}
246}
247
David Symondsabd3b412014-11-28 11:43:44 +1100248// A word32Val represents a field of type int32, uint32, float32, or enum.
249// That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
250type word32Val struct {
251 v reflect.Value
252}
253
254// Set sets *p to x.
255func word32Val_Set(p word32Val, x uint32) {
256 switch p.v.Type() {
257 case int32Type:
258 p.v.SetInt(int64(x))
259 return
260 case uint32Type:
261 p.v.SetUint(uint64(x))
262 return
263 case float32Type:
264 p.v.SetFloat(float64(math.Float32frombits(x)))
265 return
266 }
267
268 // must be enum
269 p.v.SetInt(int64(int32(x)))
270}
271
272// Get gets the bits pointed at by p, as a uint32.
273func word32Val_Get(p word32Val) uint32 {
274 elem := p.v
275 switch elem.Kind() {
276 case reflect.Int32:
277 return uint32(elem.Int())
278 case reflect.Uint32:
279 return uint32(elem.Uint())
280 case reflect.Float32:
281 return math.Float32bits(float32(elem.Float()))
282 }
283 panic("unreachable")
284}
285
286// Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
287func structPointer_Word32Val(p structPointer, f field) word32Val {
288 return word32Val{structPointer_field(p, f)}
289}
290
Russ Coxd4ce3f12012-09-12 10:36:26 +1000291// A word32Slice is a slice of 32-bit values.
292// That is, v.Type() is []int32, []uint32, []float32, or []enum.
293type word32Slice struct {
294 v reflect.Value
295}
296
297func (p word32Slice) Append(x uint32) {
298 n, m := p.v.Len(), p.v.Cap()
299 if n < m {
300 p.v.SetLen(n + 1)
301 } else {
302 t := p.v.Type().Elem()
303 p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
304 }
305 elem := p.v.Index(n)
306 switch elem.Kind() {
307 case reflect.Int32:
308 elem.SetInt(int64(int32(x)))
309 case reflect.Uint32:
310 elem.SetUint(uint64(x))
311 case reflect.Float32:
312 elem.SetFloat(float64(math.Float32frombits(x)))
313 }
314}
315
316func (p word32Slice) Len() int {
317 return p.v.Len()
318}
319
320func (p word32Slice) Index(i int) uint32 {
321 elem := p.v.Index(i)
322 switch elem.Kind() {
323 case reflect.Int32:
324 return uint32(elem.Int())
325 case reflect.Uint32:
326 return uint32(elem.Uint())
327 case reflect.Float32:
328 return math.Float32bits(float32(elem.Float()))
329 }
330 panic("unreachable")
331}
332
333// Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
334func structPointer_Word32Slice(p structPointer, f field) word32Slice {
335 return word32Slice{structPointer_field(p, f)}
336}
337
338// word64 is like word32 but for 64-bit values.
339type word64 struct {
340 v reflect.Value
341}
342
343func word64_Set(p word64, o *Buffer, x uint64) {
344 t := p.v.Type().Elem()
345 switch t {
346 case int64Type:
347 if len(o.int64s) == 0 {
348 o.int64s = make([]int64, uint64PoolSize)
349 }
350 o.int64s[0] = int64(x)
351 p.v.Set(reflect.ValueOf(&o.int64s[0]))
352 o.int64s = o.int64s[1:]
353 return
354 case uint64Type:
355 if len(o.uint64s) == 0 {
356 o.uint64s = make([]uint64, uint64PoolSize)
357 }
358 o.uint64s[0] = x
359 p.v.Set(reflect.ValueOf(&o.uint64s[0]))
360 o.uint64s = o.uint64s[1:]
361 return
362 case float64Type:
363 if len(o.float64s) == 0 {
364 o.float64s = make([]float64, uint64PoolSize)
365 }
366 o.float64s[0] = math.Float64frombits(x)
367 p.v.Set(reflect.ValueOf(&o.float64s[0]))
368 o.float64s = o.float64s[1:]
369 return
370 }
371 panic("unreachable")
372}
373
374func word64_IsNil(p word64) bool {
375 return p.v.IsNil()
376}
377
378func word64_Get(p word64) uint64 {
379 elem := p.v.Elem()
380 switch elem.Kind() {
381 case reflect.Int64:
382 return uint64(elem.Int())
383 case reflect.Uint64:
384 return elem.Uint()
385 case reflect.Float64:
386 return math.Float64bits(elem.Float())
387 }
388 panic("unreachable")
389}
390
391func structPointer_Word64(p structPointer, f field) word64 {
392 return word64{structPointer_field(p, f)}
393}
394
David Symondsabd3b412014-11-28 11:43:44 +1100395// word64Val is like word32Val but for 64-bit values.
396type word64Val struct {
397 v reflect.Value
398}
399
400func word64Val_Set(p word64Val, o *Buffer, x uint64) {
401 switch p.v.Type() {
402 case int64Type:
403 p.v.SetInt(int64(x))
404 return
405 case uint64Type:
406 p.v.SetUint(x)
407 return
408 case float64Type:
409 p.v.SetFloat(math.Float64frombits(x))
410 return
411 }
412 panic("unreachable")
413}
414
415func word64Val_Get(p word64Val) uint64 {
416 elem := p.v
417 switch elem.Kind() {
418 case reflect.Int64:
419 return uint64(elem.Int())
420 case reflect.Uint64:
421 return elem.Uint()
422 case reflect.Float64:
423 return math.Float64bits(elem.Float())
424 }
425 panic("unreachable")
426}
427
428func structPointer_Word64Val(p structPointer, f field) word64Val {
429 return word64Val{structPointer_field(p, f)}
430}
431
Russ Coxd4ce3f12012-09-12 10:36:26 +1000432type word64Slice struct {
433 v reflect.Value
434}
435
436func (p word64Slice) Append(x uint64) {
437 n, m := p.v.Len(), p.v.Cap()
438 if n < m {
439 p.v.SetLen(n + 1)
440 } else {
441 t := p.v.Type().Elem()
442 p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
443 }
444 elem := p.v.Index(n)
445 switch elem.Kind() {
446 case reflect.Int64:
447 elem.SetInt(int64(int64(x)))
448 case reflect.Uint64:
449 elem.SetUint(uint64(x))
450 case reflect.Float64:
451 elem.SetFloat(float64(math.Float64frombits(x)))
452 }
453}
454
455func (p word64Slice) Len() int {
456 return p.v.Len()
457}
458
459func (p word64Slice) Index(i int) uint64 {
460 elem := p.v.Index(i)
461 switch elem.Kind() {
462 case reflect.Int64:
463 return uint64(elem.Int())
464 case reflect.Uint64:
465 return uint64(elem.Uint())
466 case reflect.Float64:
467 return math.Float64bits(float64(elem.Float()))
468 }
469 panic("unreachable")
470}
471
472func structPointer_Word64Slice(p structPointer, f field) word64Slice {
473 return word64Slice{structPointer_field(p, f)}
474}