marshal.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. "strconv"
  14. "time"
  15. "speter.net/go/exp/math/dec/inf"
  16. )
  17. var (
  18. bigOne = big.NewInt(1)
  19. )
  20. // Marshaler is the interface implemented by objects that can marshal
  21. // themselves into values understood by Cassandra.
  22. type Marshaler interface {
  23. MarshalCQL(info TypeInfo) ([]byte, error)
  24. }
  25. // Unmarshaler is the interface implemented by objects that can unmarshal
  26. // a Cassandra specific description of themselves.
  27. type Unmarshaler interface {
  28. UnmarshalCQL(info TypeInfo, data []byte) error
  29. }
  30. // Marshal returns the CQL encoding of the value for the Cassandra
  31. // internal type described by the info parameter.
  32. func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
  33. if value == nil {
  34. return nil, nil
  35. }
  36. if info.Version() < protoVersion1 {
  37. panic("protocol version not set")
  38. }
  39. if v, ok := value.(Marshaler); ok {
  40. return v.MarshalCQL(info)
  41. }
  42. if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr {
  43. if valueRef.IsNil() {
  44. return nil, nil
  45. } else {
  46. return Marshal(info, valueRef.Elem().Interface())
  47. }
  48. }
  49. switch info.Type() {
  50. case TypeVarchar, TypeAscii, TypeBlob:
  51. return marshalVarchar(info, value)
  52. case TypeBoolean:
  53. return marshalBool(info, value)
  54. case TypeInt:
  55. return marshalInt(info, value)
  56. case TypeBigInt, TypeCounter:
  57. return marshalBigInt(info, value)
  58. case TypeFloat:
  59. return marshalFloat(info, value)
  60. case TypeDouble:
  61. return marshalDouble(info, value)
  62. case TypeDecimal:
  63. return marshalDecimal(info, value)
  64. case TypeTimestamp:
  65. return marshalTimestamp(info, value)
  66. case TypeList, TypeSet:
  67. return marshalList(info, value)
  68. case TypeMap:
  69. return marshalMap(info, value)
  70. case TypeUUID, TypeTimeUUID:
  71. return marshalUUID(info, value)
  72. case TypeVarint:
  73. return marshalVarint(info, value)
  74. case TypeInet:
  75. return marshalInet(info, value)
  76. case TypeUDT:
  77. return marshalUDT(info, value)
  78. }
  79. // TODO(tux21b): add the remaining types
  80. return nil, fmt.Errorf("can not marshal %T into %s", value, info)
  81. }
  82. // Unmarshal parses the CQL encoded data based on the info parameter that
  83. // describes the Cassandra internal data type and stores the result in the
  84. // value pointed by value.
  85. func Unmarshal(info TypeInfo, data []byte, value interface{}) error {
  86. if v, ok := value.(Unmarshaler); ok {
  87. return v.UnmarshalCQL(info, data)
  88. }
  89. if isNullableValue(value) {
  90. return unmarshalNullable(info, data, value)
  91. }
  92. switch info.Type() {
  93. case TypeVarchar, TypeAscii, TypeBlob:
  94. return unmarshalVarchar(info, data, value)
  95. case TypeBoolean:
  96. return unmarshalBool(info, data, value)
  97. case TypeInt:
  98. return unmarshalInt(info, data, value)
  99. case TypeBigInt, TypeCounter:
  100. return unmarshalBigInt(info, data, value)
  101. case TypeVarint:
  102. return unmarshalVarint(info, data, value)
  103. case TypeFloat:
  104. return unmarshalFloat(info, data, value)
  105. case TypeDouble:
  106. return unmarshalDouble(info, data, value)
  107. case TypeDecimal:
  108. return unmarshalDecimal(info, data, value)
  109. case TypeTimestamp:
  110. return unmarshalTimestamp(info, data, value)
  111. case TypeList, TypeSet:
  112. return unmarshalList(info, data, value)
  113. case TypeMap:
  114. return unmarshalMap(info, data, value)
  115. case TypeTimeUUID:
  116. return unmarshalTimeUUID(info, data, value)
  117. case TypeUUID:
  118. return unmarshalUUID(info, data, value)
  119. case TypeInet:
  120. return unmarshalInet(info, data, value)
  121. case TypeTuple:
  122. return unmarshalTuple(info, data, value)
  123. case TypeUDT:
  124. return unmarshalUDT(info, data, value)
  125. }
  126. // TODO(tux21b): add the remaining types
  127. return fmt.Errorf("can not unmarshal %s into %T", info, value)
  128. }
  129. func isNullableValue(value interface{}) bool {
  130. v := reflect.ValueOf(value)
  131. return v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Ptr
  132. }
  133. func isNullData(info TypeInfo, data []byte) bool {
  134. return len(data) == 0
  135. }
  136. func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error {
  137. valueRef := reflect.ValueOf(value)
  138. if isNullData(info, data) {
  139. nilValue := reflect.Zero(valueRef.Type().Elem())
  140. valueRef.Elem().Set(nilValue)
  141. return nil
  142. }
  143. newValue := reflect.New(valueRef.Type().Elem().Elem())
  144. valueRef.Elem().Set(newValue)
  145. return Unmarshal(info, data, newValue.Interface())
  146. }
  147. func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) {
  148. switch v := value.(type) {
  149. case Marshaler:
  150. return v.MarshalCQL(info)
  151. case string:
  152. return []byte(v), nil
  153. case []byte:
  154. return v, nil
  155. }
  156. rv := reflect.ValueOf(value)
  157. t := rv.Type()
  158. k := t.Kind()
  159. switch {
  160. case k == reflect.String:
  161. return []byte(rv.String()), nil
  162. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  163. return rv.Bytes(), nil
  164. }
  165. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  166. }
  167. func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
  168. switch v := value.(type) {
  169. case Unmarshaler:
  170. return v.UnmarshalCQL(info, data)
  171. case *string:
  172. *v = string(data)
  173. return nil
  174. case *[]byte:
  175. var dataCopy []byte
  176. if data != nil {
  177. dataCopy = make([]byte, len(data))
  178. copy(dataCopy, data)
  179. }
  180. *v = dataCopy
  181. return nil
  182. }
  183. rv := reflect.ValueOf(value)
  184. if rv.Kind() != reflect.Ptr {
  185. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  186. }
  187. rv = rv.Elem()
  188. t := rv.Type()
  189. k := t.Kind()
  190. switch {
  191. case k == reflect.String:
  192. rv.SetString(string(data))
  193. return nil
  194. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  195. var dataCopy []byte
  196. if data != nil {
  197. dataCopy = make([]byte, len(data))
  198. copy(dataCopy, data)
  199. }
  200. rv.SetBytes(dataCopy)
  201. return nil
  202. }
  203. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  204. }
  205. func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
  206. switch v := value.(type) {
  207. case Marshaler:
  208. return v.MarshalCQL(info)
  209. case int:
  210. if v > math.MaxInt32 || v < math.MinInt32 {
  211. return nil, marshalErrorf("marshal int: value %d out of range", v)
  212. }
  213. return encInt(int32(v)), nil
  214. case uint:
  215. if v > math.MaxInt32 {
  216. return nil, marshalErrorf("marshal int: value %d out of range", v)
  217. }
  218. return encInt(int32(v)), nil
  219. case int64:
  220. if v > math.MaxInt32 || v < math.MinInt32 {
  221. return nil, marshalErrorf("marshal int: value %d out of range", v)
  222. }
  223. return encInt(int32(v)), nil
  224. case uint64:
  225. if v > math.MaxInt32 {
  226. return nil, marshalErrorf("marshal int: value %d out of range", v)
  227. }
  228. return encInt(int32(v)), nil
  229. case int32:
  230. return encInt(v), nil
  231. case uint32:
  232. if v > math.MaxInt32 {
  233. return nil, marshalErrorf("marshal int: value %d out of range", v)
  234. }
  235. return encInt(int32(v)), nil
  236. case int16:
  237. return encInt(int32(v)), nil
  238. case uint16:
  239. return encInt(int32(v)), nil
  240. case int8:
  241. return encInt(int32(v)), nil
  242. case uint8:
  243. return encInt(int32(v)), nil
  244. case string:
  245. i, err := strconv.ParseInt(value.(string), 10, 32)
  246. if err != nil {
  247. return nil, marshalErrorf("can not marshal string to int: %s", err)
  248. }
  249. return encInt(int32(i)), nil
  250. }
  251. rv := reflect.ValueOf(value)
  252. switch rv.Type().Kind() {
  253. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  254. v := rv.Int()
  255. if v > math.MaxInt32 || v < math.MinInt32 {
  256. return nil, marshalErrorf("marshal int: value %d out of range", v)
  257. }
  258. return encInt(int32(v)), nil
  259. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  260. v := rv.Uint()
  261. if v > math.MaxInt32 {
  262. return nil, marshalErrorf("marshal int: value %d out of range", v)
  263. }
  264. return encInt(int32(v)), nil
  265. }
  266. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  267. }
  268. func encInt(x int32) []byte {
  269. return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  270. }
  271. func decInt(x []byte) int32 {
  272. if len(x) != 4 {
  273. return 0
  274. }
  275. return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3])
  276. }
  277. func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) {
  278. switch v := value.(type) {
  279. case Marshaler:
  280. return v.MarshalCQL(info)
  281. case int:
  282. return encBigInt(int64(v)), nil
  283. case uint:
  284. if uint64(v) > math.MaxInt64 {
  285. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  286. }
  287. return encBigInt(int64(v)), nil
  288. case int64:
  289. return encBigInt(v), nil
  290. case uint64:
  291. if v > math.MaxInt64 {
  292. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  293. }
  294. return encBigInt(int64(v)), nil
  295. case int32:
  296. return encBigInt(int64(v)), nil
  297. case uint32:
  298. return encBigInt(int64(v)), nil
  299. case int16:
  300. return encBigInt(int64(v)), nil
  301. case uint16:
  302. return encBigInt(int64(v)), nil
  303. case int8:
  304. return encBigInt(int64(v)), nil
  305. case uint8:
  306. return encBigInt(int64(v)), nil
  307. case big.Int:
  308. return encBigInt2C(&v), nil
  309. case string:
  310. i, err := strconv.ParseInt(value.(string), 10, 64)
  311. if err != nil {
  312. return nil, marshalErrorf("can not marshal string to bigint: %s", err)
  313. }
  314. return encBigInt(i), nil
  315. }
  316. rv := reflect.ValueOf(value)
  317. switch rv.Type().Kind() {
  318. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  319. v := rv.Int()
  320. return encBigInt(v), nil
  321. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  322. v := rv.Uint()
  323. if v > math.MaxInt64 {
  324. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  325. }
  326. return encBigInt(int64(v)), nil
  327. }
  328. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  329. }
  330. func encBigInt(x int64) []byte {
  331. return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32),
  332. byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  333. }
  334. func bytesToInt64(data []byte) (ret int64) {
  335. for i := range data {
  336. ret |= int64(data[i]) << (8 * uint(len(data)-i-1))
  337. }
  338. return ret
  339. }
  340. func unmarshalBigInt(info TypeInfo, data []byte, value interface{}) error {
  341. return unmarshalIntlike(info, decBigInt(data), data, value)
  342. }
  343. func unmarshalInt(info TypeInfo, data []byte, value interface{}) error {
  344. return unmarshalIntlike(info, int64(decInt(data)), data, value)
  345. }
  346. func unmarshalVarint(info TypeInfo, data []byte, value interface{}) error {
  347. switch value.(type) {
  348. case *big.Int:
  349. return unmarshalIntlike(info, 0, data, value)
  350. }
  351. if len(data) > 8 {
  352. return unmarshalErrorf("unmarshal int: varint value %v out of range for %T (use big.Int)", data, value)
  353. }
  354. int64Val := bytesToInt64(data)
  355. if len(data) < 8 && data[0]&0x80 > 0 {
  356. int64Val -= (1 << uint(len(data)*8))
  357. }
  358. return unmarshalIntlike(info, int64Val, data, value)
  359. }
  360. func marshalVarint(info TypeInfo, value interface{}) ([]byte, error) {
  361. var (
  362. retBytes []byte
  363. err error
  364. )
  365. switch v := value.(type) {
  366. case uint64:
  367. if v > uint64(math.MaxInt64) {
  368. retBytes = make([]byte, 9)
  369. binary.BigEndian.PutUint64(retBytes[1:], v)
  370. } else {
  371. retBytes = make([]byte, 8)
  372. binary.BigEndian.PutUint64(retBytes, v)
  373. }
  374. default:
  375. retBytes, err = marshalBigInt(info, value)
  376. }
  377. if err == nil {
  378. // trim down to most significant byte
  379. i := 0
  380. for ; i < len(retBytes)-1; i++ {
  381. b0 := retBytes[i]
  382. if b0 != 0 && b0 != 0xFF {
  383. break
  384. }
  385. b1 := retBytes[i+1]
  386. if b0 == 0 && b1 != 0 {
  387. if b1&0x80 == 0 {
  388. i++
  389. }
  390. break
  391. }
  392. if b0 == 0xFF && b1 != 0xFF {
  393. if b1&0x80 > 0 {
  394. i++
  395. }
  396. break
  397. }
  398. }
  399. retBytes = retBytes[i:]
  400. }
  401. return retBytes, err
  402. }
  403. func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interface{}) error {
  404. switch v := value.(type) {
  405. case *int:
  406. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  407. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  408. }
  409. *v = int(int64Val)
  410. return nil
  411. case *uint:
  412. if int64Val < 0 || (^uint(0) == math.MaxUint32 && int64Val > math.MaxUint32) {
  413. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  414. }
  415. *v = uint(int64Val)
  416. return nil
  417. case *int64:
  418. *v = int64Val
  419. return nil
  420. case *uint64:
  421. if int64Val < 0 {
  422. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  423. }
  424. *v = uint64(int64Val)
  425. return nil
  426. case *int32:
  427. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  428. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  429. }
  430. *v = int32(int64Val)
  431. return nil
  432. case *uint32:
  433. if int64Val < 0 || int64Val > math.MaxUint32 {
  434. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  435. }
  436. *v = uint32(int64Val)
  437. return nil
  438. case *int16:
  439. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  440. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  441. }
  442. *v = int16(int64Val)
  443. return nil
  444. case *uint16:
  445. if int64Val < 0 || int64Val > math.MaxUint16 {
  446. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  447. }
  448. *v = uint16(int64Val)
  449. return nil
  450. case *int8:
  451. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  452. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  453. }
  454. *v = int8(int64Val)
  455. return nil
  456. case *uint8:
  457. if int64Val < 0 || int64Val > math.MaxUint8 {
  458. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  459. }
  460. *v = uint8(int64Val)
  461. return nil
  462. case *big.Int:
  463. decBigInt2C(data, v)
  464. return nil
  465. case *string:
  466. *v = strconv.FormatInt(int64Val, 10)
  467. return nil
  468. }
  469. rv := reflect.ValueOf(value)
  470. if rv.Kind() != reflect.Ptr {
  471. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  472. }
  473. rv = rv.Elem()
  474. switch rv.Type().Kind() {
  475. case reflect.Int:
  476. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  477. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  478. }
  479. rv.SetInt(int64Val)
  480. return nil
  481. case reflect.Int64:
  482. rv.SetInt(int64Val)
  483. return nil
  484. case reflect.Int32:
  485. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  486. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  487. }
  488. rv.SetInt(int64Val)
  489. return nil
  490. case reflect.Int16:
  491. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  492. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  493. }
  494. rv.SetInt(int64Val)
  495. return nil
  496. case reflect.Int8:
  497. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  498. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  499. }
  500. rv.SetInt(int64Val)
  501. return nil
  502. case reflect.Uint:
  503. if int64Val < 0 || (^uint(0) == math.MaxUint32 && int64Val > math.MaxUint32) {
  504. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  505. }
  506. rv.SetUint(uint64(int64Val))
  507. return nil
  508. case reflect.Uint64:
  509. if int64Val < 0 {
  510. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  511. }
  512. rv.SetUint(uint64(int64Val))
  513. return nil
  514. case reflect.Uint32:
  515. if int64Val < 0 || int64Val > math.MaxUint32 {
  516. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  517. }
  518. rv.SetUint(uint64(int64Val))
  519. return nil
  520. case reflect.Uint16:
  521. if int64Val < 0 || int64Val > math.MaxUint16 {
  522. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  523. }
  524. rv.SetUint(uint64(int64Val))
  525. return nil
  526. case reflect.Uint8:
  527. if int64Val < 0 || int64Val > math.MaxUint8 {
  528. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  529. }
  530. rv.SetUint(uint64(int64Val))
  531. return nil
  532. }
  533. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  534. }
  535. func decBigInt(data []byte) int64 {
  536. if len(data) != 8 {
  537. return 0
  538. }
  539. return int64(data[0])<<56 | int64(data[1])<<48 |
  540. int64(data[2])<<40 | int64(data[3])<<32 |
  541. int64(data[4])<<24 | int64(data[5])<<16 |
  542. int64(data[6])<<8 | int64(data[7])
  543. }
  544. func marshalBool(info TypeInfo, value interface{}) ([]byte, error) {
  545. switch v := value.(type) {
  546. case Marshaler:
  547. return v.MarshalCQL(info)
  548. case bool:
  549. return encBool(v), nil
  550. }
  551. rv := reflect.ValueOf(value)
  552. switch rv.Type().Kind() {
  553. case reflect.Bool:
  554. return encBool(rv.Bool()), nil
  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. }
  602. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  603. }
  604. func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error {
  605. switch v := value.(type) {
  606. case Unmarshaler:
  607. return v.UnmarshalCQL(info, data)
  608. case *float32:
  609. *v = math.Float32frombits(uint32(decInt(data)))
  610. return nil
  611. }
  612. rv := reflect.ValueOf(value)
  613. if rv.Kind() != reflect.Ptr {
  614. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  615. }
  616. rv = rv.Elem()
  617. switch rv.Type().Kind() {
  618. case reflect.Float32:
  619. rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data)))))
  620. return nil
  621. }
  622. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  623. }
  624. func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) {
  625. switch v := value.(type) {
  626. case Marshaler:
  627. return v.MarshalCQL(info)
  628. case float64:
  629. return encBigInt(int64(math.Float64bits(v))), nil
  630. }
  631. rv := reflect.ValueOf(value)
  632. switch rv.Type().Kind() {
  633. case reflect.Float64:
  634. return encBigInt(int64(math.Float64bits(rv.Float()))), nil
  635. }
  636. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  637. }
  638. func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error {
  639. switch v := value.(type) {
  640. case Unmarshaler:
  641. return v.UnmarshalCQL(info, data)
  642. case *float64:
  643. *v = math.Float64frombits(uint64(decBigInt(data)))
  644. return nil
  645. }
  646. rv := reflect.ValueOf(value)
  647. if rv.Kind() != reflect.Ptr {
  648. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  649. }
  650. rv = rv.Elem()
  651. switch rv.Type().Kind() {
  652. case reflect.Float64:
  653. rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
  654. return nil
  655. }
  656. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  657. }
  658. func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) {
  659. switch v := value.(type) {
  660. case Marshaler:
  661. return v.MarshalCQL(info)
  662. case inf.Dec:
  663. unscaled := encBigInt2C(v.UnscaledBig())
  664. if unscaled == nil {
  665. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  666. }
  667. buf := make([]byte, 4+len(unscaled))
  668. copy(buf[0:4], encInt(int32(v.Scale())))
  669. copy(buf[4:], unscaled)
  670. return buf, nil
  671. }
  672. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  673. }
  674. func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
  675. switch v := value.(type) {
  676. case Unmarshaler:
  677. return v.UnmarshalCQL(info, data)
  678. case *inf.Dec:
  679. scale := decInt(data[0:4])
  680. unscaled := decBigInt2C(data[4:], nil)
  681. *v = *inf.NewDecBig(unscaled, inf.Scale(scale))
  682. return nil
  683. }
  684. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  685. }
  686. // decBigInt2C sets the value of n to the big-endian two's complement
  687. // value stored in the given data. If data[0]&80 != 0, the number
  688. // is negative. If data is empty, the result will be 0.
  689. func decBigInt2C(data []byte, n *big.Int) *big.Int {
  690. if n == nil {
  691. n = new(big.Int)
  692. }
  693. n.SetBytes(data)
  694. if len(data) > 0 && data[0]&0x80 > 0 {
  695. n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8))
  696. }
  697. return n
  698. }
  699. // encBigInt2C returns the big-endian two's complement
  700. // form of n.
  701. func encBigInt2C(n *big.Int) []byte {
  702. switch n.Sign() {
  703. case 0:
  704. return []byte{0}
  705. case 1:
  706. b := n.Bytes()
  707. if b[0]&0x80 > 0 {
  708. b = append([]byte{0}, b...)
  709. }
  710. return b
  711. case -1:
  712. length := uint(n.BitLen()/8+1) * 8
  713. b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes()
  714. // When the most significant bit is on a byte
  715. // boundary, we can get some extra significant
  716. // bits, so strip them off when that happens.
  717. if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
  718. b = b[1:]
  719. }
  720. return b
  721. }
  722. return nil
  723. }
  724. func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) {
  725. switch v := value.(type) {
  726. case Marshaler:
  727. return v.MarshalCQL(info)
  728. case int64:
  729. return encBigInt(v), nil
  730. case time.Time:
  731. x := v.UnixNano() / int64(1000000)
  732. return encBigInt(x), nil
  733. }
  734. rv := reflect.ValueOf(value)
  735. switch rv.Type().Kind() {
  736. case reflect.Int64:
  737. return encBigInt(rv.Int()), nil
  738. }
  739. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  740. }
  741. func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error {
  742. switch v := value.(type) {
  743. case Unmarshaler:
  744. return v.UnmarshalCQL(info, data)
  745. case *int64:
  746. *v = decBigInt(data)
  747. return nil
  748. case *time.Time:
  749. if len(data) == 0 {
  750. return nil
  751. }
  752. x := decBigInt(data)
  753. sec := x / 1000
  754. nsec := (x - sec*1000) * 1000000
  755. *v = time.Unix(sec, nsec).In(time.UTC)
  756. return nil
  757. }
  758. rv := reflect.ValueOf(value)
  759. if rv.Kind() != reflect.Ptr {
  760. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  761. }
  762. rv = rv.Elem()
  763. switch rv.Type().Kind() {
  764. case reflect.Int64:
  765. rv.SetInt(decBigInt(data))
  766. return nil
  767. }
  768. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  769. }
  770. func writeCollectionSize(info CollectionType, n int, buf *bytes.Buffer) error {
  771. if info.proto > protoVersion2 {
  772. if n > math.MaxInt32 {
  773. return marshalErrorf("marshal: collection too large")
  774. }
  775. buf.WriteByte(byte(n >> 24))
  776. buf.WriteByte(byte(n >> 16))
  777. buf.WriteByte(byte(n >> 8))
  778. buf.WriteByte(byte(n))
  779. } else {
  780. if n > math.MaxUint16 {
  781. return marshalErrorf("marshal: collection too large")
  782. }
  783. buf.WriteByte(byte(n >> 8))
  784. buf.WriteByte(byte(n))
  785. }
  786. return nil
  787. }
  788. func marshalList(info TypeInfo, value interface{}) ([]byte, error) {
  789. listInfo, ok := info.(CollectionType)
  790. if !ok {
  791. return nil, marshalErrorf("marshal: can not marshal non collection type into list")
  792. }
  793. rv := reflect.ValueOf(value)
  794. t := rv.Type()
  795. k := t.Kind()
  796. switch k {
  797. case reflect.Slice, reflect.Array:
  798. if k == reflect.Slice && rv.IsNil() {
  799. return nil, nil
  800. }
  801. buf := &bytes.Buffer{}
  802. n := rv.Len()
  803. if err := writeCollectionSize(listInfo, n, buf); err != nil {
  804. return nil, err
  805. }
  806. for i := 0; i < n; i++ {
  807. item, err := Marshal(listInfo.Elem, rv.Index(i).Interface())
  808. if err != nil {
  809. return nil, err
  810. }
  811. if err := writeCollectionSize(listInfo, len(item), buf); err != nil {
  812. return nil, err
  813. }
  814. buf.Write(item)
  815. }
  816. return buf.Bytes(), nil
  817. case reflect.Map:
  818. elem := t.Elem()
  819. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  820. rkeys := rv.MapKeys()
  821. keys := make([]interface{}, len(rkeys))
  822. for i := 0; i < len(keys); i++ {
  823. keys[i] = rkeys[i].Interface()
  824. }
  825. return marshalList(listInfo, keys)
  826. }
  827. }
  828. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  829. }
  830. func readCollectionSize(info CollectionType, data []byte) (size, read int) {
  831. if info.proto > protoVersion2 {
  832. size = int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  833. read = 4
  834. } else {
  835. size = int(data[0])<<8 | int(data[1])
  836. read = 2
  837. }
  838. return
  839. }
  840. func unmarshalList(info TypeInfo, data []byte, value interface{}) error {
  841. listInfo, ok := info.(CollectionType)
  842. if !ok {
  843. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into list")
  844. }
  845. rv := reflect.ValueOf(value)
  846. if rv.Kind() != reflect.Ptr {
  847. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  848. }
  849. rv = rv.Elem()
  850. t := rv.Type()
  851. k := t.Kind()
  852. switch k {
  853. case reflect.Slice, reflect.Array:
  854. if data == nil {
  855. if k == reflect.Array {
  856. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  857. }
  858. rv.Set(reflect.Zero(t))
  859. return nil
  860. }
  861. if len(data) < 2 {
  862. return unmarshalErrorf("unmarshal list: unexpected eof")
  863. }
  864. n, p := readCollectionSize(listInfo, data)
  865. data = data[p:]
  866. if k == reflect.Array {
  867. if rv.Len() != n {
  868. return unmarshalErrorf("unmarshal list: array with wrong size")
  869. }
  870. } else if rv.Cap() < n {
  871. rv.Set(reflect.MakeSlice(t, n, n))
  872. } else {
  873. rv.SetLen(n)
  874. }
  875. for i := 0; i < n; i++ {
  876. if len(data) < 2 {
  877. return unmarshalErrorf("unmarshal list: unexpected eof")
  878. }
  879. m, p := readCollectionSize(listInfo, data)
  880. data = data[p:]
  881. if err := Unmarshal(listInfo.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  882. return err
  883. }
  884. data = data[m:]
  885. }
  886. return nil
  887. }
  888. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  889. }
  890. func marshalMap(info TypeInfo, value interface{}) ([]byte, error) {
  891. mapInfo, ok := info.(CollectionType)
  892. if !ok {
  893. return nil, marshalErrorf("marshal: can not marshal none collection type into map")
  894. }
  895. rv := reflect.ValueOf(value)
  896. t := rv.Type()
  897. if t.Kind() != reflect.Map {
  898. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  899. }
  900. if rv.IsNil() {
  901. return nil, nil
  902. }
  903. buf := &bytes.Buffer{}
  904. n := rv.Len()
  905. if err := writeCollectionSize(mapInfo, n, buf); err != nil {
  906. return nil, err
  907. }
  908. keys := rv.MapKeys()
  909. for _, key := range keys {
  910. item, err := Marshal(mapInfo.Key, key.Interface())
  911. if err != nil {
  912. return nil, err
  913. }
  914. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  915. return nil, err
  916. }
  917. buf.Write(item)
  918. item, err = Marshal(mapInfo.Elem, rv.MapIndex(key).Interface())
  919. if err != nil {
  920. return nil, err
  921. }
  922. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  923. return nil, err
  924. }
  925. buf.Write(item)
  926. }
  927. return buf.Bytes(), nil
  928. }
  929. func unmarshalMap(info TypeInfo, data []byte, value interface{}) error {
  930. mapInfo, ok := info.(CollectionType)
  931. if !ok {
  932. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into map")
  933. }
  934. rv := reflect.ValueOf(value)
  935. if rv.Kind() != reflect.Ptr {
  936. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  937. }
  938. rv = rv.Elem()
  939. t := rv.Type()
  940. if t.Kind() != reflect.Map {
  941. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  942. }
  943. if data == nil {
  944. rv.Set(reflect.Zero(t))
  945. return nil
  946. }
  947. rv.Set(reflect.MakeMap(t))
  948. if len(data) < 2 {
  949. return unmarshalErrorf("unmarshal map: unexpected eof")
  950. }
  951. n, p := readCollectionSize(mapInfo, data)
  952. data = data[p:]
  953. for i := 0; i < n; i++ {
  954. if len(data) < 2 {
  955. return unmarshalErrorf("unmarshal list: unexpected eof")
  956. }
  957. m, p := readCollectionSize(mapInfo, data)
  958. data = data[p:]
  959. key := reflect.New(t.Key())
  960. if err := Unmarshal(mapInfo.Key, data[:m], key.Interface()); err != nil {
  961. return err
  962. }
  963. data = data[m:]
  964. m, p = readCollectionSize(mapInfo, data)
  965. data = data[p:]
  966. val := reflect.New(t.Elem())
  967. if err := Unmarshal(mapInfo.Elem, data[:m], val.Interface()); err != nil {
  968. return err
  969. }
  970. data = data[m:]
  971. rv.SetMapIndex(key.Elem(), val.Elem())
  972. }
  973. return nil
  974. }
  975. func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) {
  976. switch val := value.(type) {
  977. case UUID:
  978. return val.Bytes(), nil
  979. case []byte:
  980. if len(val) == 16 {
  981. return val, nil
  982. }
  983. case string:
  984. b, err := ParseUUID(val)
  985. if err != nil {
  986. return nil, err
  987. }
  988. return b[:], nil
  989. }
  990. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  991. }
  992. func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
  993. if data == nil || len(data) == 0 {
  994. switch v := value.(type) {
  995. case *string:
  996. *v = ""
  997. case *[]byte:
  998. *v = nil
  999. case *UUID:
  1000. *v = UUID{}
  1001. default:
  1002. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1003. }
  1004. return nil
  1005. }
  1006. u, err := UUIDFromBytes(data)
  1007. if err != nil {
  1008. return unmarshalErrorf("Unable to parse UUID: %s", err)
  1009. }
  1010. switch v := value.(type) {
  1011. case *string:
  1012. *v = u.String()
  1013. return nil
  1014. case *[]byte:
  1015. *v = u[:]
  1016. return nil
  1017. case *UUID:
  1018. *v = u
  1019. return nil
  1020. }
  1021. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1022. }
  1023. func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error {
  1024. switch v := value.(type) {
  1025. case Unmarshaler:
  1026. return v.UnmarshalCQL(info, data)
  1027. case *time.Time:
  1028. id, err := UUIDFromBytes(data)
  1029. if err != nil {
  1030. return err
  1031. } else if id.Version() != 1 {
  1032. return unmarshalErrorf("invalid timeuuid")
  1033. }
  1034. *v = id.Time()
  1035. return nil
  1036. default:
  1037. return unmarshalUUID(info, data, value)
  1038. }
  1039. }
  1040. func marshalInet(info TypeInfo, value interface{}) ([]byte, error) {
  1041. // we return either the 4 or 16 byte representation of an
  1042. // ip address here otherwise the db value will be prefixed
  1043. // with the remaining byte values e.g. ::ffff:127.0.0.1 and not 127.0.0.1
  1044. switch val := value.(type) {
  1045. case net.IP:
  1046. t := val.To4()
  1047. if t == nil {
  1048. return val.To16(), nil
  1049. }
  1050. return t, nil
  1051. case string:
  1052. b := net.ParseIP(val)
  1053. if b != nil {
  1054. t := b.To4()
  1055. if t == nil {
  1056. return b.To16(), nil
  1057. }
  1058. return t, nil
  1059. }
  1060. return nil, marshalErrorf("cannot marshal. invalid ip string %s", val)
  1061. }
  1062. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1063. }
  1064. func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
  1065. switch v := value.(type) {
  1066. case Unmarshaler:
  1067. return v.UnmarshalCQL(info, data)
  1068. case *net.IP:
  1069. ip := net.IP(data)
  1070. if v4 := ip.To4(); v4 != nil {
  1071. *v = v4
  1072. return nil
  1073. }
  1074. *v = ip
  1075. return nil
  1076. case *string:
  1077. if len(data) == 0 {
  1078. *v = ""
  1079. return nil
  1080. }
  1081. ip := net.IP(data)
  1082. if v4 := ip.To4(); v4 != nil {
  1083. *v = v4.String()
  1084. return nil
  1085. }
  1086. *v = ip.String()
  1087. return nil
  1088. }
  1089. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1090. }
  1091. // currently only support unmarshal into a list of values, this makes it possible
  1092. // to support tuples without changing the query API. In the future this can be extend
  1093. // to allow unmarshalling into custom tuple types.
  1094. func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
  1095. if v, ok := value.(Unmarshaler); ok {
  1096. return v.UnmarshalCQL(info, data)
  1097. }
  1098. tuple := info.(TupleTypeInfo)
  1099. switch v := value.(type) {
  1100. case []interface{}:
  1101. for i, elem := range tuple.Elems {
  1102. // each element inside data is a [bytes]
  1103. size := readInt(data)
  1104. data = data[4:]
  1105. err := Unmarshal(elem, data[:size], v[i])
  1106. if err != nil {
  1107. return err
  1108. }
  1109. data = data[size:]
  1110. }
  1111. return nil
  1112. }
  1113. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1114. }
  1115. // UDTMarshaler is an interface which should be implemented by users wishing to
  1116. // handle encoding UDT types to sent to Cassandra. Note: due to current implentations
  1117. // methods defined for this interface must be value receivers not pointer receivers.
  1118. type UDTMarshaler interface {
  1119. // MarshalUDT will be called for each field in the the UDT returned by Cassandra,
  1120. // the implementor should marshal the type to return by for example calling
  1121. // Marshal.
  1122. MarshalUDT(name string, info TypeInfo) ([]byte, error)
  1123. }
  1124. // UDTUnmarshaler should be implemented by users wanting to implement custom
  1125. // UDT unmarshaling.
  1126. type UDTUnmarshaler interface {
  1127. // UnmarshalUDT will be called for each field in the UDT return by Cassandra,
  1128. // the implementor should unmarshal the data into the value of their chosing,
  1129. // for example by calling Unmarshal.
  1130. UnmarshalUDT(name string, info TypeInfo, data []byte) error
  1131. }
  1132. func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
  1133. udt := info.(UDTTypeInfo)
  1134. switch v := value.(type) {
  1135. case UDTMarshaler:
  1136. var buf []byte
  1137. for _, e := range udt.Elements {
  1138. data, err := v.MarshalUDT(e.Name, e.Type)
  1139. if err != nil {
  1140. return nil, err
  1141. }
  1142. n := len(data)
  1143. buf = append(buf, byte(n<<24),
  1144. byte(n<<16),
  1145. byte(n<<8),
  1146. byte(n))
  1147. buf = append(buf, data...)
  1148. }
  1149. return buf, nil
  1150. case map[string]interface{}:
  1151. var buf []byte
  1152. for _, e := range udt.Elements {
  1153. val, ok := v[e.Name]
  1154. if !ok {
  1155. return nil, marshalErrorf("missing UDT field in map: %s", e.Name)
  1156. }
  1157. data, err := Marshal(e.Type, val)
  1158. if err != nil {
  1159. return nil, err
  1160. }
  1161. n := len(data)
  1162. buf = append(buf, byte(n<<24),
  1163. byte(n<<16),
  1164. byte(n<<8),
  1165. byte(n))
  1166. buf = append(buf, data...)
  1167. }
  1168. return buf, nil
  1169. }
  1170. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1171. }
  1172. func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
  1173. switch v := value.(type) {
  1174. case Unmarshaler:
  1175. return v.UnmarshalCQL(info, data)
  1176. case UDTUnmarshaler:
  1177. udt := info.(UDTTypeInfo)
  1178. for _, e := range udt.Elements {
  1179. size := readInt(data[:4])
  1180. data = data[4:]
  1181. var err error
  1182. if size < 0 {
  1183. err = v.UnmarshalUDT(e.Name, e.Type, nil)
  1184. } else {
  1185. err = v.UnmarshalUDT(e.Name, e.Type, data[:size])
  1186. data = data[size:]
  1187. }
  1188. if err != nil {
  1189. return err
  1190. }
  1191. }
  1192. return nil
  1193. }
  1194. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1195. }
  1196. // TypeInfo describes a Cassandra specific data type.
  1197. type TypeInfo interface {
  1198. Type() Type
  1199. Version() byte
  1200. Custom() string
  1201. // New creates a pointer to an empty version of whatever type
  1202. // is referenced by the TypeInfo receiver
  1203. New() interface{}
  1204. }
  1205. type NativeType struct {
  1206. proto byte
  1207. typ Type
  1208. custom string // only used for TypeCustom
  1209. }
  1210. func (t NativeType) New() interface{} {
  1211. return reflect.New(goType(t)).Interface()
  1212. }
  1213. func (s NativeType) Type() Type {
  1214. return s.typ
  1215. }
  1216. func (s NativeType) Version() byte {
  1217. return s.proto
  1218. }
  1219. func (s NativeType) Custom() string {
  1220. return s.custom
  1221. }
  1222. func (s NativeType) String() string {
  1223. switch s.typ {
  1224. case TypeCustom:
  1225. return fmt.Sprintf("%s(%s)", s.typ, s.custom)
  1226. default:
  1227. return s.typ.String()
  1228. }
  1229. }
  1230. type CollectionType struct {
  1231. NativeType
  1232. Key TypeInfo // only used for TypeMap
  1233. Elem TypeInfo // only used for TypeMap, TypeList and TypeSet
  1234. }
  1235. func (t CollectionType) New() interface{} {
  1236. return reflect.New(goType(t)).Interface()
  1237. }
  1238. func (c CollectionType) String() string {
  1239. switch c.typ {
  1240. case TypeMap:
  1241. return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem)
  1242. case TypeList, TypeSet:
  1243. return fmt.Sprintf("%s(%s)", c.typ, c.Elem)
  1244. case TypeCustom:
  1245. return fmt.Sprintf("%s(%s)", c.typ, c.custom)
  1246. default:
  1247. return c.typ.String()
  1248. }
  1249. }
  1250. type TupleTypeInfo struct {
  1251. NativeType
  1252. Elems []TypeInfo
  1253. }
  1254. type UDTField struct {
  1255. Name string
  1256. Type TypeInfo
  1257. }
  1258. type UDTTypeInfo struct {
  1259. NativeType
  1260. KeySpace string
  1261. Name string
  1262. Elements []UDTField
  1263. }
  1264. func (u UDTTypeInfo) String() string {
  1265. buf := &bytes.Buffer{}
  1266. fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name)
  1267. first := true
  1268. for _, e := range u.Elements {
  1269. if !first {
  1270. fmt.Fprint(buf, ",")
  1271. } else {
  1272. first = false
  1273. }
  1274. fmt.Fprintf(buf, "%s=%v", e.Name, e.Type)
  1275. }
  1276. fmt.Fprint(buf, "}")
  1277. return buf.String()
  1278. }
  1279. // String returns a human readable name for the Cassandra datatype
  1280. // described by t.
  1281. // Type is the identifier of a Cassandra internal datatype.
  1282. type Type int
  1283. const (
  1284. TypeCustom Type = 0x0000
  1285. TypeAscii = 0x0001
  1286. TypeBigInt = 0x0002
  1287. TypeBlob = 0x0003
  1288. TypeBoolean = 0x0004
  1289. TypeCounter = 0x0005
  1290. TypeDecimal = 0x0006
  1291. TypeDouble = 0x0007
  1292. TypeFloat = 0x0008
  1293. TypeInt = 0x0009
  1294. TypeTimestamp = 0x000B
  1295. TypeUUID = 0x000C
  1296. TypeVarchar = 0x000D
  1297. TypeVarint = 0x000E
  1298. TypeTimeUUID = 0x000F
  1299. TypeInet = 0x0010
  1300. TypeList = 0x0020
  1301. TypeMap = 0x0021
  1302. TypeSet = 0x0022
  1303. TypeUDT = 0x0030
  1304. TypeTuple = 0x0031
  1305. )
  1306. // String returns the name of the identifier.
  1307. func (t Type) String() string {
  1308. switch t {
  1309. case TypeCustom:
  1310. return "custom"
  1311. case TypeAscii:
  1312. return "ascii"
  1313. case TypeBigInt:
  1314. return "bigint"
  1315. case TypeBlob:
  1316. return "blob"
  1317. case TypeBoolean:
  1318. return "boolean"
  1319. case TypeCounter:
  1320. return "counter"
  1321. case TypeDecimal:
  1322. return "decimal"
  1323. case TypeDouble:
  1324. return "double"
  1325. case TypeFloat:
  1326. return "float"
  1327. case TypeInt:
  1328. return "int"
  1329. case TypeTimestamp:
  1330. return "timestamp"
  1331. case TypeUUID:
  1332. return "uuid"
  1333. case TypeVarchar:
  1334. return "varchar"
  1335. case TypeTimeUUID:
  1336. return "timeuuid"
  1337. case TypeInet:
  1338. return "inet"
  1339. case TypeList:
  1340. return "list"
  1341. case TypeMap:
  1342. return "map"
  1343. case TypeSet:
  1344. return "set"
  1345. case TypeVarint:
  1346. return "varint"
  1347. case TypeTuple:
  1348. return "tuple"
  1349. default:
  1350. return fmt.Sprintf("unknown_type_%d", t)
  1351. }
  1352. }
  1353. type MarshalError string
  1354. func (m MarshalError) Error() string {
  1355. return string(m)
  1356. }
  1357. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1358. return MarshalError(fmt.Sprintf(format, args...))
  1359. }
  1360. type UnmarshalError string
  1361. func (m UnmarshalError) Error() string {
  1362. return string(m)
  1363. }
  1364. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1365. return UnmarshalError(fmt.Sprintf(format, args...))
  1366. }