marshal.go 26 KB

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