marshal.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 decInt(x []byte) int32 {
  221. if len(x) != 4 {
  222. return 0
  223. }
  224. return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3])
  225. }
  226. func marshalBigInt(info *TypeInfo, value interface{}) ([]byte, error) {
  227. switch v := value.(type) {
  228. case Marshaler:
  229. return v.MarshalCQL(info)
  230. case int:
  231. return encBigInt(int64(v)), nil
  232. case uint:
  233. if uint64(v) > math.MaxInt64 {
  234. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  235. }
  236. return encBigInt(int64(v)), nil
  237. case int64:
  238. return encBigInt(v), nil
  239. case uint64:
  240. if v > math.MaxInt64 {
  241. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  242. }
  243. return encBigInt(int64(v)), nil
  244. case int32:
  245. return encBigInt(int64(v)), nil
  246. case uint32:
  247. return encBigInt(int64(v)), nil
  248. case int16:
  249. return encBigInt(int64(v)), nil
  250. case uint16:
  251. return encBigInt(int64(v)), nil
  252. case int8:
  253. return encBigInt(int64(v)), nil
  254. case uint8:
  255. return encBigInt(int64(v)), nil
  256. }
  257. rv := reflect.ValueOf(value)
  258. switch rv.Type().Kind() {
  259. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  260. v := rv.Int()
  261. return encBigInt(v), nil
  262. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  263. v := rv.Uint()
  264. if v > math.MaxInt64 {
  265. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  266. }
  267. return encBigInt(int64(v)), nil
  268. case reflect.Ptr:
  269. return marshalBigInt(info, rv.Elem().Interface())
  270. }
  271. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  272. }
  273. func encBigInt(x int64) []byte {
  274. return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32),
  275. byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  276. }
  277. func unmarshalBigInt(info *TypeInfo, data []byte, value interface{}) error {
  278. return unmarshalIntlike(info, decBigInt(data), data, value)
  279. }
  280. func unmarshalInt(info *TypeInfo, data []byte, value interface{}) error {
  281. return unmarshalIntlike(info, int64(decInt(data)), data, value)
  282. }
  283. func unmarshalIntlike(info *TypeInfo, int64Val int64, data []byte, value interface{}) error {
  284. switch v := value.(type) {
  285. case *int:
  286. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  287. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  288. }
  289. *v = int(int64Val)
  290. return nil
  291. case *uint:
  292. if int64Val < 0 || (^uint(0) == math.MaxUint32 && int64Val > math.MaxUint32) {
  293. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  294. }
  295. *v = uint(int64Val)
  296. return nil
  297. case *int64:
  298. *v = int64Val
  299. return nil
  300. case *uint64:
  301. if int64Val < 0 {
  302. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  303. }
  304. *v = uint64(int64Val)
  305. return nil
  306. case *int32:
  307. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  308. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  309. }
  310. *v = int32(int64Val)
  311. return nil
  312. case *uint32:
  313. if int64Val < 0 || int64Val > math.MaxUint32 {
  314. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  315. }
  316. *v = uint32(int64Val)
  317. return nil
  318. case *int16:
  319. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  320. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  321. }
  322. *v = int16(int64Val)
  323. return nil
  324. case *uint16:
  325. if int64Val < 0 || int64Val > math.MaxUint16 {
  326. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  327. }
  328. *v = uint16(int64Val)
  329. return nil
  330. case *int8:
  331. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  332. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  333. }
  334. *v = int8(int64Val)
  335. return nil
  336. case *uint8:
  337. if int64Val < 0 || int64Val > math.MaxUint8 {
  338. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  339. }
  340. *v = uint8(int64Val)
  341. return nil
  342. }
  343. rv := reflect.ValueOf(value)
  344. if rv.Kind() != reflect.Ptr {
  345. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  346. }
  347. rv = rv.Elem()
  348. switch rv.Type().Kind() {
  349. case reflect.Int:
  350. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  351. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  352. }
  353. rv.SetInt(int64Val)
  354. return nil
  355. case reflect.Int64:
  356. rv.SetInt(int64Val)
  357. return nil
  358. case reflect.Int32:
  359. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  360. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  361. }
  362. rv.SetInt(int64Val)
  363. return nil
  364. case reflect.Int16:
  365. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  366. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  367. }
  368. rv.SetInt(int64Val)
  369. return nil
  370. case reflect.Int8:
  371. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  372. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  373. }
  374. rv.SetInt(int64Val)
  375. return nil
  376. case reflect.Uint:
  377. if int64Val < 0 || (^uint(0) == math.MaxUint32 && int64Val > math.MaxUint32) {
  378. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  379. }
  380. rv.SetUint(uint64(int64Val))
  381. return nil
  382. case reflect.Uint64:
  383. if int64Val < 0 {
  384. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  385. }
  386. rv.SetUint(uint64(int64Val))
  387. return nil
  388. case reflect.Uint32:
  389. if int64Val < 0 || int64Val > math.MaxUint32 {
  390. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  391. }
  392. rv.SetUint(uint64(int64Val))
  393. return nil
  394. case reflect.Uint16:
  395. if int64Val < 0 || int64Val > math.MaxUint16 {
  396. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  397. }
  398. rv.SetUint(uint64(int64Val))
  399. return nil
  400. case reflect.Uint8:
  401. if int64Val < 0 || int64Val > math.MaxUint8 {
  402. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  403. }
  404. rv.SetUint(uint64(int64Val))
  405. return nil
  406. }
  407. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  408. }
  409. func decBigInt(data []byte) int64 {
  410. if len(data) != 8 {
  411. return 0
  412. }
  413. return int64(data[0])<<56 | int64(data[1])<<48 |
  414. int64(data[2])<<40 | int64(data[3])<<32 |
  415. int64(data[4])<<24 | int64(data[5])<<16 |
  416. int64(data[6])<<8 | int64(data[7])
  417. }
  418. func marshalBool(info *TypeInfo, value interface{}) ([]byte, error) {
  419. switch v := value.(type) {
  420. case Marshaler:
  421. return v.MarshalCQL(info)
  422. case bool:
  423. return encBool(v), nil
  424. }
  425. rv := reflect.ValueOf(value)
  426. switch rv.Type().Kind() {
  427. case reflect.Bool:
  428. return encBool(rv.Bool()), nil
  429. case reflect.Ptr:
  430. return marshalBool(info, rv.Elem().Interface())
  431. }
  432. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  433. }
  434. func encBool(v bool) []byte {
  435. if v {
  436. return []byte{1}
  437. }
  438. return []byte{0}
  439. }
  440. func unmarshalBool(info *TypeInfo, data []byte, value interface{}) error {
  441. switch v := value.(type) {
  442. case Unmarshaler:
  443. return v.UnmarshalCQL(info, data)
  444. case *bool:
  445. *v = decBool(data)
  446. return nil
  447. }
  448. rv := reflect.ValueOf(value)
  449. if rv.Kind() != reflect.Ptr {
  450. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  451. }
  452. rv = rv.Elem()
  453. switch rv.Type().Kind() {
  454. case reflect.Bool:
  455. rv.SetBool(decBool(data))
  456. return nil
  457. }
  458. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  459. }
  460. func decBool(v []byte) bool {
  461. if len(v) == 0 {
  462. return false
  463. }
  464. return v[0] != 0
  465. }
  466. func marshalFloat(info *TypeInfo, value interface{}) ([]byte, error) {
  467. switch v := value.(type) {
  468. case Marshaler:
  469. return v.MarshalCQL(info)
  470. case float32:
  471. return encInt(int32(math.Float32bits(v))), nil
  472. }
  473. rv := reflect.ValueOf(value)
  474. switch rv.Type().Kind() {
  475. case reflect.Float32:
  476. return encInt(int32(math.Float32bits(float32(rv.Float())))), nil
  477. case reflect.Ptr:
  478. return marshalFloat(info, rv.Elem().Interface())
  479. }
  480. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  481. }
  482. func unmarshalFloat(info *TypeInfo, data []byte, value interface{}) error {
  483. switch v := value.(type) {
  484. case Unmarshaler:
  485. return v.UnmarshalCQL(info, data)
  486. case *float32:
  487. *v = math.Float32frombits(uint32(decInt(data)))
  488. return nil
  489. }
  490. rv := reflect.ValueOf(value)
  491. if rv.Kind() != reflect.Ptr {
  492. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  493. }
  494. rv = rv.Elem()
  495. switch rv.Type().Kind() {
  496. case reflect.Float32:
  497. rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data)))))
  498. return nil
  499. }
  500. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  501. }
  502. func marshalDouble(info *TypeInfo, value interface{}) ([]byte, error) {
  503. switch v := value.(type) {
  504. case Marshaler:
  505. return v.MarshalCQL(info)
  506. case float64:
  507. return encBigInt(int64(math.Float64bits(v))), nil
  508. }
  509. rv := reflect.ValueOf(value)
  510. switch rv.Type().Kind() {
  511. case reflect.Float64:
  512. return encBigInt(int64(math.Float64bits(rv.Float()))), nil
  513. case reflect.Ptr:
  514. return marshalDouble(info, rv.Elem().Interface())
  515. }
  516. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  517. }
  518. func unmarshalDouble(info *TypeInfo, data []byte, value interface{}) error {
  519. switch v := value.(type) {
  520. case Unmarshaler:
  521. return v.UnmarshalCQL(info, data)
  522. case *float64:
  523. *v = math.Float64frombits(uint64(decBigInt(data)))
  524. return nil
  525. }
  526. rv := reflect.ValueOf(value)
  527. if rv.Kind() != reflect.Ptr {
  528. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  529. }
  530. rv = rv.Elem()
  531. switch rv.Type().Kind() {
  532. case reflect.Float64:
  533. rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
  534. return nil
  535. }
  536. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  537. }
  538. func marshalDecimal(info *TypeInfo, value interface{}) ([]byte, error) {
  539. switch v := value.(type) {
  540. case Marshaler:
  541. return v.MarshalCQL(info)
  542. case *inf.Dec:
  543. if v == nil {
  544. return nil, nil
  545. }
  546. unscaled := encBigInt2C(v.UnscaledBig())
  547. if unscaled == nil {
  548. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  549. }
  550. buf := make([]byte, 4+len(unscaled))
  551. copy(buf[0:4], encInt(int32(v.Scale())))
  552. copy(buf[4:], unscaled)
  553. return buf, nil
  554. }
  555. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  556. }
  557. func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
  558. switch v := value.(type) {
  559. case Unmarshaler:
  560. return v.UnmarshalCQL(info, data)
  561. case **inf.Dec:
  562. if len(data) > 4 {
  563. scale := decInt(data[0:4])
  564. unscaled := decBigInt2C(data[4:])
  565. *v = inf.NewDecBig(unscaled, inf.Scale(scale))
  566. return nil
  567. } else if len(data) == 0 {
  568. *v = nil
  569. return nil
  570. } else {
  571. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  572. }
  573. }
  574. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  575. }
  576. // decBigInt2C sets the value of n to the big-endian two's complement
  577. // value stored in the given data. If data[0]&80 != 0, the number
  578. // is negative. If data is empty, the result will be 0.
  579. func decBigInt2C(data []byte) *big.Int {
  580. n := new(big.Int).SetBytes(data)
  581. if len(data) > 0 && data[0]&0x80 > 0 {
  582. n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8))
  583. }
  584. return n
  585. }
  586. // encBigInt2C returns the big-endian two's complement
  587. // form of n.
  588. func encBigInt2C(n *big.Int) []byte {
  589. switch n.Sign() {
  590. case 0:
  591. return []byte{0}
  592. case 1:
  593. b := n.Bytes()
  594. if b[0]&0x80 > 0 {
  595. b = append([]byte{0}, b...)
  596. }
  597. return b
  598. case -1:
  599. length := uint(n.BitLen()/8+1) * 8
  600. b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes()
  601. // When the most significant bit is on a byte
  602. // boundary, we can get some extra significant
  603. // bits, so strip them off when that happens.
  604. if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
  605. b = b[1:]
  606. }
  607. return b
  608. }
  609. return nil
  610. }
  611. func marshalTimestamp(info *TypeInfo, value interface{}) ([]byte, error) {
  612. switch v := value.(type) {
  613. case Marshaler:
  614. return v.MarshalCQL(info)
  615. case int64:
  616. return encBigInt(v), nil
  617. case time.Time:
  618. x := v.In(time.UTC).UnixNano() / int64(time.Millisecond)
  619. return encBigInt(x), nil
  620. }
  621. rv := reflect.ValueOf(value)
  622. switch rv.Type().Kind() {
  623. case reflect.Int64:
  624. return encBigInt(rv.Int()), nil
  625. case reflect.Ptr:
  626. return marshalTimestamp(info, rv.Elem().Interface())
  627. }
  628. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  629. }
  630. func unmarshalTimestamp(info *TypeInfo, data []byte, value interface{}) error {
  631. switch v := value.(type) {
  632. case Unmarshaler:
  633. return v.UnmarshalCQL(info, data)
  634. case *int64:
  635. *v = decBigInt(data)
  636. return nil
  637. case *time.Time:
  638. x := decBigInt(data)
  639. sec := x / 1000
  640. nsec := (x - sec*1000) * 1000000
  641. *v = time.Unix(sec, nsec).In(time.UTC)
  642. return nil
  643. }
  644. rv := reflect.ValueOf(value)
  645. if rv.Kind() != reflect.Ptr {
  646. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  647. }
  648. rv = rv.Elem()
  649. switch rv.Type().Kind() {
  650. case reflect.Int64:
  651. rv.SetInt(decBigInt(data))
  652. return nil
  653. }
  654. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  655. }
  656. func marshalList(info *TypeInfo, value interface{}) ([]byte, error) {
  657. rv := reflect.ValueOf(value)
  658. t := rv.Type()
  659. k := t.Kind()
  660. switch k {
  661. case reflect.Slice, reflect.Array:
  662. if k == reflect.Slice && rv.IsNil() {
  663. return nil, nil
  664. }
  665. buf := &bytes.Buffer{}
  666. n := rv.Len()
  667. if n > math.MaxUint16 {
  668. return nil, marshalErrorf("marshal: slice / array too large")
  669. }
  670. buf.WriteByte(byte(n >> 8))
  671. buf.WriteByte(byte(n))
  672. for i := 0; i < n; i++ {
  673. item, err := Marshal(info.Elem, rv.Index(i).Interface())
  674. if err != nil {
  675. return nil, err
  676. }
  677. if len(item) > math.MaxUint16 {
  678. return nil, marshalErrorf("marshal: slice / array item too large")
  679. }
  680. buf.WriteByte(byte(len(item) >> 8))
  681. buf.WriteByte(byte(len(item)))
  682. buf.Write(item)
  683. }
  684. return buf.Bytes(), nil
  685. }
  686. if k == reflect.Map {
  687. elem := t.Elem()
  688. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  689. rkeys := rv.MapKeys()
  690. keys := make([]interface{}, len(rkeys))
  691. for i := 0; i < len(keys); i++ {
  692. keys[i] = rkeys[i].Interface()
  693. }
  694. return marshalList(info, keys)
  695. }
  696. }
  697. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  698. }
  699. func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
  700. rv := reflect.ValueOf(value)
  701. if rv.Kind() != reflect.Ptr {
  702. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  703. }
  704. rv = rv.Elem()
  705. t := rv.Type()
  706. k := t.Kind()
  707. switch k {
  708. case reflect.Slice, reflect.Array:
  709. if data == nil {
  710. if k == reflect.Array {
  711. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  712. }
  713. rv.Set(reflect.Zero(t))
  714. return nil
  715. }
  716. if len(data) < 2 {
  717. return unmarshalErrorf("unmarshal list: unexpected eof")
  718. }
  719. n := int(data[0])<<8 | int(data[1])
  720. data = data[2:]
  721. if k == reflect.Array {
  722. if rv.Len() != n {
  723. return unmarshalErrorf("unmarshal list: array with wrong size")
  724. }
  725. } else if rv.Cap() < n {
  726. rv.Set(reflect.MakeSlice(t, n, n))
  727. } else {
  728. rv.SetLen(n)
  729. }
  730. for i := 0; i < n; i++ {
  731. if len(data) < 2 {
  732. return unmarshalErrorf("unmarshal list: unexpected eof")
  733. }
  734. m := int(data[0])<<8 | int(data[1])
  735. data = data[2:]
  736. if err := Unmarshal(info.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  737. return err
  738. }
  739. data = data[m:]
  740. }
  741. return nil
  742. }
  743. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  744. }
  745. func marshalMap(info *TypeInfo, value interface{}) ([]byte, error) {
  746. rv := reflect.ValueOf(value)
  747. t := rv.Type()
  748. if t.Kind() != reflect.Map {
  749. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  750. }
  751. if rv.IsNil() {
  752. return nil, nil
  753. }
  754. buf := &bytes.Buffer{}
  755. n := rv.Len()
  756. if n > math.MaxUint16 {
  757. return nil, marshalErrorf("marshal: map too large")
  758. }
  759. buf.WriteByte(byte(n >> 8))
  760. buf.WriteByte(byte(n))
  761. keys := rv.MapKeys()
  762. for _, key := range keys {
  763. item, err := Marshal(info.Key, key.Interface())
  764. if err != nil {
  765. return nil, err
  766. }
  767. if len(item) > math.MaxUint16 {
  768. return nil, marshalErrorf("marshal: slice / array item too large")
  769. }
  770. buf.WriteByte(byte(len(item) >> 8))
  771. buf.WriteByte(byte(len(item)))
  772. buf.Write(item)
  773. item, err = Marshal(info.Elem, rv.MapIndex(key).Interface())
  774. if err != nil {
  775. return nil, err
  776. }
  777. if len(item) > math.MaxUint16 {
  778. return nil, marshalErrorf("marshal: slice / array item too large")
  779. }
  780. buf.WriteByte(byte(len(item) >> 8))
  781. buf.WriteByte(byte(len(item)))
  782. buf.Write(item)
  783. }
  784. return buf.Bytes(), nil
  785. }
  786. func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
  787. rv := reflect.ValueOf(value)
  788. if rv.Kind() != reflect.Ptr {
  789. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  790. }
  791. rv = rv.Elem()
  792. t := rv.Type()
  793. if t.Kind() != reflect.Map {
  794. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  795. }
  796. if data == nil {
  797. rv.Set(reflect.Zero(t))
  798. return nil
  799. }
  800. rv.Set(reflect.MakeMap(t))
  801. if len(data) < 2 {
  802. return unmarshalErrorf("unmarshal map: unexpected eof")
  803. }
  804. n := int(data[1]) | int(data[0])<<8
  805. data = data[2:]
  806. for i := 0; i < n; i++ {
  807. if len(data) < 2 {
  808. return unmarshalErrorf("unmarshal list: unexpected eof")
  809. }
  810. m := int(data[1]) | int(data[0])<<8
  811. data = data[2:]
  812. key := reflect.New(t.Key())
  813. if err := Unmarshal(info.Key, data[:m], key.Interface()); err != nil {
  814. return err
  815. }
  816. data = data[m:]
  817. m = int(data[1]) | int(data[0])<<8
  818. data = data[2:]
  819. val := reflect.New(t.Elem())
  820. if err := Unmarshal(info.Elem, data[:m], val.Interface()); err != nil {
  821. return err
  822. }
  823. data = data[m:]
  824. rv.SetMapIndex(key.Elem(), val.Elem())
  825. }
  826. return nil
  827. }
  828. func marshalUUID(info *TypeInfo, value interface{}) ([]byte, error) {
  829. switch val := value.(type) {
  830. case UUID:
  831. return val.Bytes(), nil
  832. case []byte:
  833. if len(val) == 16 {
  834. return val, nil
  835. }
  836. case string:
  837. b, err := ParseUUID(val)
  838. if err != nil {
  839. return nil, err
  840. }
  841. return b[:], nil
  842. }
  843. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  844. }
  845. func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
  846. if data == nil || len(data) == 0 {
  847. switch v := value.(type) {
  848. case *string:
  849. *v = ""
  850. case *[]byte:
  851. *v = nil
  852. case *UUID:
  853. *v = UUID{}
  854. default:
  855. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  856. }
  857. return nil
  858. }
  859. u, err := UUIDFromBytes(data)
  860. if err != nil {
  861. return unmarshalErrorf("Unable to parse UUID: %s", err)
  862. }
  863. switch v := value.(type) {
  864. case *string:
  865. *v = u.String()
  866. return nil
  867. case *[]byte:
  868. *v = u[:]
  869. return nil
  870. case *UUID:
  871. *v = u
  872. return nil
  873. }
  874. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  875. }
  876. func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
  877. switch v := value.(type) {
  878. case Unmarshaler:
  879. return v.UnmarshalCQL(info, data)
  880. case *time.Time:
  881. id, err := UUIDFromBytes(data)
  882. if err != nil {
  883. return err
  884. } else if id.Version() != 1 {
  885. return unmarshalErrorf("invalid timeuuid")
  886. }
  887. *v = id.Time()
  888. return nil
  889. default:
  890. return unmarshalUUID(info, data, value)
  891. }
  892. }
  893. func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {
  894. switch v := value.(type) {
  895. case Unmarshaler:
  896. return v.UnmarshalCQL(info, data)
  897. case *string:
  898. if len(data) == 0 {
  899. *v = ""
  900. return nil
  901. }
  902. if len(data) == 4 {
  903. *v = fmt.Sprintf("%d.%d.%d.%d", data[0], data[1], data[2], data[3])
  904. return nil
  905. }
  906. // TODO: support IPv6
  907. }
  908. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  909. }
  910. // TypeInfo describes a Cassandra specific data type.
  911. type TypeInfo struct {
  912. Type Type
  913. Key *TypeInfo // only used for TypeMap
  914. Elem *TypeInfo // only used for TypeMap, TypeList and TypeSet
  915. Custom string // only used for TypeCostum
  916. }
  917. // String returns a human readable name for the Cassandra datatype
  918. // described by t.
  919. func (t TypeInfo) String() string {
  920. switch t.Type {
  921. case TypeMap:
  922. return fmt.Sprintf("%s(%s, %s)", t.Type, t.Key, t.Elem)
  923. case TypeList, TypeSet:
  924. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  925. case TypeCustom:
  926. return fmt.Sprintf("%s(%s)", t.Type, t.Custom)
  927. }
  928. return t.Type.String()
  929. }
  930. // Type is the identifier of a Cassandra internal datatype.
  931. type Type int
  932. const (
  933. TypeCustom Type = 0x0000
  934. TypeAscii Type = 0x0001
  935. TypeBigInt Type = 0x0002
  936. TypeBlob Type = 0x0003
  937. TypeBoolean Type = 0x0004
  938. TypeCounter Type = 0x0005
  939. TypeDecimal Type = 0x0006
  940. TypeDouble Type = 0x0007
  941. TypeFloat Type = 0x0008
  942. TypeInt Type = 0x0009
  943. TypeTimestamp Type = 0x000B
  944. TypeUUID Type = 0x000C
  945. TypeVarchar Type = 0x000D
  946. TypeVarint Type = 0x000E
  947. TypeTimeUUID Type = 0x000F
  948. TypeInet Type = 0x0010
  949. TypeList Type = 0x0020
  950. TypeMap Type = 0x0021
  951. TypeSet Type = 0x0022
  952. )
  953. // String returns the name of the identifier.
  954. func (t Type) String() string {
  955. switch t {
  956. case TypeCustom:
  957. return "custom"
  958. case TypeAscii:
  959. return "ascii"
  960. case TypeBigInt:
  961. return "bigint"
  962. case TypeBlob:
  963. return "blob"
  964. case TypeBoolean:
  965. return "boolean"
  966. case TypeCounter:
  967. return "counter"
  968. case TypeDecimal:
  969. return "decimal"
  970. case TypeDouble:
  971. return "double"
  972. case TypeFloat:
  973. return "float"
  974. case TypeInt:
  975. return "int"
  976. case TypeTimestamp:
  977. return "timestamp"
  978. case TypeUUID:
  979. return "uuid"
  980. case TypeVarchar:
  981. return "varchar"
  982. case TypeTimeUUID:
  983. return "timeuuid"
  984. case TypeInet:
  985. return "inet"
  986. case TypeList:
  987. return "list"
  988. case TypeMap:
  989. return "map"
  990. case TypeSet:
  991. return "set"
  992. default:
  993. return "unknown"
  994. }
  995. }
  996. type MarshalError string
  997. func (m MarshalError) Error() string {
  998. return string(m)
  999. }
  1000. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1001. return MarshalError(fmt.Sprintf(format, args...))
  1002. }
  1003. type UnmarshalError string
  1004. func (m UnmarshalError) Error() string {
  1005. return string(m)
  1006. }
  1007. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1008. return UnmarshalError(fmt.Sprintf(format, args...))
  1009. }