MCPcopy
hub / github.com/redis/go-redis / setCommandValueReflection

Method setCommandValueReflection

osscluster_router.go:965–1002  ·  view source on GitHub ↗

setCommandValueReflection is a fallback function that uses reflection

(cmd Cmder, value interface{})

Source from the content-addressed store, hash-verified

963
964// setCommandValueReflection is a fallback function that uses reflection
965func (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}

Callers 1

setCommandValueMethod · 0.95

Calls 2

SetErrMethod · 0.65
IsValidMethod · 0.45

Tested by

no test coverage detected