本文整理匯總了Golang中encoding/gob.Decoder.DecodeValue方法的典型用法代碼示例。如果您正苦於以下問題:Golang Decoder.DecodeValue方法的具體用法?Golang Decoder.DecodeValue怎麽用?Golang Decoder.DecodeValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encoding/gob.Decoder
的用法示例。
在下文中一共展示了Decoder.DecodeValue方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: deserializeStructInternal
// deserializeStructInternal is a helper function for deserializeStruct.
// It takes a gob decoder and struct field metadata, and then assigns the correct value to the specified struct field.
func deserializeStructInternal(dec *gob.Decoder, fi *fieldInfo, fieldName string, nameParts []string, slice, zeroValue bool, structHistory map[string]map[string]bool, v reflect.Value, t reflect.Type) error {
if len(fi.sliceIndex) > 0 {
v = v.FieldByIndex(fi.sliceIndex)
t = v.Type()
var sv reflect.Value
createNew := false
nameIdx := len(fi.sliceIndex)
absName, childName := strings.Join(nameParts[:nameIdx], "."), strings.Join(nameParts[nameIdx:], ".")
sh, ok := structHistory[absName]
if !ok || sh[childName] {
sh = make(map[string]bool, 8)
structHistory[absName] = sh
createNew = true
} else if len(sh) == 0 {
createNew = true
}
if createNew {
structType := t.Elem()
sv = reflect.New(structType).Elem()
v.Set(reflect.Append(v, sv))
}
sv = v.Index(v.Len() - 1)
sh[childName] = true
v = sv
t = v.Type()
}
vf := v.FieldByIndex(fi.fieldIndex)
if vf.Kind() == reflect.Slice && !slice {
elemType := vf.Type().Elem()
if elemType.Kind() == reflect.Uint8 {
if !zeroValue {
if err := dec.DecodeValue(vf); err != nil {
return fmt.Errorf("goon: Failed to decode field %v - %v", fieldName, err)
}
}
} else {
ev := reflect.New(elemType).Elem()
if !zeroValue {
if err := dec.DecodeValue(ev); err != nil {
return fmt.Errorf("goon: Failed to decode field %v - %v", fieldName, err)
}
}
vf.Set(reflect.Append(vf, ev))
}
} else if !zeroValue {
if err := dec.DecodeValue(vf); err != nil {
return fmt.Errorf("goon: Failed to decode field %v - %v", fieldName, err)
}
}
return nil
}