|
| 1 | +package cmdutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-redis/redis/v8" |
| 11 | + "github.com/go-redis/redisext/internal" |
| 12 | +) |
| 13 | + |
| 14 | +func CmdString(cmd redis.Cmder) string { |
| 15 | + b := make([]byte, 0, 32) |
| 16 | + b = AppendCmd(b, cmd) |
| 17 | + return internal.String(b) |
| 18 | +} |
| 19 | + |
| 20 | +func CmdsString(cmds []redis.Cmder) (string, string) { |
| 21 | + const numCmdLimit = 100 |
| 22 | + const numNameLimit = 10 |
| 23 | + |
| 24 | + seen := make(map[string]struct{}, numNameLimit) |
| 25 | + unqNames := make([]string, 0, numNameLimit) |
| 26 | + |
| 27 | + b := make([]byte, 0, 32*len(cmds)) |
| 28 | + |
| 29 | + for i, cmd := range cmds { |
| 30 | + if i > numCmdLimit { |
| 31 | + break |
| 32 | + } |
| 33 | + |
| 34 | + if i > 0 { |
| 35 | + b = append(b, '\n') |
| 36 | + } |
| 37 | + b = AppendCmd(b, cmd) |
| 38 | + |
| 39 | + if len(unqNames) >= numNameLimit { |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + name := cmd.FullName() |
| 44 | + if _, ok := seen[name]; !ok { |
| 45 | + seen[name] = struct{}{} |
| 46 | + unqNames = append(unqNames, name) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + summary := strings.Join(unqNames, " ") |
| 51 | + return summary, internal.String(b) |
| 52 | +} |
| 53 | + |
| 54 | +func AppendCmd(b []byte, cmd redis.Cmder) []byte { |
| 55 | + const numArgLimit = 32 |
| 56 | + |
| 57 | + for i, arg := range cmd.Args() { |
| 58 | + if i > numArgLimit { |
| 59 | + break |
| 60 | + } |
| 61 | + if i > 0 { |
| 62 | + b = append(b, ' ') |
| 63 | + } |
| 64 | + b = appendArg(b, arg) |
| 65 | + } |
| 66 | + |
| 67 | + if err := cmd.Err(); err != nil { |
| 68 | + b = append(b, ": "...) |
| 69 | + b = append(b, err.Error()...) |
| 70 | + } |
| 71 | + |
| 72 | + return b |
| 73 | +} |
| 74 | + |
| 75 | +func appendArg(b []byte, v interface{}) []byte { |
| 76 | + const argLenLimit = 64 |
| 77 | + |
| 78 | + switch v := v.(type) { |
| 79 | + case nil: |
| 80 | + return append(b, "<nil>"...) |
| 81 | + case string: |
| 82 | + if len(v) > argLenLimit { |
| 83 | + v = v[:argLenLimit] |
| 84 | + } |
| 85 | + return appendUTF8String(b, internal.Bytes(v)) |
| 86 | + case []byte: |
| 87 | + if len(v) > argLenLimit { |
| 88 | + v = v[:argLenLimit] |
| 89 | + } |
| 90 | + return appendUTF8String(b, v) |
| 91 | + case int: |
| 92 | + return strconv.AppendInt(b, int64(v), 10) |
| 93 | + case int8: |
| 94 | + return strconv.AppendInt(b, int64(v), 10) |
| 95 | + case int16: |
| 96 | + return strconv.AppendInt(b, int64(v), 10) |
| 97 | + case int32: |
| 98 | + return strconv.AppendInt(b, int64(v), 10) |
| 99 | + case int64: |
| 100 | + return strconv.AppendInt(b, v, 10) |
| 101 | + case uint: |
| 102 | + return strconv.AppendUint(b, uint64(v), 10) |
| 103 | + case uint8: |
| 104 | + return strconv.AppendUint(b, uint64(v), 10) |
| 105 | + case uint16: |
| 106 | + return strconv.AppendUint(b, uint64(v), 10) |
| 107 | + case uint32: |
| 108 | + return strconv.AppendUint(b, uint64(v), 10) |
| 109 | + case uint64: |
| 110 | + return strconv.AppendUint(b, v, 10) |
| 111 | + case float32: |
| 112 | + return strconv.AppendFloat(b, float64(v), 'f', -1, 64) |
| 113 | + case float64: |
| 114 | + return strconv.AppendFloat(b, v, 'f', -1, 64) |
| 115 | + case bool: |
| 116 | + if v { |
| 117 | + return append(b, "true"...) |
| 118 | + } |
| 119 | + return append(b, "false"...) |
| 120 | + case time.Time: |
| 121 | + return v.AppendFormat(b, time.RFC3339Nano) |
| 122 | + default: |
| 123 | + return append(b, fmt.Sprint(v)...) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func appendUTF8String(dst []byte, src []byte) []byte { |
| 128 | + if isSimple(src) { |
| 129 | + dst = append(dst, src...) |
| 130 | + return dst |
| 131 | + } |
| 132 | + |
| 133 | + s := len(dst) |
| 134 | + dst = append(dst, make([]byte, hex.EncodedLen(len(src)))...) |
| 135 | + hex.Encode(dst[s:], src) |
| 136 | + return dst |
| 137 | +} |
| 138 | + |
| 139 | +func isSimple(b []byte) bool { |
| 140 | + for _, c := range b { |
| 141 | + if !isSimpleByte(c) { |
| 142 | + return false |
| 143 | + } |
| 144 | + } |
| 145 | + return true |
| 146 | +} |
| 147 | + |
| 148 | +func isSimpleByte(c byte) bool { |
| 149 | + return simple[c] |
| 150 | +} |
| 151 | + |
| 152 | +var simple = [256]bool{ |
| 153 | + '-': true, |
| 154 | + '+': true, |
| 155 | + '_': true, |
| 156 | + ':': true, |
| 157 | + |
| 158 | + '0': true, |
| 159 | + '1': true, |
| 160 | + '2': true, |
| 161 | + '3': true, |
| 162 | + '4': true, |
| 163 | + '5': true, |
| 164 | + '6': true, |
| 165 | + '7': true, |
| 166 | + '8': true, |
| 167 | + '9': true, |
| 168 | + |
| 169 | + 'a': true, |
| 170 | + 'b': true, |
| 171 | + 'c': true, |
| 172 | + 'd': true, |
| 173 | + 'e': true, |
| 174 | + 'f': true, |
| 175 | + 'g': true, |
| 176 | + 'h': true, |
| 177 | + 'i': true, |
| 178 | + 'j': true, |
| 179 | + 'k': true, |
| 180 | + 'l': true, |
| 181 | + 'm': true, |
| 182 | + 'n': true, |
| 183 | + 'o': true, |
| 184 | + 'p': true, |
| 185 | + 'q': true, |
| 186 | + 'r': true, |
| 187 | + 's': true, |
| 188 | + 't': true, |
| 189 | + 'u': true, |
| 190 | + 'v': true, |
| 191 | + 'w': true, |
| 192 | + 'x': true, |
| 193 | + 'y': true, |
| 194 | + 'z': true, |
| 195 | + |
| 196 | + 'A': true, |
| 197 | + 'B': true, |
| 198 | + 'C': true, |
| 199 | + 'D': true, |
| 200 | + 'E': true, |
| 201 | + 'F': true, |
| 202 | + 'G': true, |
| 203 | + 'H': true, |
| 204 | + 'I': true, |
| 205 | + 'J': true, |
| 206 | + 'K': true, |
| 207 | + 'L': true, |
| 208 | + 'M': true, |
| 209 | + 'N': true, |
| 210 | + 'O': true, |
| 211 | + 'P': true, |
| 212 | + 'Q': true, |
| 213 | + 'R': true, |
| 214 | + 'S': true, |
| 215 | + 'T': true, |
| 216 | + 'U': true, |
| 217 | + 'V': true, |
| 218 | + 'W': true, |
| 219 | + 'X': true, |
| 220 | + 'Y': true, |
| 221 | + 'Z': true, |
| 222 | +} |
0 commit comments