Example #1
0
func ListGobUnmarshalNotExistCheck(outList []interface{}, obj reflect.Value) (err error) {
	switch obj.Kind() {
	case reflect.Ptr:
		return ListGobUnmarshalNotExistCheck(outList, obj.Elem())
	case reflect.Slice:
		newSlice := reflect.MakeSlice(obj.Type(), len(outList), len(outList))
		elemType := obj.Type().Elem()
		for i, stringI := range outList {
			s, ok := stringI.(string)
			if !ok {
				return ErrKeyNotExist
			}
			thisValue := newSlice.Index(i)
			thisElem := reflect.New(elemType)
			err = kmgGob.Unmarshal([]byte(s), thisElem.Interface())
			if err != nil {
				return err
			}
			thisValue.Set(thisElem.Elem())
		}
		obj.Set(newSlice)
		return nil
	default:
		return fmt.Errorf("[mgetNotExistCheckGobUnmarshal] Unmarshal unexpect Kind %s", obj.Kind().String())
	}
}
Example #2
0
// @deprecated
func MustGetDataWithGob(c *redis.Client, key string, inData interface{}) {
	result, err := c.Get(key).Result()
	if err != nil {
		panic(fmt.Errorf("[MustGetDataWithGob] fail %s", err))
	}
	err = kmgGob.Unmarshal([]byte(result), inData)
	if err != nil {
		panic(err)
	}
}
Example #3
0
// 如果数据不存在,会返回ErrKeyNotExist
// 序列化错误,会返回 error
// 网络错误也会返回 error
func GetGob(key string, obj interface{}) (err error) {
	value, err := Get(key)
	if err != nil {
		return err
	}
	err = kmgGob.Unmarshal([]byte(value), obj)
	if err != nil {
		return err
	}
	return nil
}