marshal.go 28 KB

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