marshal.go 29 KB

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