aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-03-06 10:50:07 -0800
committerRob Pike <r@golang.org>2011-03-06 10:50:07 -0800
commit9e25eccf48338902740bae65a2e40f68a2b8a842 (patch)
tree39420381084053cbd4a60c621315301983969a8e
parent255b538152aa4e28eaaca7bf41ea2a5368d77ac9 (diff)
downloadgo-9e25eccf48338902740bae65a2e40f68a2b8a842.tar.gz
go-9e25eccf48338902740bae65a2e40f68a2b8a842.zip
gob: clean up getTypeInfo (address a TODO)
also fix a caching bug. R=rsc CC=golang-dev https://golang.org/cl/4261049
-rw-r--r--src/pkg/gob/type.go66
1 files changed, 29 insertions, 37 deletions
diff --git a/src/pkg/gob/type.go b/src/pkg/gob/type.go
index 9d82454ea8..a438139415 100644
--- a/src/pkg/gob/type.go
+++ b/src/pkg/gob/type.go
@@ -638,55 +638,47 @@ var typeInfoMap = make(map[reflect.Type]*typeInfo) // protected by typeLock
// typeLock must be held.
func getTypeInfo(ut *userTypeInfo) (*typeInfo, os.Error) {
-
+ rt := ut.base
if ut.isGobEncoder {
- // TODO: clean up this code - too much duplication.
- info, ok := typeInfoMap[ut.user]
- if ok {
- return info, nil
- }
// We want the user type, not the base type.
- userType, err := getType(ut.user.Name(), ut, ut.user)
- if err != nil {
- return nil, err
- }
- info = new(typeInfo)
- gt, err := getBaseType(ut.base.Name(), ut.base)
+ rt = ut.user
+ }
+ info, ok := typeInfoMap[rt]
+ if ok {
+ return info, nil
+ }
+ info = new(typeInfo)
+ gt, err := getBaseType(rt.Name(), rt)
+ if err != nil {
+ return nil, err
+ }
+ info.id = gt.id()
+
+ if ut.isGobEncoder {
+ userType, err := getType(rt.Name(), ut, rt)
if err != nil {
return nil, err
}
- info.id = gt.id()
info.wire = &wireType{GobEncoderT: userType.id().gobType().(*gobEncoderType)}
typeInfoMap[ut.user] = info
return info, nil
}
- base := ut.base
- info, ok := typeInfoMap[base]
- if !ok {
- info = new(typeInfo)
- name := base.Name()
- gt, err := getBaseType(name, base)
- if err != nil {
- return nil, err
- }
- info.id = gt.id()
- t := info.id.gobType()
- switch typ := base.(type) {
- case *reflect.ArrayType:
- info.wire = &wireType{ArrayT: t.(*arrayType)}
- case *reflect.MapType:
- info.wire = &wireType{MapT: t.(*mapType)}
- case *reflect.SliceType:
- // []byte == []uint8 is a special case handled separately
- if typ.Elem().Kind() != reflect.Uint8 {
- info.wire = &wireType{SliceT: t.(*sliceType)}
- }
- case *reflect.StructType:
- info.wire = &wireType{StructT: t.(*structType)}
+ t := info.id.gobType()
+ switch typ := rt.(type) {
+ case *reflect.ArrayType:
+ info.wire = &wireType{ArrayT: t.(*arrayType)}
+ case *reflect.MapType:
+ info.wire = &wireType{MapT: t.(*mapType)}
+ case *reflect.SliceType:
+ // []byte == []uint8 is a special case handled separately
+ if typ.Elem().Kind() != reflect.Uint8 {
+ info.wire = &wireType{SliceT: t.(*sliceType)}
}
- typeInfoMap[base] = info
+ case *reflect.StructType:
+ info.wire = &wireType{StructT: t.(*structType)}
}
+ typeInfoMap[rt] = info
return info, nil
}