protobuf-go/internal/impl/pointer_reflect.go
Joe Tsai fa02f4eaa6 internal/impl: initial commit
This provides an implementation of the has, get, set, clear methods for each
field in a message. The approach taken here is similar to the table-driven
implementation in the current v1 proto package.

The pointer_reflect.go and pointer_unsafe.go files are a simplified version of
the same files in the v1 implementation. They provide a pointer abstraction
that enables a high-efficiency approach in a non-purego environment.
The unsafe fast-path is not implemented in this commit.

This commit only implements the accessor methods for scalars using pure
Go reflection.

Change-Id: Icdf707e9d4e3385e55434f93b30a341a7680ae11
Reviewed-on: https://go-review.googlesource.com/135136
Reviewed-by: Damien Neil <dneil@google.com>
2018-09-13 20:23:15 +00:00

48 lines
1.3 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build purego
package impl
import (
"fmt"
"reflect"
)
// offset represents the offset to a struct field, accessible from a pointer.
// The offset is the field index into a struct.
type offset []int
// offsetOf returns a field offset for the struct field.
func offsetOf(f reflect.StructField) offset {
if len(f.Index) != 1 {
panic("embedded structs are not supported")
}
return f.Index
}
// pointer is an abstract representation of a pointer to a struct or field.
type pointer struct{ v reflect.Value }
// pointerOfValue returns v as a pointer.
func pointerOfValue(v reflect.Value) pointer {
return pointer{v: v}
}
// apply adds an offset to the pointer to derive a new pointer
// to a specified field. The current pointer must be pointing at a struct.
func (p pointer) apply(f offset) pointer {
// TODO: Handle unexported fields in an API that hides XXX fields?
return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
}
// asType treats p as a pointer to an object of type t and returns the value.
func (p pointer) asType(t reflect.Type) reflect.Value {
if p.v.Type().Elem() != t {
panic(fmt.Sprintf("invalid type: got %v, want %v", p.v.Type(), t))
}
return p.v
}