scan_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. PtrB *bool
  194. s0
  195. Sd durationScan `redis:"sd"`
  196. Sdp *durationScan `redis:"sdp"`
  197. }
  198. var boolTrue = true
  199. var scanStructTests = []struct {
  200. title string
  201. reply []string
  202. value interface{}
  203. }{
  204. {"basic",
  205. []string{
  206. "i", "-1234",
  207. "u", "5678",
  208. "s", "hello",
  209. "p", "world",
  210. "b", "t",
  211. "Bt", "1",
  212. "Bf", "0",
  213. "PtrB", "1",
  214. "X", "123",
  215. "y", "456",
  216. "sd", "1m",
  217. "sdp", "1m",
  218. },
  219. &s1{
  220. I: -1234,
  221. U: 5678,
  222. S: "hello",
  223. P: []byte("world"),
  224. B: true,
  225. Bt: true,
  226. Bf: false,
  227. PtrB: &boolTrue,
  228. s0: s0{X: 123, Y: 456},
  229. Sd: durationScan{Duration: time.Minute},
  230. Sdp: &durationScan{Duration: time.Minute},
  231. },
  232. },
  233. {"absent values",
  234. []string{},
  235. &s1{},
  236. },
  237. }
  238. func TestScanStruct(t *testing.T) {
  239. for _, tt := range scanStructTests {
  240. var reply []interface{}
  241. for _, v := range tt.reply {
  242. reply = append(reply, []byte(v))
  243. }
  244. value := reflect.New(reflect.ValueOf(tt.value).Type().Elem())
  245. if err := redis.ScanStruct(reply, value.Interface()); err != nil {
  246. t.Fatalf("ScanStruct(%s) returned error %v", tt.title, err)
  247. }
  248. if !reflect.DeepEqual(value.Interface(), tt.value) {
  249. t.Fatalf("ScanStruct(%s) returned %v, want %v", tt.title, value.Interface(), tt.value)
  250. }
  251. }
  252. }
  253. func TestBadScanStructArgs(t *testing.T) {
  254. x := []interface{}{"A", "b"}
  255. test := func(v interface{}) {
  256. if err := redis.ScanStruct(x, v); err == nil {
  257. t.Errorf("Expect error for ScanStruct(%T, %T)", x, v)
  258. }
  259. }
  260. test(nil)
  261. var v0 *struct{}
  262. test(v0)
  263. var v1 int
  264. test(&v1)
  265. x = x[:1]
  266. v2 := struct{ A string }{}
  267. test(&v2)
  268. }
  269. var scanSliceTests = []struct {
  270. src []interface{}
  271. fieldNames []string
  272. ok bool
  273. dest interface{}
  274. }{
  275. {
  276. []interface{}{[]byte("1"), nil, []byte("-1")},
  277. nil,
  278. true,
  279. []int{1, 0, -1},
  280. },
  281. {
  282. []interface{}{[]byte("1"), nil, []byte("2")},
  283. nil,
  284. true,
  285. []uint{1, 0, 2},
  286. },
  287. {
  288. []interface{}{[]byte("-1")},
  289. nil,
  290. false,
  291. []uint{1},
  292. },
  293. {
  294. []interface{}{[]byte("hello"), nil, []byte("world")},
  295. nil,
  296. true,
  297. [][]byte{[]byte("hello"), nil, []byte("world")},
  298. },
  299. {
  300. []interface{}{[]byte("hello"), nil, []byte("world")},
  301. nil,
  302. true,
  303. []string{"hello", "", "world"},
  304. },
  305. {
  306. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  307. nil,
  308. true,
  309. []struct{ A, B string }{{"a1", "b1"}, {"a2", "b2"}},
  310. },
  311. {
  312. []interface{}{[]byte("a1"), []byte("b1")},
  313. nil,
  314. false,
  315. []struct{ A, B, C string }{{"a1", "b1", ""}},
  316. },
  317. {
  318. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  319. nil,
  320. true,
  321. []*struct{ A, B string }{{A: "a1", B: "b1"}, {A: "a2", B: "b2"}},
  322. },
  323. {
  324. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  325. []string{"A", "B"},
  326. true,
  327. []struct{ A, C, B string }{{"a1", "", "b1"}, {"a2", "", "b2"}},
  328. },
  329. {
  330. []interface{}{[]byte("a1"), []byte("b1"), []byte("a2"), []byte("b2")},
  331. nil,
  332. false,
  333. []struct{}{},
  334. },
  335. }
  336. func TestScanSlice(t *testing.T) {
  337. for _, tt := range scanSliceTests {
  338. typ := reflect.ValueOf(tt.dest).Type()
  339. dest := reflect.New(typ)
  340. err := redis.ScanSlice(tt.src, dest.Interface(), tt.fieldNames...)
  341. if tt.ok != (err == nil) {
  342. t.Errorf("ScanSlice(%v, []%s, %v) returned error %v", tt.src, typ, tt.fieldNames, err)
  343. continue
  344. }
  345. if tt.ok && !reflect.DeepEqual(dest.Elem().Interface(), tt.dest) {
  346. t.Errorf("ScanSlice(src, []%s) returned %#v, want %#v", typ, dest.Elem().Interface(), tt.dest)
  347. }
  348. }
  349. }
  350. func ExampleScanSlice() {
  351. c, err := dial()
  352. if err != nil {
  353. fmt.Println(err)
  354. return
  355. }
  356. defer c.Close()
  357. c.Send("HMSET", "album:1", "title", "Red", "rating", 5)
  358. c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1)
  359. c.Send("HMSET", "album:3", "title", "Beat", "rating", 4)
  360. c.Send("LPUSH", "albums", "1")
  361. c.Send("LPUSH", "albums", "2")
  362. c.Send("LPUSH", "albums", "3")
  363. values, err := redis.Values(c.Do("SORT", "albums",
  364. "BY", "album:*->rating",
  365. "GET", "album:*->title",
  366. "GET", "album:*->rating"))
  367. if err != nil {
  368. fmt.Println(err)
  369. return
  370. }
  371. var albums []struct {
  372. Title string
  373. Rating int
  374. }
  375. if err := redis.ScanSlice(values, &albums); err != nil {
  376. fmt.Println(err)
  377. return
  378. }
  379. fmt.Printf("%v\n", albums)
  380. // Output:
  381. // [{Earthbound 1} {Beat 4} {Red 5}]
  382. }
  383. var argsTests = []struct {
  384. title string
  385. actual redis.Args
  386. expected redis.Args
  387. }{
  388. {"struct ptr",
  389. redis.Args{}.AddFlat(&struct {
  390. I int `redis:"i"`
  391. U uint `redis:"u"`
  392. S string `redis:"s"`
  393. P []byte `redis:"p"`
  394. M map[string]string `redis:"m"`
  395. Bt bool
  396. Bf bool
  397. PtrB *bool
  398. PtrI *int
  399. }{
  400. -1234, 5678, "hello", []byte("world"), map[string]string{"hello": "world"}, true, false, &boolTrue, nil,
  401. }),
  402. redis.Args{"i", int(-1234), "u", uint(5678), "s", "hello", "p", []byte("world"), "m", map[string]string{"hello": "world"}, "Bt", true, "Bf", false, "PtrB", true},
  403. },
  404. {"struct",
  405. redis.Args{}.AddFlat(struct{ I int }{123}),
  406. redis.Args{"I", 123},
  407. },
  408. {"slice",
  409. redis.Args{}.Add(1).AddFlat([]string{"a", "b", "c"}).Add(2),
  410. redis.Args{1, "a", "b", "c", 2},
  411. },
  412. {"struct omitempty",
  413. redis.Args{}.AddFlat(&struct {
  414. Sdp *durationArg `redis:"Sdp,omitempty"`
  415. }{
  416. nil,
  417. }),
  418. redis.Args{},
  419. },
  420. }
  421. func TestArgs(t *testing.T) {
  422. for _, tt := range argsTests {
  423. if !reflect.DeepEqual(tt.actual, tt.expected) {
  424. t.Fatalf("%s is %v, want %v", tt.title, tt.actual, tt.expected)
  425. }
  426. }
  427. }
  428. type InnerStruct struct {
  429. Foo int64
  430. }
  431. func (f *InnerStruct) RedisScan(src interface{}) (err error) {
  432. switch s := src.(type) {
  433. case []byte:
  434. f.Foo, err = strconv.ParseInt(string(s), 10, 64)
  435. case string:
  436. f.Foo, err = strconv.ParseInt(s, 10, 64)
  437. default:
  438. return fmt.Errorf("invalid type %T", src)
  439. }
  440. return err
  441. }
  442. type OuterStruct struct {
  443. Inner *InnerStruct
  444. }
  445. func TestScanPtrRedisScan(t *testing.T) {
  446. tests := []struct {
  447. name string
  448. src []interface{}
  449. dest OuterStruct
  450. expected OuterStruct
  451. }{
  452. {
  453. name: "value-to-nil",
  454. src: []interface{}{[]byte("1234"), nil},
  455. dest: OuterStruct{&InnerStruct{}},
  456. expected: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  457. },
  458. {
  459. name: "nil-to-nil",
  460. src: []interface{}{[]byte(nil), nil},
  461. dest: OuterStruct{},
  462. expected: OuterStruct{},
  463. },
  464. {
  465. name: "value-to-value",
  466. src: []interface{}{[]byte("1234"), nil},
  467. dest: OuterStruct{Inner: &InnerStruct{Foo: 5678}},
  468. expected: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  469. },
  470. {
  471. name: "nil-to-value",
  472. src: []interface{}{[]byte(nil), nil},
  473. dest: OuterStruct{Inner: &InnerStruct{Foo: 1234}},
  474. expected: OuterStruct{},
  475. },
  476. }
  477. for _, tc := range tests {
  478. t.Run(tc.name, func(t *testing.T) {
  479. _, err := redis.Scan(tc.src, &tc.dest.Inner)
  480. require.NoError(t, err)
  481. require.Equal(t, tc.expected, tc.dest)
  482. })
  483. }
  484. }
  485. func ExampleArgs() {
  486. c, err := dial()
  487. if err != nil {
  488. fmt.Println(err)
  489. return
  490. }
  491. defer c.Close()
  492. var p1, p2 struct {
  493. Title string `redis:"title"`
  494. Author string `redis:"author"`
  495. Body string `redis:"body"`
  496. }
  497. p1.Title = "Example"
  498. p1.Author = "Gary"
  499. p1.Body = "Hello"
  500. if _, err := c.Do("HMSET", redis.Args{}.Add("id1").AddFlat(&p1)...); err != nil {
  501. fmt.Println(err)
  502. return
  503. }
  504. m := map[string]string{
  505. "title": "Example2",
  506. "author": "Steve",
  507. "body": "Map",
  508. }
  509. if _, err := c.Do("HMSET", redis.Args{}.Add("id2").AddFlat(m)...); err != nil {
  510. fmt.Println(err)
  511. return
  512. }
  513. for _, id := range []string{"id1", "id2"} {
  514. v, err := redis.Values(c.Do("HGETALL", id))
  515. if err != nil {
  516. fmt.Println(err)
  517. return
  518. }
  519. if err := redis.ScanStruct(v, &p2); err != nil {
  520. fmt.Println(err)
  521. return
  522. }
  523. fmt.Printf("%+v\n", p2)
  524. }
  525. // Output:
  526. // {Title:Example Author:Gary Body:Hello}
  527. // {Title:Example2 Author:Steve Body:Map}
  528. }