Commit ba0cf083 authored by Rob Pike's avatar Rob Pike

change reflect.Type.Name() into two functions: Name() and PkgPath() for ease of use.

R=rsc
DELTA=31  (8 added, 2 deleted, 21 changed)
OCL=31778
CL=31792
parent db508ccb
...@@ -741,7 +741,7 @@ func decode(b *bytes.Buffer, wireId TypeId, e interface{}) os.Error { ...@@ -741,7 +741,7 @@ func decode(b *bytes.Buffer, wireId TypeId, e interface{}) os.Error {
} }
engine := *enginePtr; engine := *enginePtr;
if engine.numInstr == 0 && st.NumField() > 0 && len(wireId.gobType().(*structType).field) > 0 { if engine.numInstr == 0 && st.NumField() > 0 && len(wireId.gobType().(*structType).field) > 0 {
path, name := rt.Name(); name := rt.Name();
return os.ErrorString("gob: type mismatch: no fields matched compiling decoder for " + name) return os.ErrorString("gob: type mismatch: no fields matched compiling decoder for " + name)
} }
return decodeStruct(engine, rt.(*reflect.StructType), b, uintptr(v.Addr()), 0); return decodeStruct(engine, rt.(*reflect.StructType), b, uintptr(v.Addr()), 0);
......
...@@ -241,8 +241,7 @@ func newTypeObject(name string, rt reflect.Type) gobType { ...@@ -241,8 +241,7 @@ func newTypeObject(name string, rt reflect.Type) gobType {
if _, ok := t.Elem().(*reflect.Uint8Type); ok { if _, ok := t.Elem().(*reflect.Uint8Type); ok {
return tBytes.gobType() return tBytes.gobType()
} }
_, elemName := t.Elem().Name(); return newSliceType(name, newType(t.Elem().Name(), t.Elem()));
return newSliceType(name, newType(elemName, t.Elem()));
case *reflect.StructType: case *reflect.StructType:
// Install the struct type itself before the fields so recursive // Install the struct type itself before the fields so recursive
...@@ -254,7 +253,7 @@ func newTypeObject(name string, rt reflect.Type) gobType { ...@@ -254,7 +253,7 @@ func newTypeObject(name string, rt reflect.Type) gobType {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
f := t.Field(i); f := t.Field(i);
typ, _indir := indirect(f.Type); typ, _indir := indirect(f.Type);
_pkg, tname := typ.Name(); tname := typ.Name();
if tname == "" { if tname == "" {
tname = f.Type.String(); tname = f.Type.String();
} }
...@@ -346,7 +345,7 @@ func getTypeInfo(rt reflect.Type) *typeInfo { ...@@ -346,7 +345,7 @@ func getTypeInfo(rt reflect.Type) *typeInfo {
info, ok := typeInfoMap[rt]; info, ok := typeInfoMap[rt];
if !ok { if !ok {
info = new(typeInfo); info = new(typeInfo);
path, name := rt.Name(); name := rt.Name();
info.typeId = getType(name, rt).id(); info.typeId = getType(name, rt).id();
// assume it's a struct type // assume it's a struct type
info.wire = &wireType{info.typeId.gobType().(*structType)}; info.wire = &wireType{info.typeId.gobType().(*structType)};
......
...@@ -246,9 +246,12 @@ type Method struct { ...@@ -246,9 +246,12 @@ type Method struct {
// Each type in a program has a unique Type, so == on Types // Each type in a program has a unique Type, so == on Types
// corresponds to Go's type equality. // corresponds to Go's type equality.
type Type interface { type Type interface {
// Name returns the type's package and name. // PkgPath returns the type's package path.
// The package is a full package import path like "container/vector". // The package path is a full package import path like "container/vector".
Name() (pkgPath string, name string); PkgPath() string;
// Name returns the type's name within its package.
Name() string;
// String returns a string representation of the type. // String returns a string representation of the type.
// The string representation may use shortened package names // The string representation may use shortened package names
...@@ -284,17 +287,18 @@ func (t *uncommonType) uncommon() *uncommonType { ...@@ -284,17 +287,18 @@ func (t *uncommonType) uncommon() *uncommonType {
return t; return t;
} }
func (t *uncommonType) Name() (pkgPath string, name string) { func (t *uncommonType) PkgPath() string {
if t == nil { if t == nil || t.pkgPath == nil {
return; return ""
} }
if t.pkgPath != nil { return *t.pkgPath;
pkgPath = *t.pkgPath; }
}
if t.name != nil { func (t *uncommonType) Name() string {
name = *t.name; if t == nil || t.name == nil {
return "";
} }
return; return *t.name;
} }
func (t *commonType) String() string { func (t *commonType) String() string {
...@@ -348,7 +352,11 @@ func (t *commonType) Method(i int) (m Method) { ...@@ -348,7 +352,11 @@ func (t *commonType) Method(i int) (m Method) {
return t.uncommonType.Method(i); return t.uncommonType.Method(i);
} }
func (t *commonType) Name() (pkgPath string, name string) { func (t *commonType) PkgPath() string {
return t.uncommonType.PkgPath();
}
func (t *commonType) Name() string {
return t.uncommonType.Name(); return t.uncommonType.Name();
} }
...@@ -469,8 +477,7 @@ func (t *StructType) Field(i int) (f StructField) { ...@@ -469,8 +477,7 @@ func (t *StructType) Field(i int) (f StructField) {
if p.name != nil { if p.name != nil {
f.Name = *p.name; f.Name = *p.name;
} else { } else {
nam, pkg := f.Type.Name(); f.Name = f.Type.Name();
f.Name = nam;
f.Anonymous = true; f.Anonymous = true;
} }
if p.pkgPath != nil { if p.pkgPath != nil {
......
...@@ -71,7 +71,7 @@ func (server *serverType) add(rcvr interface{}) os.Error { ...@@ -71,7 +71,7 @@ func (server *serverType) add(rcvr interface{}) os.Error {
s := new(service); s := new(service);
s.typ = reflect.Typeof(rcvr); s.typ = reflect.Typeof(rcvr);
s.rcvr = reflect.NewValue(rcvr); s.rcvr = reflect.NewValue(rcvr);
path_, sname := reflect.Indirect(s.rcvr).Type().Name(); sname := reflect.Indirect(s.rcvr).Type().Name();
if sname == "" { if sname == "" {
log.Exit("rpc: no service name for type", s.typ.String()) log.Exit("rpc: no service name for type", s.typ.String())
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment