marshal.go 27 KB

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