scan_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "fmt"
  17. "math"
  18. "reflect"
  19. "strconv"
  20. "testing"
  21. "time"
  22. "github.com/gomodule/redigo/redis"
  23. "github.com/stretchr/testify/require"
  24. )
  25. type durationScan struct {
  26. time.Duration `redis:"sd"`
  27. }
  28. func (t *durationScan) RedisScan(src interface{}) (err error) {
  29. if t == nil {
  30. return fmt.Errorf("nil pointer")
  31. }
  32. switch src := src.(type) {
  33. case string:
  34. t.Duration, err = time.ParseDuration(src)
  35. case []byte:
  36. t.Duration, err = time.ParseDuration(string(src))
  37. case int64:
  38. t.Duration = time.Duration(src)
  39. default:
  40. err = fmt.Errorf("cannot convert from %T to %T", src, t)
  41. }
  42. return err
  43. }
  44. var scanConversionTests = []struct {
  45. src interface{}
  46. dest interface{}
  47. }{
  48. {[]byte("-inf"), math.Inf(-1)},
  49. {[]byte("+inf"), math.Inf(1)},
  50. {[]byte("0"), float64(0)},
  51. {[]byte("3.14159"), float64(3.14159)},
  52. {[]byte("3.14"), float32(3.14)},
  53. {[]byte("-100"), int(-100)},
  54. {[]byte("101"), int(101)},
  55. {int64(102), int(102)},
  56. {[]byte("103"), uint(103)},
  57. {int64(104), uint(104)},
  58. {[]byte("105"), int8(105)},
  59. {int64(106), int8(106)},
  60. {[]byte("107"), uint8(107)},
  61. {int64(108), uint8(108)},
  62. {[]byte("0"), false},
  63. {int64(0), false},
  64. {[]byte("f"), false},
  65. {[]byte("1"), true},
  66. {int64(1), true},
  67. {[]byte("t"), true},
  68. {"hello", "hello"},
  69. {[]byte("hello"), "hello"},
  70. {[]byte("world"), []byte("world")},
  71. {nil, ""},
  72. {nil, []byte(nil)},
  73. {[]interface{}{[]byte("b1")}, []interface{}{[]byte("b1")}},
  74. {[]interface{}{[]byte("b2")}, []string{"b2"}},
  75. {[]interface{}{[]byte("b3"), []byte("b4")}, []string{"b3", "b4"}},
  76. {[]interface{}{[]byte("b5")}, [][]byte{[]byte("b5")}},
  77. {[]interface{}{[]byte("1")}, []int{1}},
  78. {[]interface{}{[]byte("1"), []byte("2")}, []int{1, 2}},
  79. {[]interface{}{[]byte("1"), []byte("2")}, []float64{1, 2}},
  80. {[]interface{}{[]byte("1")}, []byte{1}},
  81. {[]interface{}{[]byte("1")}, []bool{true}},
  82. {[]interface{}{"s1"}, []interface{}{"s1"}},
  83. {[]interface{}{"s2"}, [][]byte{[]byte("s2")}},
  84. {[]interface{}{"s3", "s4"}, []string{"s3", "s4"}},
  85. {[]interface{}{"s5"}, [][]byte{[]byte("s5")}},
  86. {[]interface{}{"1"}, []int{1}},
  87. {[]interface{}{"1", "2"}, []int{1, 2}},
  88. {[]interface{}{"1", "2"}, []float64{1, 2}},
  89. {[]interface{}{"1"}, []byte{1}},
  90. {[]interface{}{"1"}, []bool{true}},
  91. {[]interface{}{nil, "2"}, []interface{}{nil, "2"}},
  92. {[]interface{}{nil, []byte("2")}, [][]byte{nil, []byte("2")}},
  93. {[]interface{}{redis.Error("e1")}, []interface{}{redis.Error("e1")}},
  94. {[]interface{}{redis.Error("e2")}, [][]byte{[]byte("e2")}},
  95. {[]interface{}{redis.Error("e3")}, []string{"e3"}},
  96. {"1m", durationScan{Duration: time.Minute}},
  97. {[]byte("1m"), durationScan{Duration: time.Minute}},
  98. {time.Minute.Nanoseconds(), durationScan{Duration: time.Minute}},
  99. {[]interface{}{[]byte("1m")}, []durationScan{{Duration: time.Minute}}},
  100. {[]interface{}{[]byte("1m")}, []*durationScan{{Duration: time.Minute}}},
  101. }
  102. func TestScanConversion(t *testing.T) {
  103. for _, tt := range scanConversionTests {
  104. values := []interface{}{tt.src}
  105. dest := reflect.New(reflect.TypeOf(tt.dest))
  106. values, err := redis.Scan(values, dest.Interface())
  107. if err != nil {
  108. t.Errorf("Scan(%v) returned error %v", tt, err)
  109. continue
  110. }
  111. if !reflect.DeepEqual(tt.dest, dest.Elem().Interface()) {
  112. t.Errorf("Scan(%v) returned %v, want %v", tt, dest.Elem().Interface(), tt.dest)
  113. }
  114. }
  115. }
  116. var scanConversionErrorTests = []struct {
  117. src interface{}
  118. dest interface{}
  119. }{
  120. {[]byte("1234"), byte(0)},
  121. {int64(1234), byte(0)},
  122. {[]byte("-1"), byte(0)},
  123. {int64(-1), byte(0)},
  124. {[]byte("junk"), false},
  125. {redis.Error("blah"), false},
  126. {redis.Error("blah"), durationScan{Duration: time.Minute}},
  127. {"invalid", durationScan{Duration: time.Minute}},
  128. }
  129. func TestScanConversionError(t *testing.T) {
  130. for _, tt := range scanConversionErrorTests {
  131. values := []interface{}{tt.src}
  132. dest := reflect.New(reflect.TypeOf(tt.dest))
  133. values, err := redis.Scan(values, dest.Interface())
  134. if err == nil {
  135. t.Errorf("Scan(%v) did not return error", tt)
  136. }
  137. }
  138. }
  139. func ExampleScan() {
  140. c, err := dial()
  141. if err != nil {
  142. fmt.Println(err)
  143. return
  144. }
  145. defer c.Close()
  146. c.Send("HMSET", "album:1", "title", "Red", "rating", 5)
  147. c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1)
  148. c.Send("HMSET", "album:3", "title", "Beat")
  149. c.Send("LPUSH", "albums", "1")
  150. c.Send("LPUSH", "albums", "2")
  151. c.Send("LPUSH", "albums", "3")
  152. values, err := redis.Values(c.Do("SORT", "albums",
  153. "BY", "album:*->rating",
  154. "GET", "album:*->title",
  155. "GET", "album:*->rating"))
  156. if err != nil {
  157. fmt.Println(err)
  158. return
  159. }
  160. for len(values) > 0 {
  161. var title string
  162. rating := -1 // initialize to illegal value to detect nil.
  163. values, err = redis.Scan(values, &title, &rating)
  164. if err != nil {
  165. fmt.Println(err)
  166. return
  167. }
  168. if rating == -1 {
  169. fmt.Println(title, "not-rated")
  170. } else {
  171. fmt.Println(title, rating)
  172. }
  173. }
  174. // Output:
  175. // Beat not-rated
  176. // Earthbound 1
  177. // Red 5
  178. }
  179. type s0 struct {
  180. X int
  181. Y int `redis:"y"`
  182. Bt bool
  183. }
  184. type s1 struct {
  185. X int `redis:"-"`
  186. I int `redis:"i"`
  187. U uint `redis:"u"`
  188. S string `redis:"s"`
  189. P []byte `redis:"p"`
  190. B bool `redis:"b"`
  191. Bt bool
  192. Bf bool
  193. s0
  194. Sd durationScan `redis:"sd"`
  195. Sdp *durationScan `redis:"sdp"`
  196. }
  197. var scanStructTests = []struct {
  198. title string
  199. reply []string
  200. value interface{}
  201. }{
  202. {"basic",
  203. []string{
  204. "i", "-1234",
  205. "u", "5678",
  206. "s", "hello",
  207. "p", "world",
  208. "b", "t",
  209. "Bt", "1",
  210. "Bf", "0",
  211. "X", "123",
  212. "y", "456",
  213. "sd", "1m",
  214. "sdp", "1m",
  215. },
  216. &s1{
  217. I: -1234,
  218. U: 5678,
  219. S: "hello",
  220. P: []byte("world"),
  221. B: true,
  222. Bt: true,
  223. Bf: false,
  224. s0: s0{X: 123, Y: 456},
  225. Sd: durationScan{Duration: time.Minute},
  226. Sdp: &durationScan{Duration: time.Minute},
  227. },
  228. },
  229. {"absent values",
  230. []string{},
  231. &s1{},
  232. },
  233. }
  234. func TestScanStruct(t *testing.T) {
  235. for _, tt := range scanStructTests {
  236. var reply []interface{}
  237. for _, v := range tt.reply {
  238. reply = append(reply, []byte(v))
  239. }
  240. value := reflect.New(reflect.ValueOf(tt.value).Type().Elem())
  241. if err := redis.ScanStruct(reply, value.Interface()); err != nil {
  242. t.Fatalf("ScanStruct(%s) returned error %v", tt.title, err)
  243. }
  244. if !reflect.DeepEqual(value.Interface(), tt.value) {
  245. t.Fatalf("ScanStruct(%s) returned %v, want %v", tt.title, value.Interface(), tt.value)
  246. }
  247. }
  248. }
  249. func TestBadScanStructArgs(t *testing.T) {
  250. x := []interface{}{"A", "b"}
  251. test := func(v interface{}) {
  252. if err := redis.ScanStruct(x, v); err == nil {
  253. t.Errorf("Expect error for ScanStruct(%T, %T)", x, v)
  254. }
  255. }
  256. test(nil)
  257. var v0 *struct{}
  258. test(v0)
  259. var v1 int
  260. test(&v1)
  261. x = x[:1]
  262. v2 := struct{ A string }{}
  263. test(&v2)
  264. }
  265. var scanSliceTests = []struct {
  266. src []interface{}
  267. fieldNames []string
  268. ok bool
  269. dest interface{}
  270. }{
  271. {
  272. []interface{}{[]byte("1"), nil, []byte("-1")},
  273. nil,
  274. true,
  275. []int{1, 0, -1},
  276. },
  277. {
  278. []interface{}{[]byte("1"), nil, []byte("2")},
  279. nil,
  280. true,
  281. []uint{1, 0, 2},
  282. },
  283. {
  284. []interface{}{[]byte("-1")},
  285. nil,
  286. false,
  287. []uint{1},
  288. },
  289. {
  290. []interface{}{[]byte("hello"), nil, []byte("world")},
  291. nil,
  292. true,
  293. [][]byte{[]byte("hello"), nil, []byte("world")},
  294. },
  295. {
  296. []interface{}{[]byte("hello"), nil, []byte("world")},
  297. nil,
  298. true,
  299. []string{"hello", "", "world"},
  300. },
  301. {
  302. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  303. nil,
  304. true,
  305. []struct{ A, B string }{{"a1", "b1"}, {"a2", "b2"}},
  306. },
  307. {
  308. []interface{}{[]byte("a1"), []byte("b1")},
  309. nil,
  310. false,
  311. []struct{ A, B, C string }{{"a1", "b1", ""}},
  312. },
  313. {
  314. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  315. nil,
  316. true,
  317. []*struct{ A, B string }{{A: "a1", B: "b1"}, {A: "a2", B: "b2"}},
  318. },
  319. {
  320. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  321. []string{"A", "B"},
  322. true,
  323. []struct{ A, C, B string }{{"a1", "", "b1"}, {"a2", "", "b2"}},
  324. },
  325. {
  326. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  327. nil,
  328. false,
  329. []struct{}{},
  330. },
  331. }
  332. func TestScanSlice(t *testing.T) {
  333. for _, tt := range scanSliceTests {
  334. typ := reflect.ValueOf(tt.dest).Type()
  335. dest := reflect.New(typ)
  336. err := redis.ScanSlice(tt.src, dest.Interface(), tt.fieldNames...)
  337. if tt.ok != (err == nil) {
  338. t.Errorf("ScanSlice(%v, []%s, %v) returned error %v", tt.src, typ, tt.fieldNames, err)
  339. continue
  340. }
  341. if tt.ok && !reflect.DeepEqual(dest.Elem().Interface(), tt.dest) {
  342. t.Errorf("ScanSlice(src, []%s) returned %#v, want %#v", typ, dest.Elem().Interface(), tt.dest)
  343. }
  344. }
  345. }
  346. func ExampleScanSlice() {
  347. c, err := dial()
  348. if err != nil {
  349. fmt.Println(err)
  350. return
  351. }
  352. defer c.Close()
  353. c.Send("HMSET", "album:1", "title", "Red", "rating", 5)
  354. c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1)
  355. c.Send("HMSET", "album:3", "title", "Beat", "rating", 4)
  356. c.Send("LPUSH", "albums", "1")
  357. c.Send("LPUSH", "albums", "2")
  358. c.Send("LPUSH", "albums", "3")
  359. values, err := redis.Values(c.Do("SORT", "albums",
  360. "BY", "album:*->rating",
  361. "GET", "album:*->title",
  362. "GET", "album:*->rating"))
  363. if err != nil {
  364. fmt.Println(err)
  365. return
  366. }
  367. var albums []struct {
  368. Title string
  369. Rating int
  370. }
  371. if err := redis.ScanSlice(values, &albums); err != nil {
  372. fmt.Println(err)
  373. return
  374. }
  375. fmt.Printf("%v\n", albums)
  376. // Output:
  377. // [{Earthbound 1} {Beat 4} {Red 5}]
  378. }
  379. var argsTests = []struct {
  380. title string
  381. actual redis.Args
  382. expected redis.Args
  383. }{
  384. {"struct ptr",
  385. redis.Args{}.AddFlat(&struct {
  386. I int `redis:"i"`
  387. U uint `redis:"u"`
  388. S string `redis:"s"`
  389. P []byte `redis:"p"`
  390. M map[string]string `redis:"m"`
  391. Bt bool
  392. Bf bool
  393. }{
  394. -1234, 5678, "hello", []byte("world"), map[string]string{"hello": "world"}, true, false,
  395. }),
  396. redis.Args{"i", int(-1234), "u", uint(5678), "s", "hello", "p", []byte("world"), "m", map[string]string{"hello": "world"}, "Bt", true, "Bf", false},
  397. },
  398. {"struct",
  399. redis.Args{}.AddFlat(struct{ I int }{123}),
  400. redis.Args{"I", 123},
  401. },
  402. {"slice",
  403. redis.Args{}.Add(1).AddFlat([]string{"a", "b", "c"}).Add(2),
  404. redis.Args{1, "a", "b", "c", 2},
  405. },
  406. {"struct omitempty",
  407. redis.Args{}.AddFlat(&struct {
  408. Sdp *durationArg `redis:"Sdp,omitempty"`
  409. }{
  410. nil,
  411. }),
  412. redis.Args{},
  413. },
  414. }
  415. func TestArgs(t *testing.T) {
  416. for _, tt := range argsTests {
  417. if !reflect.DeepEqual(tt.actual, tt.expected) {
  418. t.Fatalf("%s is %v, want %v", tt.title, tt.actual, tt.expected)
  419. }
  420. }
  421. }
  422. type InnerStruct struct {
  423. Foo int64
  424. }
  425. func (f *InnerStruct) RedisScan(src interface{}) (err error) {
  426. switch s := src.(type) {
  427. case []byte:
  428. f.Foo, err = strconv.ParseInt(string(s), 10, 64)
  429. case string:
  430. f.Foo, err = strconv.ParseInt(s, 10, 64)
  431. default:
  432. return fmt.Errorf("invalid type %T", src)
  433. }
  434. return err
  435. }
  436. type OuterStruct struct {
  437. Inner *InnerStruct
  438. }
  439. func TestScanPtrRedisScan(t *testing.T) {
  440. tests := []struct {
  441. name string
  442. src []interface{}
  443. dest OuterStruct
  444. expected OuterStruct
  445. }{
  446. {
  447. name: "value-to-nil",
  448. src: []interface{}{[]byte("1234"), nil},
  449. dest: OuterStruct{&InnerStruct{}},
  450. expected: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  451. },
  452. {
  453. name: "nil-to-nil",
  454. src: []interface{}{[]byte(nil), nil},
  455. dest: OuterStruct{},
  456. expected: OuterStruct{},
  457. },
  458. {
  459. name: "value-to-value",
  460. src: []interface{}{[]byte("1234"), nil},
  461. dest: OuterStruct{Inner: &InnerStruct{Foo: 5678}},
  462. expected: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  463. },
  464. {
  465. name: "nil-to-value",
  466. src: []interface{}{[]byte(nil), nil},
  467. dest: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  468. expected: OuterStruct{},
  469. },
  470. }
  471. for _, tc := range tests {
  472. t.Run(tc.name, func(t *testing.T) {
  473. _, err := redis.Scan(tc.src, &tc.dest.Inner)
  474. require.NoError(t, err)
  475. require.Equal(t, tc.expected, tc.dest)
  476. })
  477. }
  478. }
  479. func ExampleArgs() {
  480. c, err := dial()
  481. if err != nil {
  482. fmt.Println(err)
  483. return
  484. }
  485. defer c.Close()
  486. var p1, p2 struct {
  487. Title string `redis:"title"`
  488. Author string `redis:"author"`
  489. Body string `redis:"body"`
  490. }
  491. p1.Title = "Example"
  492. p1.Author = "Gary"
  493. p1.Body = "Hello"
  494. if _, err := c.Do("HMSET", redis.Args{}.Add("id1").AddFlat(&p1)...); err != nil {
  495. fmt.Println(err)
  496. return
  497. }
  498. m := map[string]string{
  499. "title": "Example2",
  500. "author": "Steve",
  501. "body": "Map",
  502. }
  503. if _, err := c.Do("HMSET", redis.Args{}.Add("id2").AddFlat(m)...); err != nil {
  504. fmt.Println(err)
  505. return
  506. }
  507. for _, id := range []string{"id1", "id2"} {
  508. v, err := redis.Values(c.Do("HGETALL", id))
  509. if err != nil {
  510. fmt.Println(err)
  511. return
  512. }
  513. if err := redis.ScanStruct(v, &p2); err != nil {
  514. fmt.Println(err)
  515. return
  516. }
  517. fmt.Printf("%+v\n", p2)
  518. }
  519. // Output:
  520. // {Title:Example Author:Gary Body:Hello}
  521. // {Title:Example2 Author:Steve Body:Map}
  522. }