func registerType(s, funcName string, t reflect.Type) error {
if t.Kind() == reflect.Struct {
return fmt.Errorf("%s in the function [%s] should be passed by reference, i.e. *%s", s, funcName, t)
}
if err := validateType(t); err != nil {
return fmt.Errorf("%s in the function [%s] cannot contain %s", s, funcName, err)
}
t = removePtr(t)
tv := reflect.New(t)
if t.Kind() != reflect.Struct {
tv = reflect.Indirect(tv)
}
switch t.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
RegisterType(tv.Interface())
default:
}
return nil
}
func RegisterType(x interface{}) {
gob.Register(x)
}
- 首先对t reflect.Type进行判断,看是不是合法的类型.具体函数为validateType
- 然后申请t对应的类型,然后如果发现是特殊的类型reflect.Array, reflect.Slice, reflect.Map, reflect.Struct,则使用gob.Register()进行注册
- gob编码解码前需要用gob.Register()进行注册
type validatedType struct {
t reflect.Type
err *error
}
var validatedTypes []*validatedType
func validateType(t reflect.Type) (err error) {
t = removePtr(t)
for _, vd := range validatedTypes {
if vd.t == t {
return *vd.err
}
}
validatedTypes = append(validatedTypes, &validatedType{
t: t,
err: &err,
})
switch t.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.UnsafePointer:
err = fmt.Errorf("%s. Found [%s]", t.Kind(), t)
return err
case reflect.Array, reflect.Slice:
if err = validateType(t.Elem()); err != nil {
err = fmt.Errorf("%s in the %s [%s]", err, t.Kind(), t)
return err
}
case reflect.Map:
if err = validateType(t.Elem()); err != nil {
err = fmt.Errorf("%s in the value of map [%s]", err, t)
return err
}
if err = validateType(t.Key()); err != nil {
err = fmt.Errorf("%s in the key of map [%s]", err, t)
return err
}
case reflect.Struct:
if supportsGob(t) {
return nil
}
// Special case for struct{}
if t.NumField() == 0 {
return nil
}
n := 0
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.PkgPath == "" {
if err = validateType(f.Type); err != nil {
err = fmt.Errorf("%s in the field [%s] of struct [%s]", err, f.Name, t)
return err
}
n++
}
}
if n == 0 {
err = fmt.Errorf("struct without exported fields [%s]", t)
return err
}
}
return err
}
- 函数写的很巧妙,记忆化深度优先搜索.通过数组validatedTypes对每次请求的type映射err.
- 支持的类型,结构体,map,slice,array均不能包含reflect.Chan, reflect.Func, reflect.Interface, reflect.UnsafePointer.
- 特殊的类型在supportGob函数中判断,也支持实现了(gob.GobEncoder,gob.GobDecoder)/(encoding.BinaryMarshaler,encoding.BinaryUnmarshaler)接口的类型
var (
gobEncoderType = reflect.TypeOf((*gob.GobEncoder)(nil)).Elem()
gobDecoderType = reflect.TypeOf((*gob.GobDecoder)(nil)).Elem()
binaryMarshalerType = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
binaryUnmarshalerType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
)
func supportsGob(t reflect.Type) bool {
if t.Kind() != reflect.Struct {
panic(fmt.Sprintf("non-struct type passed to supportsGob: %s", t))
}
t = reflect.PtrTo(t)
if t.Implements(gobEncoderType) && t.Implements(gobDecoderType) {
return true
}
if t.Implements(binaryMarshalerType) && t.Implements(binaryUnmarshalerType) {
return true
}
return false
}