marshal.go 30 KB

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