marshal.go 29 KB

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