setCommandValueReflection is a fallback function that uses reflection
(cmd Cmder, value interface{})
| 963 | |
| 964 | // setCommandValueReflection is a fallback function that uses reflection |
| 965 | func (c *ClusterClient) setCommandValueReflection(cmd Cmder, value interface{}) error { |
| 966 | cmdValue := reflect.ValueOf(cmd) |
| 967 | if cmdValue.Kind() != reflect.Ptr || cmdValue.IsNil() { |
| 968 | return errInvalidCmdPointer |
| 969 | } |
| 970 | |
| 971 | setValMethod := cmdValue.MethodByName("SetVal") |
| 972 | if !setValMethod.IsValid() { |
| 973 | return fmt.Errorf("redis: command %T does not have SetVal method", cmd) |
| 974 | } |
| 975 | |
| 976 | args := []reflect.Value{reflect.ValueOf(value)} |
| 977 | |
| 978 | switch cmd.(type) { |
| 979 | case *XAutoClaimCmd, *XAutoClaimJustIDCmd: |
| 980 | args = append(args, reflect.ValueOf("")) |
| 981 | case *ScanCmd: |
| 982 | args = append(args, reflect.ValueOf(uint64(0))) |
| 983 | case *KeyValuesCmd, *ZSliceWithKeyCmd: |
| 984 | if key, ok := value.(string); ok { |
| 985 | args = []reflect.Value{reflect.ValueOf(key)} |
| 986 | if _, ok := cmd.(*ZSliceWithKeyCmd); ok { |
| 987 | args = append(args, reflect.ValueOf([]Z{})) |
| 988 | } else { |
| 989 | args = append(args, reflect.ValueOf([]string{})) |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | defer func() { |
| 995 | if r := recover(); r != nil { |
| 996 | cmd.SetErr(fmt.Errorf("redis: failed to set command value: %v", r)) |
| 997 | } |
| 998 | }() |
| 999 | |
| 1000 | setValMethod.Call(args) |
| 1001 | return nil |
| 1002 | } |
no test coverage detected