marshal.go 29 KB

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