marshal.go 30 KB

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