marshal.go 31 KB

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