marshal.go 30 KB

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