Commit 9e25eccf authored by Rob Pike's avatar Rob Pike

gob: clean up getTypeInfo (address a TODO)

also fix a caching bug.

R=rsc
CC=golang-dev
https://golang.org/cl/4261049
parent 255b5381
...@@ -638,55 +638,47 @@ var typeInfoMap = make(map[reflect.Type]*typeInfo) // protected by typeLock ...@@ -638,55 +638,47 @@ var typeInfoMap = make(map[reflect.Type]*typeInfo) // protected by typeLock
// typeLock must be held. // typeLock must be held.
func getTypeInfo(ut *userTypeInfo) (*typeInfo, os.Error) { func getTypeInfo(ut *userTypeInfo) (*typeInfo, os.Error) {
rt := ut.base
if ut.isGobEncoder { 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. // We want the user type, not the base type.
userType, err := getType(ut.user.Name(), ut, ut.user) rt = ut.user
if err != nil { }
return nil, err info, ok := typeInfoMap[rt]
} if ok {
info = new(typeInfo) return info, nil
gt, err := getBaseType(ut.base.Name(), ut.base) }
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 { if err != nil {
return nil, err return nil, err
} }
info.id = gt.id()
info.wire = &wireType{GobEncoderT: userType.id().gobType().(*gobEncoderType)} info.wire = &wireType{GobEncoderT: userType.id().gobType().(*gobEncoderType)}
typeInfoMap[ut.user] = info typeInfoMap[ut.user] = info
return info, nil return info, nil
} }
base := ut.base t := info.id.gobType()
info, ok := typeInfoMap[base] switch typ := rt.(type) {
if !ok { case *reflect.ArrayType:
info = new(typeInfo) info.wire = &wireType{ArrayT: t.(*arrayType)}
name := base.Name() case *reflect.MapType:
gt, err := getBaseType(name, base) info.wire = &wireType{MapT: t.(*mapType)}
if err != nil { case *reflect.SliceType:
return nil, err // []byte == []uint8 is a special case handled separately
} if typ.Elem().Kind() != reflect.Uint8 {
info.id = gt.id() info.wire = &wireType{SliceT: t.(*sliceType)}
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)}
} }
typeInfoMap[base] = info case *reflect.StructType:
info.wire = &wireType{StructT: t.(*structType)}
} }
typeInfoMap[rt] = info
return info, nil return info, nil
} }
......
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