marshal.go 27 KB

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