|
| 1 | +package hscan |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strconv" |
| 8 | +) |
| 9 | + |
| 10 | +// decoderFunc represents decoding functions for default built-in types. |
| 11 | +type decoderFunc func(reflect.Value, string) error |
| 12 | + |
| 13 | +var ( |
| 14 | + // List of built-in decoders indexed by their numeric constant values (eg: reflect.Bool = 1) |
| 15 | + decoders = []decoderFunc{ |
| 16 | + reflect.Bool: decodeBool, |
| 17 | + reflect.Int: decodeInt, |
| 18 | + reflect.Int8: decodeInt, |
| 19 | + reflect.Int16: decodeInt, |
| 20 | + reflect.Int32: decodeInt, |
| 21 | + reflect.Int64: decodeInt, |
| 22 | + reflect.Uint: decodeUint, |
| 23 | + reflect.Uint8: decodeUint, |
| 24 | + reflect.Uint16: decodeUint, |
| 25 | + reflect.Uint32: decodeUint, |
| 26 | + reflect.Uint64: decodeUint, |
| 27 | + reflect.Float32: decodeFloat, |
| 28 | + reflect.Float64: decodeFloat, |
| 29 | + reflect.Complex64: decodeUnsupported, |
| 30 | + reflect.Complex128: decodeUnsupported, |
| 31 | + reflect.Array: decodeUnsupported, |
| 32 | + reflect.Chan: decodeUnsupported, |
| 33 | + reflect.Func: decodeUnsupported, |
| 34 | + reflect.Interface: decodeUnsupported, |
| 35 | + reflect.Map: decodeUnsupported, |
| 36 | + reflect.Ptr: decodeUnsupported, |
| 37 | + reflect.Slice: decodeStringSlice, |
| 38 | + reflect.String: decodeString, |
| 39 | + reflect.Struct: decodeUnsupported, |
| 40 | + reflect.UnsafePointer: decodeUnsupported, |
| 41 | + } |
| 42 | + |
| 43 | + // Global map of struct field specs that is populated once for every new |
| 44 | + // struct type that is scanned. This caches the field types and the corresponding |
| 45 | + // decoder functions to avoid iterating through struct fields on subsequent scans. |
| 46 | + structSpecs = newStructMap() |
| 47 | +) |
| 48 | + |
| 49 | +// Scan scans the results from a key-value Redis map result set ([]interface{}) |
| 50 | +// to a destination struct. The Redis keys are matched to the struct's field |
| 51 | +// with the `redis` tag. |
| 52 | +func Scan(vals []interface{}, dest interface{}) error { |
| 53 | + if len(vals)%2 != 0 { |
| 54 | + return errors.New("args should have an even number of items (key-val)") |
| 55 | + } |
| 56 | + |
| 57 | + // The destination to scan into should be a struct pointer. |
| 58 | + v := reflect.ValueOf(dest) |
| 59 | + if v.Kind() != reflect.Ptr || v.IsNil() { |
| 60 | + return fmt.Errorf("redis.Scan(non-pointer %T)", dest) |
| 61 | + } |
| 62 | + v = v.Elem() |
| 63 | + |
| 64 | + if v.Kind() != reflect.Struct { |
| 65 | + return fmt.Errorf("redis.Scan(non-struct %T)", dest) |
| 66 | + } |
| 67 | + |
| 68 | + // If the struct field spec is not cached, build and cache it to avoid |
| 69 | + // iterating through the fields of a struct type every time values are |
| 70 | + // scanned into it. |
| 71 | + typ := v.Type() |
| 72 | + fMap, ok := structSpecs.get(typ) |
| 73 | + |
| 74 | + if !ok { |
| 75 | + fMap = makeStructSpecs(v, "redis") |
| 76 | + structSpecs.set(typ, fMap) |
| 77 | + } |
| 78 | + |
| 79 | + // Iterate through the (key, value) sequence. |
| 80 | + for i := 0; i < len(vals); i += 2 { |
| 81 | + key, ok := vals[i].(string) |
| 82 | + if !ok { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + val, ok := vals[i+1].(string) |
| 87 | + if !ok { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + // Check if the field name is in the field spec map. |
| 92 | + field, ok := fMap.get(key) |
| 93 | + if !ok { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + if err := field.fn(v.Field(field.index), val); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func decodeBool(f reflect.Value, s string) error { |
| 106 | + b, err := strconv.ParseBool(s) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + f.SetBool(b) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func decodeInt(f reflect.Value, s string) error { |
| 115 | + v, err := strconv.ParseInt(s, 10, 0) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + f.SetInt(v) |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func decodeUint(f reflect.Value, s string) error { |
| 124 | + v, err := strconv.ParseUint(s, 10, 0) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + f.SetUint(v) |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func decodeFloat(f reflect.Value, s string) error { |
| 133 | + v, err := strconv.ParseFloat(s, 0) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + f.SetFloat(v) |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func decodeString(f reflect.Value, s string) error { |
| 142 | + f.SetString(s) |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func decodeStringSlice(f reflect.Value, s string) error { |
| 147 | + // []byte slice ([]uint8). |
| 148 | + if f.Type().Elem().Kind() == reflect.Uint8 { |
| 149 | + f.SetBytes([]byte(s)) |
| 150 | + } |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func decodeUnsupported(f reflect.Value, s string) error { |
| 155 | + return fmt.Errorf("redis.Scan(unsupported type %v)", f) |
| 156 | +} |
0 commit comments