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