marshal.go 30 KB

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