marshal.go 41 KB

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