marshal.go 29 KB

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