marshal.go 27 KB

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