marshal.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. bigOne = big.NewInt(1)
  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. return nil
  691. } else if len(data) == 0 {
  692. return nil
  693. } else {
  694. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  695. }
  696. }
  697. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  698. }
  699. // decBigInt2C sets the value of n to the big-endian two's complement
  700. // value stored in the given data. If data[0]&80 != 0, the number
  701. // is negative. If data is empty, the result will be 0.
  702. func decBigInt2C(data []byte) *big.Int {
  703. n := new(big.Int).SetBytes(data)
  704. if len(data) > 0 && data[0]&0x80 > 0 {
  705. n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8))
  706. }
  707. return n
  708. }
  709. // encBigInt2C returns the big-endian two's complement
  710. // form of n.
  711. func encBigInt2C(n *big.Int) []byte {
  712. switch n.Sign() {
  713. case 0:
  714. return []byte{0}
  715. case 1:
  716. b := n.Bytes()
  717. if b[0]&0x80 > 0 {
  718. b = append([]byte{0}, b...)
  719. }
  720. return b
  721. case -1:
  722. length := uint(n.BitLen()/8+1) * 8
  723. b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes()
  724. // When the most significant bit is on a byte
  725. // boundary, we can get some extra significant
  726. // bits, so strip them off when that happens.
  727. if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
  728. b = b[1:]
  729. }
  730. return b
  731. }
  732. return nil
  733. }
  734. func marshalTimestamp(info *TypeInfo, value interface{}) ([]byte, error) {
  735. switch v := value.(type) {
  736. case Marshaler:
  737. return v.MarshalCQL(info)
  738. case int64:
  739. return encBigInt(v), nil
  740. case time.Time:
  741. x := v.In(time.UTC).UnixNano() / int64(time.Millisecond)
  742. return encBigInt(x), nil
  743. }
  744. rv := reflect.ValueOf(value)
  745. switch rv.Type().Kind() {
  746. case reflect.Int64:
  747. return encBigInt(rv.Int()), nil
  748. case reflect.Ptr:
  749. return marshalTimestamp(info, rv.Elem().Interface())
  750. }
  751. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  752. }
  753. func unmarshalTimestamp(info *TypeInfo, data []byte, value interface{}) error {
  754. switch v := value.(type) {
  755. case Unmarshaler:
  756. return v.UnmarshalCQL(info, data)
  757. case *int64:
  758. *v = decBigInt(data)
  759. return nil
  760. case *time.Time:
  761. x := decBigInt(data)
  762. sec := x / 1000
  763. nsec := (x - sec*1000) * 1000000
  764. *v = time.Unix(sec, nsec).In(time.UTC)
  765. return nil
  766. }
  767. rv := reflect.ValueOf(value)
  768. if rv.Kind() != reflect.Ptr {
  769. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  770. }
  771. rv = rv.Elem()
  772. switch rv.Type().Kind() {
  773. case reflect.Int64:
  774. rv.SetInt(decBigInt(data))
  775. return nil
  776. }
  777. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  778. }
  779. func marshalList(info *TypeInfo, value interface{}) ([]byte, error) {
  780. rv := reflect.ValueOf(value)
  781. t := rv.Type()
  782. k := t.Kind()
  783. switch k {
  784. case reflect.Slice, reflect.Array:
  785. if k == reflect.Slice && rv.IsNil() {
  786. return nil, nil
  787. }
  788. buf := &bytes.Buffer{}
  789. n := rv.Len()
  790. if n > math.MaxUint16 {
  791. return nil, marshalErrorf("marshal: slice / array too large")
  792. }
  793. buf.WriteByte(byte(n >> 8))
  794. buf.WriteByte(byte(n))
  795. for i := 0; i < n; i++ {
  796. item, err := Marshal(info.Elem, rv.Index(i).Interface())
  797. if err != nil {
  798. return nil, err
  799. }
  800. if len(item) > math.MaxUint16 {
  801. return nil, marshalErrorf("marshal: slice / array item too large")
  802. }
  803. buf.WriteByte(byte(len(item) >> 8))
  804. buf.WriteByte(byte(len(item)))
  805. buf.Write(item)
  806. }
  807. return buf.Bytes(), nil
  808. }
  809. if k == reflect.Map {
  810. elem := t.Elem()
  811. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  812. rkeys := rv.MapKeys()
  813. keys := make([]interface{}, len(rkeys))
  814. for i := 0; i < len(keys); i++ {
  815. keys[i] = rkeys[i].Interface()
  816. }
  817. return marshalList(info, keys)
  818. }
  819. }
  820. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  821. }
  822. func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
  823. rv := reflect.ValueOf(value)
  824. if rv.Kind() != reflect.Ptr {
  825. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  826. }
  827. rv = rv.Elem()
  828. t := rv.Type()
  829. k := t.Kind()
  830. switch k {
  831. case reflect.Slice, reflect.Array:
  832. if data == nil {
  833. if k == reflect.Array {
  834. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  835. }
  836. rv.Set(reflect.Zero(t))
  837. return nil
  838. }
  839. if len(data) < 2 {
  840. return unmarshalErrorf("unmarshal list: unexpected eof")
  841. }
  842. n := int(data[0])<<8 | int(data[1])
  843. data = data[2:]
  844. if k == reflect.Array {
  845. if rv.Len() != n {
  846. return unmarshalErrorf("unmarshal list: array with wrong size")
  847. }
  848. } else if rv.Cap() < n {
  849. rv.Set(reflect.MakeSlice(t, n, n))
  850. } else {
  851. rv.SetLen(n)
  852. }
  853. for i := 0; i < n; i++ {
  854. if len(data) < 2 {
  855. return unmarshalErrorf("unmarshal list: unexpected eof")
  856. }
  857. m := int(data[0])<<8 | int(data[1])
  858. data = data[2:]
  859. if err := Unmarshal(info.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  860. return err
  861. }
  862. data = data[m:]
  863. }
  864. return nil
  865. }
  866. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  867. }
  868. func marshalMap(info *TypeInfo, value interface{}) ([]byte, error) {
  869. rv := reflect.ValueOf(value)
  870. t := rv.Type()
  871. if t.Kind() != reflect.Map {
  872. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  873. }
  874. if rv.IsNil() {
  875. return nil, nil
  876. }
  877. buf := &bytes.Buffer{}
  878. n := rv.Len()
  879. if n > math.MaxUint16 {
  880. return nil, marshalErrorf("marshal: map too large")
  881. }
  882. buf.WriteByte(byte(n >> 8))
  883. buf.WriteByte(byte(n))
  884. keys := rv.MapKeys()
  885. for _, key := range keys {
  886. item, err := Marshal(info.Key, key.Interface())
  887. if err != nil {
  888. return nil, err
  889. }
  890. if len(item) > math.MaxUint16 {
  891. return nil, marshalErrorf("marshal: slice / array item too large")
  892. }
  893. buf.WriteByte(byte(len(item) >> 8))
  894. buf.WriteByte(byte(len(item)))
  895. buf.Write(item)
  896. item, err = Marshal(info.Elem, rv.MapIndex(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. }
  907. return buf.Bytes(), nil
  908. }
  909. func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
  910. rv := reflect.ValueOf(value)
  911. if rv.Kind() != reflect.Ptr {
  912. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  913. }
  914. rv = rv.Elem()
  915. t := rv.Type()
  916. if t.Kind() != reflect.Map {
  917. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  918. }
  919. if data == nil {
  920. rv.Set(reflect.Zero(t))
  921. return nil
  922. }
  923. rv.Set(reflect.MakeMap(t))
  924. if len(data) < 2 {
  925. return unmarshalErrorf("unmarshal map: unexpected eof")
  926. }
  927. n := int(data[1]) | int(data[0])<<8
  928. data = data[2:]
  929. for i := 0; i < n; i++ {
  930. if len(data) < 2 {
  931. return unmarshalErrorf("unmarshal list: unexpected eof")
  932. }
  933. m := int(data[1]) | int(data[0])<<8
  934. data = data[2:]
  935. key := reflect.New(t.Key())
  936. if err := Unmarshal(info.Key, data[:m], key.Interface()); err != nil {
  937. return err
  938. }
  939. data = data[m:]
  940. m = int(data[1]) | int(data[0])<<8
  941. data = data[2:]
  942. val := reflect.New(t.Elem())
  943. if err := Unmarshal(info.Elem, data[:m], val.Interface()); err != nil {
  944. return err
  945. }
  946. data = data[m:]
  947. rv.SetMapIndex(key.Elem(), val.Elem())
  948. }
  949. return nil
  950. }
  951. func marshalUUID(info *TypeInfo, value interface{}) ([]byte, error) {
  952. switch val := value.(type) {
  953. case UUID:
  954. return val.Bytes(), nil
  955. case []byte:
  956. if len(val) == 16 {
  957. return val, nil
  958. }
  959. case string:
  960. b, err := ParseUUID(val)
  961. if err != nil {
  962. return nil, err
  963. }
  964. return b[:], nil
  965. }
  966. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  967. }
  968. func unmarshalUUID(info *TypeInfo, data []byte, value interface{}) error {
  969. if data == nil || len(data) == 0 {
  970. switch v := value.(type) {
  971. case *string:
  972. *v = ""
  973. case *[]byte:
  974. *v = nil
  975. case *UUID:
  976. *v = UUID{}
  977. default:
  978. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  979. }
  980. return nil
  981. }
  982. u, err := UUIDFromBytes(data)
  983. if err != nil {
  984. return unmarshalErrorf("Unable to parse UUID: %s", err)
  985. }
  986. switch v := value.(type) {
  987. case *string:
  988. *v = u.String()
  989. return nil
  990. case *[]byte:
  991. *v = u[:]
  992. return nil
  993. case *UUID:
  994. *v = u
  995. return nil
  996. }
  997. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  998. }
  999. func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
  1000. switch v := value.(type) {
  1001. case Unmarshaler:
  1002. return v.UnmarshalCQL(info, data)
  1003. case *time.Time:
  1004. id, err := UUIDFromBytes(data)
  1005. if err != nil {
  1006. return err
  1007. } else if id.Version() != 1 {
  1008. return unmarshalErrorf("invalid timeuuid")
  1009. }
  1010. *v = id.Time()
  1011. return nil
  1012. default:
  1013. return unmarshalUUID(info, data, value)
  1014. }
  1015. }
  1016. func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {
  1017. switch v := value.(type) {
  1018. case Unmarshaler:
  1019. return v.UnmarshalCQL(info, data)
  1020. case *string:
  1021. if len(data) == 0 {
  1022. *v = ""
  1023. return nil
  1024. }
  1025. if len(data) == 4 {
  1026. *v = fmt.Sprintf("%d.%d.%d.%d", data[0], data[1], data[2], data[3])
  1027. return nil
  1028. }
  1029. // TODO: support IPv6
  1030. }
  1031. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1032. }
  1033. // TypeInfo describes a Cassandra specific data type.
  1034. type TypeInfo struct {
  1035. Type Type
  1036. Key *TypeInfo // only used for TypeMap
  1037. Elem *TypeInfo // only used for TypeMap, TypeList and TypeSet
  1038. Custom string // only used for TypeCostum
  1039. }
  1040. // String returns a human readable name for the Cassandra datatype
  1041. // described by t.
  1042. func (t TypeInfo) String() string {
  1043. switch t.Type {
  1044. case TypeMap:
  1045. return fmt.Sprintf("%s(%s, %s)", t.Type, t.Key, t.Elem)
  1046. case TypeList, TypeSet:
  1047. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  1048. case TypeCustom:
  1049. return fmt.Sprintf("%s(%s)", t.Type, t.Elem)
  1050. }
  1051. return t.Type.String()
  1052. }
  1053. // Type is the identifier of a Cassandra internal datatype.
  1054. type Type int
  1055. const (
  1056. TypeCustom Type = 0x0000
  1057. TypeAscii Type = 0x0001
  1058. TypeBigInt Type = 0x0002
  1059. TypeBlob Type = 0x0003
  1060. TypeBoolean Type = 0x0004
  1061. TypeCounter Type = 0x0005
  1062. TypeDecimal Type = 0x0006
  1063. TypeDouble Type = 0x0007
  1064. TypeFloat Type = 0x0008
  1065. TypeInt Type = 0x0009
  1066. TypeTimestamp Type = 0x000B
  1067. TypeUUID Type = 0x000C
  1068. TypeVarchar Type = 0x000D
  1069. TypeVarint Type = 0x000E
  1070. TypeTimeUUID Type = 0x000F
  1071. TypeInet Type = 0x0010
  1072. TypeList Type = 0x0020
  1073. TypeMap Type = 0x0021
  1074. TypeSet Type = 0x0022
  1075. )
  1076. // String returns the name of the identifier.
  1077. func (t Type) String() string {
  1078. switch t {
  1079. case TypeCustom:
  1080. return "custom"
  1081. case TypeAscii:
  1082. return "ascii"
  1083. case TypeBigInt:
  1084. return "bigint"
  1085. case TypeBlob:
  1086. return "blob"
  1087. case TypeBoolean:
  1088. return "boolean"
  1089. case TypeCounter:
  1090. return "counter"
  1091. case TypeDecimal:
  1092. return "decimal"
  1093. case TypeDouble:
  1094. return "double"
  1095. case TypeFloat:
  1096. return "float"
  1097. case TypeInt:
  1098. return "int"
  1099. case TypeTimestamp:
  1100. return "timestamp"
  1101. case TypeUUID:
  1102. return "uuid"
  1103. case TypeVarchar:
  1104. return "varchar"
  1105. case TypeTimeUUID:
  1106. return "timeuuid"
  1107. case TypeInet:
  1108. return "inet"
  1109. case TypeList:
  1110. return "list"
  1111. case TypeMap:
  1112. return "map"
  1113. case TypeSet:
  1114. return "set"
  1115. default:
  1116. return "unknown"
  1117. }
  1118. }
  1119. type MarshalError string
  1120. func (m MarshalError) Error() string {
  1121. return string(m)
  1122. }
  1123. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1124. return MarshalError(fmt.Sprintf(format, args...))
  1125. }
  1126. type UnmarshalError string
  1127. func (m UnmarshalError) Error() string {
  1128. return string(m)
  1129. }
  1130. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1131. return UnmarshalError(fmt.Sprintf(format, args...))
  1132. }