marshal.go 27 KB

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