add reflect.Typeof; test for and fix nil interface bug in DeepEqual

R=r
DELTA=40  (30 added, 2 deleted, 8 changed)
OCL=30742
CL=30753
This commit is contained in:
Russ Cox 2009-06-25 14:25:38 -07:00
parent 6af5775d74
commit 4866223c2e
3 changed files with 38 additions and 10 deletions

View file

@ -942,9 +942,7 @@ func copyArray(dst ArrayValue, src ArrayValue, n int) {
}
}
// NewValue creates a new Value from the interface{} object provided.
func NewValue(e interface {}) Value {
value, typestring, indir := unsafe.Reflect(e);
func typeof(typestring string) Type {
typ, ok := typecache[typestring];
if !ok {
typ = ParseTypeString("", typestring);
@ -958,6 +956,13 @@ func NewValue(e interface {}) Value {
}
typecache[typestring] = typ;
}
return typ;
}
// NewValue creates a new Value from the interface{} object provided.
func NewValue(e interface {}) Value {
value, typestring, indir := unsafe.Reflect(e);
typ := typeof(typestring);
var ap Addr;
if indir {
// Content of interface is large and didn't
@ -984,6 +989,12 @@ func NewValue(e interface {}) Value {
return newValueAddr(typ, ap);
}
// Typeof returns the type of the value in the interface{} object provided.
func Typeof(e interface{}) Type {
value, typestring, indir := unsafe.Reflect(e);
return typeof(typestring);
}
// Indirect indirects one level through a value, if it is a pointer.
// If not a pointer, the value is returned unchanged.
// Useful when walking arbitrary data structures.