marshal.go 27 KB

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