marshal.go 30 KB

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