marshal.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "fmt"
  8. "math"
  9. "reflect"
  10. "time"
  11. )
  12. // Marshaler is the interface implemented by objects that can marshal
  13. // themselves into values understood by Cassandra.
  14. type Marshaler interface {
  15. MarshalCQL(info *TypeInfo) ([]byte, error)
  16. }
  17. // Unmarshaler is the interface implemented by objects that can unmarshal
  18. // a Cassandra specific description of themselves.
  19. type Unmarshaler interface {
  20. UnmarshalCQL(info *TypeInfo, data []byte) error
  21. }
  22. // Marshal returns the CQL encoding of the value for the Cassandra
  23. // internal type described by the info parameter.
  24. func Marshal(info *TypeInfo, value interface{}) ([]byte, error) {
  25. if v, ok := value.(Marshaler); ok {
  26. return v.MarshalCQL(info)
  27. }
  28. switch info.Type {
  29. case TypeVarchar, TypeAscii, TypeBlob:
  30. return marshalVarchar(info, value)
  31. case TypeBoolean:
  32. return marshalBool(info, value)
  33. case TypeInt:
  34. return marshalInt(info, value)
  35. case TypeBigInt:
  36. return marshalBigInt(info, value)
  37. case TypeFloat:
  38. return marshalFloat(info, value)
  39. case TypeDouble:
  40. return marshalDouble(info, value)
  41. case TypeTimestamp:
  42. return marshalTimestamp(info, value)
  43. case TypeList, TypeSet:
  44. return marshalList(info, value)
  45. case TypeMap:
  46. return marshalMap(info, value)
  47. }
  48. // TODO(tux21b): add the remaining types
  49. return nil, fmt.Errorf("can not marshal %T into %s", value, info)
  50. }
  51. // Unmarshal parses the CQL encoded data based on the info parameter that
  52. // describes the Cassandra internal data type and stores the result in the
  53. // value pointed by value.
  54. func Unmarshal(info *TypeInfo, data []byte, value interface{}) error {
  55. if v, ok := value.(Unmarshaler); ok {
  56. return v.UnmarshalCQL(info, data)
  57. }
  58. switch info.Type {
  59. case TypeVarchar, TypeAscii, TypeBlob:
  60. return unmarshalVarchar(info, data, value)
  61. case TypeBoolean:
  62. return unmarshalBool(info, data, value)
  63. case TypeInt:
  64. return unmarshalInt(info, data, value)
  65. case TypeBigInt, TypeCounter:
  66. return unmarshalBigInt(info, data, value)
  67. case TypeFloat:
  68. return unmarshalFloat(info, data, value)
  69. case TypeDouble:
  70. return unmarshalDouble(info, data, value)
  71. case TypeTimestamp:
  72. return unmarshalTimestamp(info, data, value)
  73. case TypeList, TypeSet:
  74. return unmarshalList(info, data, value)
  75. case TypeMap:
  76. return unmarshalMap(info, data, value)
  77. }
  78. // TODO(tux21b): add the remaining types
  79. return fmt.Errorf("can not unmarshal %s into %T", info, value)
  80. }
  81. func marshalVarchar(info *TypeInfo, value interface{}) ([]byte, error) {
  82. switch v := value.(type) {
  83. case Marshaler:
  84. return v.MarshalCQL(info)
  85. case string:
  86. return []byte(v), nil
  87. case []byte:
  88. return v, nil
  89. }
  90. rv := reflect.ValueOf(value)
  91. t := rv.Type()
  92. k := t.Kind()
  93. switch {
  94. case k == reflect.String:
  95. return []byte(rv.String()), nil
  96. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  97. return rv.Bytes(), nil
  98. case k == reflect.Ptr:
  99. return marshalVarchar(info, rv.Elem().Interface())
  100. }
  101. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  102. }
  103. func unmarshalVarchar(info *TypeInfo, data []byte, value interface{}) error {
  104. switch v := value.(type) {
  105. case Unmarshaler:
  106. return v.UnmarshalCQL(info, data)
  107. case *string:
  108. *v = string(data)
  109. return nil
  110. case *[]byte:
  111. var dataCopy []byte
  112. if data != nil {
  113. dataCopy = make([]byte, len(data))
  114. copy(dataCopy, data)
  115. }
  116. *v = dataCopy
  117. return nil
  118. }
  119. rv := reflect.ValueOf(value)
  120. if rv.Kind() != reflect.Ptr {
  121. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  122. }
  123. rv = rv.Elem()
  124. t := rv.Type()
  125. k := t.Kind()
  126. switch {
  127. case k == reflect.String:
  128. rv.SetString(string(data))
  129. return nil
  130. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  131. var dataCopy []byte
  132. if data != nil {
  133. dataCopy = make([]byte, len(data))
  134. copy(dataCopy, data)
  135. }
  136. rv.SetBytes(dataCopy)
  137. return nil
  138. }
  139. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  140. }
  141. func marshalInt(info *TypeInfo, value interface{}) ([]byte, error) {
  142. switch v := value.(type) {
  143. case Marshaler:
  144. return v.MarshalCQL(info)
  145. case int:
  146. if v > math.MaxInt32 || v < math.MinInt32 {
  147. return nil, marshalErrorf("marshal int: value %d out of range", v)
  148. }
  149. return encInt(int32(v)), nil
  150. case uint:
  151. if v > math.MaxInt32 {
  152. return nil, marshalErrorf("marshal int: value %d out of range", v)
  153. }
  154. return encInt(int32(v)), nil
  155. case int64:
  156. if v > math.MaxInt32 || v < math.MinInt32 {
  157. return nil, marshalErrorf("marshal int: value %d out of range", v)
  158. }
  159. return encInt(int32(v)), nil
  160. case uint64:
  161. if v > math.MaxInt32 {
  162. return nil, marshalErrorf("marshal int: value %d out of range", v)
  163. }
  164. return encInt(int32(v)), nil
  165. case int32:
  166. return encInt(v), nil
  167. case uint32:
  168. if v > math.MaxInt32 {
  169. return nil, marshalErrorf("marshal int: value %d out of range", v)
  170. }
  171. return encInt(int32(v)), nil
  172. case int16:
  173. return encInt(int32(v)), nil
  174. case uint16:
  175. return encInt(int32(v)), nil
  176. case int8:
  177. return encInt(int32(v)), nil
  178. case uint8:
  179. return encInt(int32(v)), nil
  180. }
  181. rv := reflect.ValueOf(value)
  182. switch rv.Type().Kind() {
  183. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  184. v := rv.Int()
  185. if v > math.MaxInt32 || v < math.MinInt32 {
  186. return nil, marshalErrorf("marshal int: value %d out of range", v)
  187. }
  188. return encInt(int32(v)), nil
  189. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  190. v := rv.Uint()
  191. if v > math.MaxInt32 {
  192. return nil, marshalErrorf("marshal int: value %d out of range", v)
  193. }
  194. return encInt(int32(v)), nil
  195. case reflect.Ptr:
  196. return marshalInt(info, rv.Elem().Interface())
  197. }
  198. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  199. }
  200. func encInt(x int32) []byte {
  201. return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  202. }
  203. func unmarshalInt(info *TypeInfo, data []byte, value interface{}) error {
  204. switch v := value.(type) {
  205. case Unmarshaler:
  206. return v.UnmarshalCQL(info, data)
  207. case *int:
  208. *v = int(decInt(data))
  209. return nil
  210. case *uint:
  211. x := decInt(data)
  212. if x < 0 {
  213. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  214. }
  215. *v = uint(x)
  216. return nil
  217. case *int64:
  218. *v = int64(decInt(data))
  219. return nil
  220. case *uint64:
  221. x := decInt(data)
  222. if x < 0 {
  223. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  224. }
  225. *v = uint64(x)
  226. return nil
  227. case *int32:
  228. *v = decInt(data)
  229. return nil
  230. case *uint32:
  231. x := decInt(data)
  232. if x < 0 {
  233. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  234. }
  235. *v = uint32(x)
  236. return nil
  237. case *int16:
  238. x := decInt(data)
  239. if x < math.MinInt16 || x > math.MaxInt16 {
  240. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  241. }
  242. *v = int16(x)
  243. return nil
  244. case *uint16:
  245. x := decInt(data)
  246. if x < 0 || x > math.MaxUint16 {
  247. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  248. }
  249. *v = uint16(x)
  250. return nil
  251. case *int8:
  252. x := decInt(data)
  253. if x < math.MinInt8 || x > math.MaxInt8 {
  254. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  255. }
  256. *v = int8(x)
  257. return nil
  258. case *uint8:
  259. x := decInt(data)
  260. if x < 0 || x > math.MaxUint8 {
  261. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  262. }
  263. *v = uint8(x)
  264. return nil
  265. }
  266. rv := reflect.ValueOf(value)
  267. if rv.Kind() != reflect.Ptr {
  268. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  269. }
  270. rv = rv.Elem()
  271. switch rv.Type().Kind() {
  272. case reflect.Int, reflect.Int64, reflect.Int32:
  273. rv.SetInt(int64(decInt(data)))
  274. return nil
  275. case reflect.Int16:
  276. x := decInt(data)
  277. if x < math.MinInt16 || x > math.MaxInt16 {
  278. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  279. }
  280. rv.SetInt(int64(x))
  281. return nil
  282. case reflect.Int8:
  283. x := decInt(data)
  284. if x < math.MinInt8 || x > math.MaxInt8 {
  285. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  286. }
  287. rv.SetInt(int64(x))
  288. return nil
  289. case reflect.Uint, reflect.Uint64, reflect.Uint32:
  290. x := decInt(data)
  291. if x < 0 {
  292. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  293. }
  294. rv.SetUint(uint64(x))
  295. return nil
  296. case reflect.Uint16:
  297. x := decInt(data)
  298. if x < 0 || x > math.MaxUint16 {
  299. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  300. }
  301. rv.SetUint(uint64(x))
  302. return nil
  303. case reflect.Uint8:
  304. x := decInt(data)
  305. if x < 0 || x > math.MaxUint8 {
  306. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  307. }
  308. rv.SetUint(uint64(x))
  309. return nil
  310. }
  311. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  312. }
  313. func decInt(x []byte) int32 {
  314. if len(x) != 4 {
  315. return 0
  316. }
  317. return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3])
  318. }
  319. func marshalBigInt(info *TypeInfo, value interface{}) ([]byte, error) {
  320. switch v := value.(type) {
  321. case Marshaler:
  322. return v.MarshalCQL(info)
  323. case int:
  324. return encBigInt(int64(v)), nil
  325. case uint:
  326. if v > math.MaxInt64 {
  327. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  328. }
  329. return encBigInt(int64(v)), nil
  330. case int64:
  331. return encBigInt(v), nil
  332. case uint64:
  333. if v > math.MaxInt64 {
  334. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  335. }
  336. return encBigInt(int64(v)), nil
  337. case int32:
  338. return encBigInt(int64(v)), nil
  339. case uint32:
  340. return encBigInt(int64(v)), nil
  341. case int16:
  342. return encBigInt(int64(v)), nil
  343. case uint16:
  344. return encBigInt(int64(v)), nil
  345. case int8:
  346. return encBigInt(int64(v)), nil
  347. case uint8:
  348. return encBigInt(int64(v)), nil
  349. }
  350. rv := reflect.ValueOf(value)
  351. switch rv.Type().Kind() {
  352. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  353. v := rv.Int()
  354. return encBigInt(v), nil
  355. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  356. v := rv.Uint()
  357. if v > math.MaxInt64 {
  358. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  359. }
  360. return encBigInt(int64(v)), nil
  361. case reflect.Ptr:
  362. return marshalBigInt(info, rv.Elem().Interface())
  363. }
  364. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  365. }
  366. func encBigInt(x int64) []byte {
  367. return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32),
  368. byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  369. }
  370. func unmarshalBigInt(info *TypeInfo, data []byte, value interface{}) error {
  371. switch v := value.(type) {
  372. case Unmarshaler:
  373. return v.UnmarshalCQL(info, data)
  374. case *int:
  375. x := decBigInt(data)
  376. if ^uint(0) == math.MaxUint32 && (x < math.MinInt32 || x > math.MaxInt32) {
  377. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  378. }
  379. *v = int(x)
  380. return nil
  381. case *uint:
  382. x := decBigInt(data)
  383. if x < 0 || (^uint(0) == math.MaxUint32 && x > math.MaxUint32) {
  384. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  385. }
  386. *v = uint(x)
  387. return nil
  388. case *int64:
  389. *v = decBigInt(data)
  390. return nil
  391. case *uint64:
  392. x := decBigInt(data)
  393. if x < 0 {
  394. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  395. }
  396. *v = uint64(x)
  397. return nil
  398. case *int32:
  399. x := decBigInt(data)
  400. if x < math.MinInt32 || x > math.MaxInt32 {
  401. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  402. }
  403. *v = int32(x)
  404. return nil
  405. case *uint32:
  406. x := decBigInt(data)
  407. if x < 0 || x > math.MaxUint32 {
  408. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  409. }
  410. *v = uint32(x)
  411. return nil
  412. case *int16:
  413. x := decBigInt(data)
  414. if x < math.MinInt16 || x > math.MaxInt16 {
  415. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  416. }
  417. *v = int16(x)
  418. return nil
  419. case *uint16:
  420. x := decBigInt(data)
  421. if x < 0 || x > math.MaxUint16 {
  422. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  423. }
  424. *v = uint16(x)
  425. return nil
  426. case *int8:
  427. x := decBigInt(data)
  428. if x < math.MinInt8 || x > math.MaxInt8 {
  429. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  430. }
  431. *v = int8(x)
  432. return nil
  433. case *uint8:
  434. x := decBigInt(data)
  435. if x < 0 || x > math.MaxUint8 {
  436. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  437. }
  438. *v = uint8(x)
  439. return nil
  440. }
  441. rv := reflect.ValueOf(value)
  442. if rv.Kind() != reflect.Ptr {
  443. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  444. }
  445. rv = rv.Elem()
  446. switch rv.Type().Kind() {
  447. case reflect.Int:
  448. x := decBigInt(data)
  449. if ^uint(0) == math.MaxUint32 && (x < math.MinInt32 || x > math.MaxInt32) {
  450. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  451. }
  452. rv.SetInt(x)
  453. return nil
  454. case reflect.Int64:
  455. rv.SetInt(decBigInt(data))
  456. return nil
  457. case reflect.Int32:
  458. x := decBigInt(data)
  459. if x < math.MinInt32 || x > math.MaxInt32 {
  460. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  461. }
  462. rv.SetInt(x)
  463. return nil
  464. case reflect.Int16:
  465. x := decBigInt(data)
  466. if x < math.MinInt16 || x > math.MaxInt16 {
  467. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  468. }
  469. rv.SetInt(x)
  470. return nil
  471. case reflect.Int8:
  472. x := decBigInt(data)
  473. if x < math.MinInt8 || x > math.MaxInt8 {
  474. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  475. }
  476. rv.SetInt(x)
  477. return nil
  478. case reflect.Uint:
  479. x := decBigInt(data)
  480. if x < 0 || (^uint(0) == math.MaxUint32 && x > math.MaxUint32) {
  481. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  482. }
  483. rv.SetUint(uint64(x))
  484. return nil
  485. case reflect.Uint64:
  486. x := decBigInt(data)
  487. if x < 0 {
  488. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  489. }
  490. rv.SetUint(uint64(x))
  491. return nil
  492. case reflect.Uint32:
  493. x := decBigInt(data)
  494. if x < 0 || x > math.MaxUint32 {
  495. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  496. }
  497. rv.SetUint(uint64(x))
  498. return nil
  499. case reflect.Uint16:
  500. x := decBigInt(data)
  501. if x < 0 || x > math.MaxUint16 {
  502. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  503. }
  504. rv.SetUint(uint64(x))
  505. return nil
  506. case reflect.Uint8:
  507. x := decBigInt(data)
  508. if x < 0 || x > math.MaxUint8 {
  509. return unmarshalErrorf("unmarshal int: value %d out of range", x)
  510. }
  511. rv.SetUint(uint64(x))
  512. return nil
  513. }
  514. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  515. }
  516. func decBigInt(data []byte) int64 {
  517. if len(data) != 8 {
  518. return 0
  519. }
  520. return int64(data[0])<<56 | int64(data[1])<<48 |
  521. int64(data[2])<<40 | int64(data[3])<<32 |
  522. int64(data[4])<<24 | int64(data[5])<<16 |
  523. int64(data[6])<<8 | int64(data[7])
  524. }
  525. func marshalBool(info *TypeInfo, value interface{}) ([]byte, error) {
  526. switch v := value.(type) {
  527. case Marshaler:
  528. return v.MarshalCQL(info)
  529. case bool:
  530. return encBool(v), nil
  531. }
  532. rv := reflect.ValueOf(value)
  533. switch rv.Type().Kind() {
  534. case reflect.Bool:
  535. return encBool(rv.Bool()), nil
  536. case reflect.Ptr:
  537. return marshalBool(info, rv.Elem().Interface())
  538. }
  539. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  540. }
  541. func encBool(v bool) []byte {
  542. if v {
  543. return []byte{1}
  544. }
  545. return []byte{0}
  546. }
  547. func unmarshalBool(info *TypeInfo, data []byte, value interface{}) error {
  548. switch v := value.(type) {
  549. case Unmarshaler:
  550. return v.UnmarshalCQL(info, data)
  551. case *bool:
  552. *v = decBool(data)
  553. return nil
  554. }
  555. rv := reflect.ValueOf(value)
  556. if rv.Kind() != reflect.Ptr {
  557. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  558. }
  559. rv = rv.Elem()
  560. switch rv.Type().Kind() {
  561. case reflect.Bool:
  562. rv.SetBool(decBool(data))
  563. return nil
  564. }
  565. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  566. }
  567. func decBool(v []byte) bool {
  568. if len(v) == 0 {
  569. return false
  570. }
  571. return v[0] != 0
  572. }
  573. func marshalFloat(info *TypeInfo, value interface{}) ([]byte, error) {
  574. switch v := value.(type) {
  575. case Marshaler:
  576. return v.MarshalCQL(info)
  577. case float32:
  578. return encInt(int32(math.Float32bits(v))), nil
  579. }
  580. rv := reflect.ValueOf(value)
  581. switch rv.Type().Kind() {
  582. case reflect.Float32:
  583. return encInt(int32(math.Float32bits(float32(rv.Float())))), nil
  584. case reflect.Ptr:
  585. return marshalFloat(info, rv.Elem().Interface())
  586. }
  587. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  588. }
  589. func unmarshalFloat(info *TypeInfo, data []byte, value interface{}) error {
  590. switch v := value.(type) {
  591. case Unmarshaler:
  592. return v.UnmarshalCQL(info, data)
  593. case *float32:
  594. *v = math.Float32frombits(uint32(decInt(data)))
  595. return nil
  596. }
  597. rv := reflect.ValueOf(value)
  598. if rv.Kind() != reflect.Ptr {
  599. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  600. }
  601. rv = rv.Elem()
  602. switch rv.Type().Kind() {
  603. case reflect.Float32:
  604. rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data)))))
  605. return nil
  606. }
  607. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  608. }
  609. func marshalDouble(info *TypeInfo, value interface{}) ([]byte, error) {
  610. switch v := value.(type) {
  611. case Marshaler:
  612. return v.MarshalCQL(info)
  613. case float64:
  614. return encBigInt(int64(math.Float64bits(v))), nil
  615. }
  616. rv := reflect.ValueOf(value)
  617. switch rv.Type().Kind() {
  618. case reflect.Float64:
  619. return encBigInt(int64(math.Float64bits(rv.Float()))), nil
  620. case reflect.Ptr:
  621. return marshalFloat(info, rv.Elem().Interface())
  622. }
  623. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  624. }
  625. func unmarshalDouble(info *TypeInfo, data []byte, value interface{}) error {
  626. switch v := value.(type) {
  627. case Unmarshaler:
  628. return v.UnmarshalCQL(info, data)
  629. case *float64:
  630. *v = math.Float64frombits(uint64(decBigInt(data)))
  631. return nil
  632. }
  633. rv := reflect.ValueOf(value)
  634. if rv.Kind() != reflect.Ptr {
  635. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  636. }
  637. rv = rv.Elem()
  638. switch rv.Type().Kind() {
  639. case reflect.Float64:
  640. rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
  641. return nil
  642. }
  643. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  644. }
  645. func marshalTimestamp(info *TypeInfo, value interface{}) ([]byte, error) {
  646. switch v := value.(type) {
  647. case Marshaler:
  648. return v.MarshalCQL(info)
  649. case int64:
  650. return encBigInt(v), nil
  651. case time.Time:
  652. x := v.In(time.UTC).UnixNano() / int64(time.Millisecond)
  653. return encBigInt(x), nil
  654. }
  655. rv := reflect.ValueOf(value)
  656. switch rv.Type().Kind() {
  657. case reflect.Int64:
  658. return encBigInt(rv.Int()), nil
  659. case reflect.Ptr:
  660. return marshalTimestamp(info, rv.Elem().Interface())
  661. }
  662. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  663. }
  664. func unmarshalTimestamp(info *TypeInfo, data []byte, value interface{}) error {
  665. switch v := value.(type) {
  666. case Unmarshaler:
  667. return v.UnmarshalCQL(info, data)
  668. case *int64:
  669. *v = decBigInt(data)
  670. return nil
  671. case *time.Time:
  672. x := decBigInt(data)
  673. sec := x / 1000
  674. nsec := (x - sec*1000) * 1000000
  675. *v = time.Unix(sec, nsec).In(time.UTC)
  676. return nil
  677. }
  678. rv := reflect.ValueOf(value)
  679. if rv.Kind() != reflect.Ptr {
  680. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  681. }
  682. rv = rv.Elem()
  683. switch rv.Type().Kind() {
  684. case reflect.Int64:
  685. rv.SetInt(decBigInt(data))
  686. return nil
  687. }
  688. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  689. }
  690. func marshalList(info *TypeInfo, value interface{}) ([]byte, error) {
  691. rv := reflect.ValueOf(value)
  692. t := rv.Type()
  693. k := t.Kind()
  694. switch k {
  695. case reflect.Slice, reflect.Array:
  696. buf := &bytes.Buffer{}
  697. n := rv.Len()
  698. if n > math.MaxUint16 {
  699. return nil, marshalErrorf("marshal: slice / array too large")
  700. }
  701. buf.WriteByte(byte(n >> 8))
  702. buf.WriteByte(byte(n))
  703. for i := 0; i < n; i++ {
  704. item, err := Marshal(info.Elem, rv.Index(i).Interface())
  705. if err != nil {
  706. return nil, err
  707. }
  708. if len(item) > math.MaxUint16 {
  709. return nil, marshalErrorf("marshal: slice / array item too large")
  710. }
  711. buf.WriteByte(byte(len(item) >> 8))
  712. buf.WriteByte(byte(len(item)))
  713. buf.Write(item)
  714. }
  715. return buf.Bytes(), nil
  716. }
  717. if k == reflect.Map {
  718. elem := t.Elem()
  719. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  720. rkeys := rv.MapKeys()
  721. keys := make([]interface{}, len(rkeys))
  722. for i := 0; i < len(keys); i++ {
  723. keys[i] = rkeys[i].Interface()
  724. }
  725. return marshalList(info, keys)
  726. }
  727. }
  728. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  729. }
  730. func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
  731. rv := reflect.ValueOf(value)
  732. if rv.Kind() != reflect.Ptr {
  733. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  734. }
  735. rv = rv.Elem()
  736. t := rv.Type()
  737. k := t.Kind()
  738. switch k {
  739. case reflect.Slice, reflect.Array:
  740. if len(data) < 2 {
  741. return unmarshalErrorf("unmarshal list: unexpected eof")
  742. }
  743. n := int(data[0]<<8) | int(data[1])
  744. data = data[2:]
  745. if k == reflect.Array {
  746. if rv.Len() != n {
  747. return unmarshalErrorf("unmarshal list: array with wrong size")
  748. }
  749. } else if rv.Cap() < n {
  750. rv.Set(reflect.MakeSlice(t, n, n))
  751. } else {
  752. rv.SetLen(n)
  753. }
  754. for i := 0; i < n; i++ {
  755. if len(data) < 2 {
  756. return unmarshalErrorf("unmarshal list: unexpected eof")
  757. }
  758. m := int(data[0]<<8) | int(data[1])
  759. data = data[2:]
  760. if err := Unmarshal(info.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  761. return err
  762. }
  763. data = data[m:]
  764. }
  765. return nil
  766. }
  767. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  768. }
  769. func marshalMap(info *TypeInfo, value interface{}) ([]byte, error) {
  770. rv := reflect.ValueOf(value)
  771. t := rv.Type()
  772. if t.Kind() != reflect.Map {
  773. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  774. }
  775. buf := &bytes.Buffer{}
  776. n := rv.Len()
  777. if n > math.MaxUint16 {
  778. return nil, marshalErrorf("marshal: map too large")
  779. }
  780. buf.WriteByte(byte(n >> 8))
  781. buf.WriteByte(byte(n))
  782. keys := rv.MapKeys()
  783. for _, key := range keys {
  784. item, err := Marshal(info.Key, key.Interface())
  785. if err != nil {
  786. return nil, err
  787. }
  788. if len(item) > math.MaxUint16 {
  789. return nil, marshalErrorf("marshal: slice / array item too large")
  790. }
  791. buf.WriteByte(byte(len(item) >> 8))
  792. buf.WriteByte(byte(len(item)))
  793. buf.Write(item)
  794. item, err = Marshal(info.Elem, rv.MapIndex(key).Interface())
  795. if err != nil {
  796. return nil, err
  797. }
  798. if len(item) > math.MaxUint16 {
  799. return nil, marshalErrorf("marshal: slice / array item too large")
  800. }
  801. buf.WriteByte(byte(len(item) >> 8))
  802. buf.WriteByte(byte(len(item)))
  803. buf.Write(item)
  804. }
  805. return buf.Bytes(), nil
  806. }
  807. func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
  808. rv := reflect.ValueOf(value)
  809. if rv.Kind() != reflect.Ptr {
  810. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  811. }
  812. rv = rv.Elem()
  813. t := rv.Type()
  814. if t.Kind() != reflect.Map {
  815. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  816. }
  817. rv.Set(reflect.MakeMap(t))
  818. if len(data) < 2 {
  819. return unmarshalErrorf("unmarshal map: unexpected eof")
  820. }
  821. n := int(data[0]<<8) | int(data[1])
  822. data = data[2:]
  823. for i := 0; i < n; i++ {
  824. if len(data) < 2 {
  825. return unmarshalErrorf("unmarshal list: unexpected eof")
  826. }
  827. m := int(data[0]<<8) | int(data[1])
  828. data = data[2:]
  829. key := reflect.New(t.Key())
  830. if err := Unmarshal(info.Key, data[:m], key.Interface()); err != nil {
  831. return err
  832. }
  833. data = data[m:]
  834. m = int(data[0]<<8) | int(data[1])
  835. data = data[2:]
  836. val := reflect.New(t.Elem())
  837. if err := Unmarshal(info.Elem, data[:m], val.Interface()); err != nil {
  838. return err
  839. }
  840. data = data[m:]
  841. rv.SetMapIndex(key.Elem(), val.Elem())
  842. }
  843. return nil
  844. }
  845. // TypeInfo describes a Cassandra specific data type.
  846. type TypeInfo struct {
  847. Type Type
  848. Key *TypeInfo // only used for TypeMap
  849. Elem *TypeInfo // only used for TypeMap, TypeList and TypeSet
  850. Custom string // only used for TypeCostum
  851. }
  852. // String returns a human readable name for the Cassandra datatype
  853. // described by t.
  854. func (t TypeInfo) String() string {
  855. switch t.Type {
  856. case TypeMap:
  857. return fmt.Sprintf("%s(%s, %s)", t.Type, t.Key, t.Elem)
  858. case TypeList, TypeSet:
  859. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  860. case TypeCustom:
  861. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  862. }
  863. return t.Type.String()
  864. }
  865. // Type is the identifier of a Cassandra internal datatype.
  866. type Type int
  867. const (
  868. TypeCustom Type = 0x0000
  869. TypeAscii Type = 0x0001
  870. TypeBigInt Type = 0x0002
  871. TypeBlob Type = 0x0003
  872. TypeBoolean Type = 0x0004
  873. TypeCounter Type = 0x0005
  874. TypeDecimal Type = 0x0006
  875. TypeDouble Type = 0x0007
  876. TypeFloat Type = 0x0008
  877. TypeInt Type = 0x0009
  878. TypeTimestamp Type = 0x000B
  879. TypeUUID Type = 0x000C
  880. TypeVarchar Type = 0x000D
  881. TypeVarint Type = 0x000E
  882. TypeTimeUUID Type = 0x000F
  883. TypeInet Type = 0x0010
  884. TypeList Type = 0x0020
  885. TypeMap Type = 0x0021
  886. TypeSet Type = 0x0022
  887. )
  888. // String returns the name of the identifier.
  889. func (t Type) String() string {
  890. switch t {
  891. case TypeCustom:
  892. return "custom"
  893. case TypeAscii:
  894. return "ascii"
  895. case TypeBigInt:
  896. return "bigint"
  897. case TypeBlob:
  898. return "blob"
  899. case TypeBoolean:
  900. return "boolean"
  901. case TypeCounter:
  902. return "counter"
  903. case TypeDecimal:
  904. return "decimal"
  905. case TypeDouble:
  906. return "double"
  907. case TypeFloat:
  908. return "float"
  909. case TypeInt:
  910. return "int"
  911. case TypeTimestamp:
  912. return "timestamp"
  913. case TypeUUID:
  914. return "uuid"
  915. case TypeVarchar:
  916. return "varchar"
  917. case TypeTimeUUID:
  918. return "timeuuid"
  919. case TypeInet:
  920. return "inet"
  921. case TypeList:
  922. return "list"
  923. case TypeMap:
  924. return "map"
  925. case TypeSet:
  926. return "set"
  927. default:
  928. return "unknown"
  929. }
  930. }
  931. type MarshalError string
  932. func (m MarshalError) Error() string {
  933. return string(m)
  934. }
  935. func marshalErrorf(format string, args ...interface{}) MarshalError {
  936. return MarshalError(fmt.Sprintf(format, args...))
  937. }
  938. type UnmarshalError string
  939. func (m UnmarshalError) Error() string {
  940. return string(m)
  941. }
  942. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  943. return UnmarshalError(fmt.Sprintf(format, args...))
  944. }