marshal.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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 Marshaler:
  1136. return v.MarshalCQL(info)
  1137. case UDTMarshaler:
  1138. var buf []byte
  1139. for _, e := range udt.Elements {
  1140. data, err := v.MarshalUDT(e.Name, e.Type)
  1141. if err != nil {
  1142. return nil, err
  1143. }
  1144. n := len(data)
  1145. buf = append(buf, byte(n<<24),
  1146. byte(n<<16),
  1147. byte(n<<8),
  1148. byte(n))
  1149. buf = append(buf, data...)
  1150. }
  1151. return buf, nil
  1152. case map[string]interface{}:
  1153. var buf []byte
  1154. for _, e := range udt.Elements {
  1155. val, ok := v[e.Name]
  1156. if !ok {
  1157. return nil, marshalErrorf("missing UDT field in map: %s", e.Name)
  1158. }
  1159. data, err := Marshal(e.Type, val)
  1160. if err != nil {
  1161. return nil, err
  1162. }
  1163. n := len(data)
  1164. buf = append(buf, byte(n<<24),
  1165. byte(n<<16),
  1166. byte(n<<8),
  1167. byte(n))
  1168. buf = append(buf, data...)
  1169. }
  1170. return buf, nil
  1171. }
  1172. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1173. }
  1174. func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
  1175. switch v := value.(type) {
  1176. case Unmarshaler:
  1177. return v.UnmarshalCQL(info, data)
  1178. case UDTUnmarshaler:
  1179. udt := info.(UDTTypeInfo)
  1180. for _, e := range udt.Elements {
  1181. size := readInt(data[:4])
  1182. data = data[4:]
  1183. var err error
  1184. if size < 0 {
  1185. err = v.UnmarshalUDT(e.Name, e.Type, nil)
  1186. } else {
  1187. err = v.UnmarshalUDT(e.Name, e.Type, data[:size])
  1188. data = data[size:]
  1189. }
  1190. if err != nil {
  1191. return err
  1192. }
  1193. }
  1194. return nil
  1195. }
  1196. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1197. }
  1198. // TypeInfo describes a Cassandra specific data type.
  1199. type TypeInfo interface {
  1200. Type() Type
  1201. Version() byte
  1202. Custom() string
  1203. // New creates a pointer to an empty version of whatever type
  1204. // is referenced by the TypeInfo receiver
  1205. New() interface{}
  1206. }
  1207. type NativeType struct {
  1208. proto byte
  1209. typ Type
  1210. custom string // only used for TypeCustom
  1211. }
  1212. func (t NativeType) New() interface{} {
  1213. return reflect.New(goType(t)).Interface()
  1214. }
  1215. func (s NativeType) Type() Type {
  1216. return s.typ
  1217. }
  1218. func (s NativeType) Version() byte {
  1219. return s.proto
  1220. }
  1221. func (s NativeType) Custom() string {
  1222. return s.custom
  1223. }
  1224. func (s NativeType) String() string {
  1225. switch s.typ {
  1226. case TypeCustom:
  1227. return fmt.Sprintf("%s(%s)", s.typ, s.custom)
  1228. default:
  1229. return s.typ.String()
  1230. }
  1231. }
  1232. type CollectionType struct {
  1233. NativeType
  1234. Key TypeInfo // only used for TypeMap
  1235. Elem TypeInfo // only used for TypeMap, TypeList and TypeSet
  1236. }
  1237. func (t CollectionType) New() interface{} {
  1238. return reflect.New(goType(t)).Interface()
  1239. }
  1240. func (c CollectionType) String() string {
  1241. switch c.typ {
  1242. case TypeMap:
  1243. return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem)
  1244. case TypeList, TypeSet:
  1245. return fmt.Sprintf("%s(%s)", c.typ, c.Elem)
  1246. case TypeCustom:
  1247. return fmt.Sprintf("%s(%s)", c.typ, c.custom)
  1248. default:
  1249. return c.typ.String()
  1250. }
  1251. }
  1252. type TupleTypeInfo struct {
  1253. NativeType
  1254. Elems []TypeInfo
  1255. }
  1256. type UDTField struct {
  1257. Name string
  1258. Type TypeInfo
  1259. }
  1260. type UDTTypeInfo struct {
  1261. NativeType
  1262. KeySpace string
  1263. Name string
  1264. Elements []UDTField
  1265. }
  1266. func (u UDTTypeInfo) String() string {
  1267. buf := &bytes.Buffer{}
  1268. fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name)
  1269. first := true
  1270. for _, e := range u.Elements {
  1271. if !first {
  1272. fmt.Fprint(buf, ",")
  1273. } else {
  1274. first = false
  1275. }
  1276. fmt.Fprintf(buf, "%s=%v", e.Name, e.Type)
  1277. }
  1278. fmt.Fprint(buf, "}")
  1279. return buf.String()
  1280. }
  1281. // String returns a human readable name for the Cassandra datatype
  1282. // described by t.
  1283. // Type is the identifier of a Cassandra internal datatype.
  1284. type Type int
  1285. const (
  1286. TypeCustom Type = 0x0000
  1287. TypeAscii = 0x0001
  1288. TypeBigInt = 0x0002
  1289. TypeBlob = 0x0003
  1290. TypeBoolean = 0x0004
  1291. TypeCounter = 0x0005
  1292. TypeDecimal = 0x0006
  1293. TypeDouble = 0x0007
  1294. TypeFloat = 0x0008
  1295. TypeInt = 0x0009
  1296. TypeTimestamp = 0x000B
  1297. TypeUUID = 0x000C
  1298. TypeVarchar = 0x000D
  1299. TypeVarint = 0x000E
  1300. TypeTimeUUID = 0x000F
  1301. TypeInet = 0x0010
  1302. TypeList = 0x0020
  1303. TypeMap = 0x0021
  1304. TypeSet = 0x0022
  1305. TypeUDT = 0x0030
  1306. TypeTuple = 0x0031
  1307. )
  1308. // String returns the name of the identifier.
  1309. func (t Type) String() string {
  1310. switch t {
  1311. case TypeCustom:
  1312. return "custom"
  1313. case TypeAscii:
  1314. return "ascii"
  1315. case TypeBigInt:
  1316. return "bigint"
  1317. case TypeBlob:
  1318. return "blob"
  1319. case TypeBoolean:
  1320. return "boolean"
  1321. case TypeCounter:
  1322. return "counter"
  1323. case TypeDecimal:
  1324. return "decimal"
  1325. case TypeDouble:
  1326. return "double"
  1327. case TypeFloat:
  1328. return "float"
  1329. case TypeInt:
  1330. return "int"
  1331. case TypeTimestamp:
  1332. return "timestamp"
  1333. case TypeUUID:
  1334. return "uuid"
  1335. case TypeVarchar:
  1336. return "varchar"
  1337. case TypeTimeUUID:
  1338. return "timeuuid"
  1339. case TypeInet:
  1340. return "inet"
  1341. case TypeList:
  1342. return "list"
  1343. case TypeMap:
  1344. return "map"
  1345. case TypeSet:
  1346. return "set"
  1347. case TypeVarint:
  1348. return "varint"
  1349. case TypeTuple:
  1350. return "tuple"
  1351. default:
  1352. return fmt.Sprintf("unknown_type_%d", t)
  1353. }
  1354. }
  1355. type MarshalError string
  1356. func (m MarshalError) Error() string {
  1357. return string(m)
  1358. }
  1359. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1360. return MarshalError(fmt.Sprintf(format, args...))
  1361. }
  1362. type UnmarshalError string
  1363. func (m UnmarshalError) Error() string {
  1364. return string(m)
  1365. }
  1366. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1367. return UnmarshalError(fmt.Sprintf(format, args...))
  1368. }