marshal.go 30 KB

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