marshal.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. if len(data) > 4 {
  681. denom := new(big.Int).SetBytes(data[0:4])
  682. num := new(big.Int).SetBytes(data[4:])
  683. *v = new(big.Rat).SetFrac(num, denom)
  684. }
  685. return nil
  686. }
  687. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  688. }
  689. func marshalTimestamp(info *TypeInfo, value interface{}) ([]byte, error) {
  690. switch v := value.(type) {
  691. case Marshaler:
  692. return v.MarshalCQL(info)
  693. case int64:
  694. return encBigInt(v), nil
  695. case time.Time:
  696. x := v.In(time.UTC).UnixNano() / int64(time.Millisecond)
  697. return encBigInt(x), nil
  698. }
  699. rv := reflect.ValueOf(value)
  700. switch rv.Type().Kind() {
  701. case reflect.Int64:
  702. return encBigInt(rv.Int()), nil
  703. case reflect.Ptr:
  704. return marshalTimestamp(info, rv.Elem().Interface())
  705. }
  706. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  707. }
  708. func unmarshalTimestamp(info *TypeInfo, data []byte, value interface{}) error {
  709. switch v := value.(type) {
  710. case Unmarshaler:
  711. return v.UnmarshalCQL(info, data)
  712. case *int64:
  713. *v = decBigInt(data)
  714. return nil
  715. case *time.Time:
  716. x := decBigInt(data)
  717. sec := x / 1000
  718. nsec := (x - sec*1000) * 1000000
  719. *v = time.Unix(sec, nsec).In(time.UTC)
  720. return nil
  721. }
  722. rv := reflect.ValueOf(value)
  723. if rv.Kind() != reflect.Ptr {
  724. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  725. }
  726. rv = rv.Elem()
  727. switch rv.Type().Kind() {
  728. case reflect.Int64:
  729. rv.SetInt(decBigInt(data))
  730. return nil
  731. }
  732. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  733. }
  734. func marshalList(info *TypeInfo, value interface{}) ([]byte, error) {
  735. rv := reflect.ValueOf(value)
  736. t := rv.Type()
  737. k := t.Kind()
  738. switch k {
  739. case reflect.Slice, reflect.Array:
  740. if k == reflect.Slice && rv.IsNil() {
  741. return nil, nil
  742. }
  743. buf := &bytes.Buffer{}
  744. n := rv.Len()
  745. if n > math.MaxUint16 {
  746. return nil, marshalErrorf("marshal: slice / array too large")
  747. }
  748. buf.WriteByte(byte(n >> 8))
  749. buf.WriteByte(byte(n))
  750. for i := 0; i < n; i++ {
  751. item, err := Marshal(info.Elem, rv.Index(i).Interface())
  752. if err != nil {
  753. return nil, err
  754. }
  755. if len(item) > math.MaxUint16 {
  756. return nil, marshalErrorf("marshal: slice / array item too large")
  757. }
  758. buf.WriteByte(byte(len(item) >> 8))
  759. buf.WriteByte(byte(len(item)))
  760. buf.Write(item)
  761. }
  762. return buf.Bytes(), nil
  763. }
  764. if k == reflect.Map {
  765. elem := t.Elem()
  766. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  767. rkeys := rv.MapKeys()
  768. keys := make([]interface{}, len(rkeys))
  769. for i := 0; i < len(keys); i++ {
  770. keys[i] = rkeys[i].Interface()
  771. }
  772. return marshalList(info, keys)
  773. }
  774. }
  775. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  776. }
  777. func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
  778. rv := reflect.ValueOf(value)
  779. if rv.Kind() != reflect.Ptr {
  780. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  781. }
  782. rv = rv.Elem()
  783. t := rv.Type()
  784. k := t.Kind()
  785. switch k {
  786. case reflect.Slice, reflect.Array:
  787. if data == nil {
  788. if k == reflect.Array {
  789. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  790. }
  791. rv.Set(reflect.Zero(t))
  792. return nil
  793. }
  794. if len(data) < 2 {
  795. return unmarshalErrorf("unmarshal list: unexpected eof")
  796. }
  797. n := int(data[0])<<8 | int(data[1])
  798. data = data[2:]
  799. if k == reflect.Array {
  800. if rv.Len() != n {
  801. return unmarshalErrorf("unmarshal list: array with wrong size")
  802. }
  803. } else if rv.Cap() < n {
  804. rv.Set(reflect.MakeSlice(t, n, n))
  805. } else {
  806. rv.SetLen(n)
  807. }
  808. for i := 0; i < n; i++ {
  809. if len(data) < 2 {
  810. return unmarshalErrorf("unmarshal list: unexpected eof")
  811. }
  812. m := int(data[0])<<8 | int(data[1])
  813. data = data[2:]
  814. if err := Unmarshal(info.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  815. return err
  816. }
  817. data = data[m:]
  818. }
  819. return nil
  820. }
  821. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  822. }
  823. func marshalMap(info *TypeInfo, value interface{}) ([]byte, error) {
  824. rv := reflect.ValueOf(value)
  825. t := rv.Type()
  826. if t.Kind() != reflect.Map {
  827. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  828. }
  829. if rv.IsNil() {
  830. return nil, nil
  831. }
  832. buf := &bytes.Buffer{}
  833. n := rv.Len()
  834. if n > math.MaxUint16 {
  835. return nil, marshalErrorf("marshal: map too large")
  836. }
  837. buf.WriteByte(byte(n >> 8))
  838. buf.WriteByte(byte(n))
  839. keys := rv.MapKeys()
  840. for _, key := range keys {
  841. item, err := Marshal(info.Key, key.Interface())
  842. if err != nil {
  843. return nil, err
  844. }
  845. if len(item) > math.MaxUint16 {
  846. return nil, marshalErrorf("marshal: slice / array item too large")
  847. }
  848. buf.WriteByte(byte(len(item) >> 8))
  849. buf.WriteByte(byte(len(item)))
  850. buf.Write(item)
  851. item, err = Marshal(info.Elem, rv.MapIndex(key).Interface())
  852. if err != nil {
  853. return nil, err
  854. }
  855. if len(item) > math.MaxUint16 {
  856. return nil, marshalErrorf("marshal: slice / array item too large")
  857. }
  858. buf.WriteByte(byte(len(item) >> 8))
  859. buf.WriteByte(byte(len(item)))
  860. buf.Write(item)
  861. }
  862. return buf.Bytes(), nil
  863. }
  864. func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
  865. rv := reflect.ValueOf(value)
  866. if rv.Kind() != reflect.Ptr {
  867. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  868. }
  869. rv = rv.Elem()
  870. t := rv.Type()
  871. if t.Kind() != reflect.Map {
  872. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  873. }
  874. if data == nil {
  875. rv.Set(reflect.Zero(t))
  876. return nil
  877. }
  878. rv.Set(reflect.MakeMap(t))
  879. if len(data) < 2 {
  880. return unmarshalErrorf("unmarshal map: unexpected eof")
  881. }
  882. n := int(data[1]) | int(data[0])<<8
  883. data = data[2:]
  884. for i := 0; i < n; i++ {
  885. if len(data) < 2 {
  886. return unmarshalErrorf("unmarshal list: unexpected eof")
  887. }
  888. m := int(data[1]) | int(data[0])<<8
  889. data = data[2:]
  890. key := reflect.New(t.Key())
  891. if err := Unmarshal(info.Key, data[:m], key.Interface()); err != nil {
  892. return err
  893. }
  894. data = data[m:]
  895. m = int(data[1]) | int(data[0])<<8
  896. data = data[2:]
  897. val := reflect.New(t.Elem())
  898. if err := Unmarshal(info.Elem, data[:m], val.Interface()); err != nil {
  899. return err
  900. }
  901. data = data[m:]
  902. rv.SetMapIndex(key.Elem(), val.Elem())
  903. }
  904. return nil
  905. }
  906. func marshalUUID(info *TypeInfo, value interface{}) ([]byte, error) {
  907. switch val := value.(type) {
  908. case UUID:
  909. return val.Bytes(), nil
  910. case []byte:
  911. if len(val) == 16 {
  912. return val, nil
  913. }
  914. case string:
  915. b, err := ParseUUID(val)
  916. if err != nil {
  917. return nil, err
  918. }
  919. return b[:], nil
  920. }
  921. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  922. }
  923. func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
  924. if data == nil || len(data) == 0 {
  925. switch v := value.(type) {
  926. case *string:
  927. *v = ""
  928. case *[]byte:
  929. *v = nil
  930. case *UUID:
  931. *v = UUID{}
  932. default:
  933. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  934. }
  935. return nil
  936. }
  937. u, err := UUIDFromBytes(data)
  938. if err != nil {
  939. return unmarshalErrorf("Unable to parse UUID: %s", err)
  940. }
  941. switch v := value.(type) {
  942. case *string:
  943. *v = u.String()
  944. return nil
  945. case *[]byte:
  946. *v = u[:]
  947. return nil
  948. case *UUID:
  949. *v = u
  950. return nil
  951. }
  952. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  953. }
  954. func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
  955. switch v := value.(type) {
  956. case Unmarshaler:
  957. return v.UnmarshalCQL(info, data)
  958. case *time.Time:
  959. id, err := UUIDFromBytes(data)
  960. if err != nil {
  961. return err
  962. } else if id.Version() != 1 {
  963. return unmarshalErrorf("invalid timeuuid")
  964. }
  965. *v = id.Time()
  966. return nil
  967. default:
  968. return unmarshalUUID(info, data, value)
  969. }
  970. }
  971. func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {
  972. switch v := value.(type) {
  973. case Unmarshaler:
  974. return v.UnmarshalCQL(info, data)
  975. case *string:
  976. if len(data) == 0 {
  977. *v = ""
  978. return nil
  979. }
  980. if len(data) == 4 {
  981. *v = fmt.Sprintf("%d.%d.%d.%d", data[0], data[1], data[2], data[3])
  982. return nil
  983. }
  984. // TODO: support IPv6
  985. }
  986. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  987. }
  988. // TypeInfo describes a Cassandra specific data type.
  989. type TypeInfo struct {
  990. Type Type
  991. Key *TypeInfo // only used for TypeMap
  992. Elem *TypeInfo // only used for TypeMap, TypeList and TypeSet
  993. Custom string // only used for TypeCostum
  994. }
  995. // String returns a human readable name for the Cassandra datatype
  996. // described by t.
  997. func (t TypeInfo) String() string {
  998. switch t.Type {
  999. case TypeMap:
  1000. return fmt.Sprintf("%s(%s, %s)", t.Type, t.Key, t.Elem)
  1001. case TypeList, TypeSet:
  1002. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  1003. case TypeCustom:
  1004. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  1005. }
  1006. return t.Type.String()
  1007. }
  1008. // Type is the identifier of a Cassandra internal datatype.
  1009. type Type int
  1010. const (
  1011. TypeCustom Type = 0x0000
  1012. TypeAscii Type = 0x0001
  1013. TypeBigInt Type = 0x0002
  1014. TypeBlob Type = 0x0003
  1015. TypeBoolean Type = 0x0004
  1016. TypeCounter Type = 0x0005
  1017. TypeDecimal Type = 0x0006
  1018. TypeDouble Type = 0x0007
  1019. TypeFloat Type = 0x0008
  1020. TypeInt Type = 0x0009
  1021. TypeTimestamp Type = 0x000B
  1022. TypeUUID Type = 0x000C
  1023. TypeVarchar Type = 0x000D
  1024. TypeVarint Type = 0x000E
  1025. TypeTimeUUID Type = 0x000F
  1026. TypeInet Type = 0x0010
  1027. TypeList Type = 0x0020
  1028. TypeMap Type = 0x0021
  1029. TypeSet Type = 0x0022
  1030. )
  1031. // String returns the name of the identifier.
  1032. func (t Type) String() string {
  1033. switch t {
  1034. case TypeCustom:
  1035. return "custom"
  1036. case TypeAscii:
  1037. return "ascii"
  1038. case TypeBigInt:
  1039. return "bigint"
  1040. case TypeBlob:
  1041. return "blob"
  1042. case TypeBoolean:
  1043. return "boolean"
  1044. case TypeCounter:
  1045. return "counter"
  1046. case TypeDecimal:
  1047. return "decimal"
  1048. case TypeDouble:
  1049. return "double"
  1050. case TypeFloat:
  1051. return "float"
  1052. case TypeInt:
  1053. return "int"
  1054. case TypeTimestamp:
  1055. return "timestamp"
  1056. case TypeUUID:
  1057. return "uuid"
  1058. case TypeVarchar:
  1059. return "varchar"
  1060. case TypeTimeUUID:
  1061. return "timeuuid"
  1062. case TypeInet:
  1063. return "inet"
  1064. case TypeList:
  1065. return "list"
  1066. case TypeMap:
  1067. return "map"
  1068. case TypeSet:
  1069. return "set"
  1070. default:
  1071. return "unknown"
  1072. }
  1073. }
  1074. type MarshalError string
  1075. func (m MarshalError) Error() string {
  1076. return string(m)
  1077. }
  1078. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1079. return MarshalError(fmt.Sprintf(format, args...))
  1080. }
  1081. type UnmarshalError string
  1082. func (m UnmarshalError) Error() string {
  1083. return string(m)
  1084. }
  1085. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1086. return UnmarshalError(fmt.Sprintf(format, args...))
  1087. }