marshal.go 30 KB

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