marshal.go 30 KB

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