marshal.go 30 KB

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