marshal.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  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 TypeTinyInt:
  59. return marshalTinyInt(info, value)
  60. case TypeSmallInt:
  61. return marshalSmallInt(info, value)
  62. case TypeInt:
  63. return marshalInt(info, value)
  64. case TypeBigInt, TypeCounter:
  65. return marshalBigInt(info, value)
  66. case TypeFloat:
  67. return marshalFloat(info, value)
  68. case TypeDouble:
  69. return marshalDouble(info, value)
  70. case TypeDecimal:
  71. return marshalDecimal(info, value)
  72. case TypeTimestamp:
  73. return marshalTimestamp(info, value)
  74. case TypeList, TypeSet:
  75. return marshalList(info, value)
  76. case TypeMap:
  77. return marshalMap(info, value)
  78. case TypeUUID, TypeTimeUUID:
  79. return marshalUUID(info, value)
  80. case TypeVarint:
  81. return marshalVarint(info, value)
  82. case TypeInet:
  83. return marshalInet(info, value)
  84. case TypeTuple:
  85. return marshalTuple(info, value)
  86. case TypeUDT:
  87. return marshalUDT(info, value)
  88. case TypeDate:
  89. return marshalDate(info, value)
  90. }
  91. // detect protocol 2 UDT
  92. if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 {
  93. return nil, ErrorUDTUnavailable
  94. }
  95. // TODO(tux21b): add the remaining types
  96. return nil, fmt.Errorf("can not marshal %T into %s", value, info)
  97. }
  98. // Unmarshal parses the CQL encoded data based on the info parameter that
  99. // describes the Cassandra internal data type and stores the result in the
  100. // value pointed by value.
  101. func Unmarshal(info TypeInfo, data []byte, value interface{}) error {
  102. if v, ok := value.(Unmarshaler); ok {
  103. return v.UnmarshalCQL(info, data)
  104. }
  105. if isNullableValue(value) {
  106. return unmarshalNullable(info, data, value)
  107. }
  108. switch info.Type() {
  109. case TypeVarchar, TypeAscii, TypeBlob, TypeText:
  110. return unmarshalVarchar(info, data, value)
  111. case TypeBoolean:
  112. return unmarshalBool(info, data, value)
  113. case TypeInt:
  114. return unmarshalInt(info, data, value)
  115. case TypeBigInt, TypeCounter:
  116. return unmarshalBigInt(info, data, value)
  117. case TypeVarint:
  118. return unmarshalVarint(info, data, value)
  119. case TypeSmallInt:
  120. return unmarshalSmallInt(info, data, value)
  121. case TypeTinyInt:
  122. return unmarshalTinyInt(info, data, value)
  123. case TypeFloat:
  124. return unmarshalFloat(info, data, value)
  125. case TypeDouble:
  126. return unmarshalDouble(info, data, value)
  127. case TypeDecimal:
  128. return unmarshalDecimal(info, data, value)
  129. case TypeTimestamp:
  130. return unmarshalTimestamp(info, data, value)
  131. case TypeList, TypeSet:
  132. return unmarshalList(info, data, value)
  133. case TypeMap:
  134. return unmarshalMap(info, data, value)
  135. case TypeTimeUUID:
  136. return unmarshalTimeUUID(info, data, value)
  137. case TypeUUID:
  138. return unmarshalUUID(info, data, value)
  139. case TypeInet:
  140. return unmarshalInet(info, data, value)
  141. case TypeTuple:
  142. return unmarshalTuple(info, data, value)
  143. case TypeUDT:
  144. return unmarshalUDT(info, data, value)
  145. case TypeDate:
  146. return unmarshalDate(info, data, value)
  147. }
  148. // detect protocol 2 UDT
  149. if strings.HasPrefix(info.Custom(), "org.apache.cassandra.db.marshal.UserType") && info.Version() < 3 {
  150. return ErrorUDTUnavailable
  151. }
  152. // TODO(tux21b): add the remaining types
  153. return fmt.Errorf("can not unmarshal %s into %T", info, value)
  154. }
  155. func isNullableValue(value interface{}) bool {
  156. v := reflect.ValueOf(value)
  157. return v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Ptr
  158. }
  159. func isNullData(info TypeInfo, data []byte) bool {
  160. return len(data) == 0
  161. }
  162. func unmarshalNullable(info TypeInfo, data []byte, value interface{}) error {
  163. valueRef := reflect.ValueOf(value)
  164. if isNullData(info, data) {
  165. nilValue := reflect.Zero(valueRef.Type().Elem())
  166. valueRef.Elem().Set(nilValue)
  167. return nil
  168. }
  169. newValue := reflect.New(valueRef.Type().Elem().Elem())
  170. valueRef.Elem().Set(newValue)
  171. return Unmarshal(info, data, newValue.Interface())
  172. }
  173. func marshalVarchar(info TypeInfo, value interface{}) ([]byte, error) {
  174. switch v := value.(type) {
  175. case Marshaler:
  176. return v.MarshalCQL(info)
  177. case unsetColumn:
  178. return nil, nil
  179. case string:
  180. return []byte(v), nil
  181. case []byte:
  182. return v, nil
  183. }
  184. if value == nil {
  185. return nil, nil
  186. }
  187. rv := reflect.ValueOf(value)
  188. t := rv.Type()
  189. k := t.Kind()
  190. switch {
  191. case k == reflect.String:
  192. return []byte(rv.String()), nil
  193. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  194. return rv.Bytes(), nil
  195. }
  196. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  197. }
  198. func unmarshalVarchar(info TypeInfo, data []byte, value interface{}) error {
  199. switch v := value.(type) {
  200. case Unmarshaler:
  201. return v.UnmarshalCQL(info, data)
  202. case *string:
  203. *v = string(data)
  204. return nil
  205. case *[]byte:
  206. var dataCopy []byte
  207. if data != nil {
  208. dataCopy = make([]byte, len(data))
  209. copy(dataCopy, data)
  210. }
  211. *v = dataCopy
  212. return nil
  213. }
  214. rv := reflect.ValueOf(value)
  215. if rv.Kind() != reflect.Ptr {
  216. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  217. }
  218. rv = rv.Elem()
  219. t := rv.Type()
  220. k := t.Kind()
  221. switch {
  222. case k == reflect.String:
  223. rv.SetString(string(data))
  224. return nil
  225. case k == reflect.Slice && t.Elem().Kind() == reflect.Uint8:
  226. var dataCopy []byte
  227. if data != nil {
  228. dataCopy = make([]byte, len(data))
  229. copy(dataCopy, data)
  230. }
  231. rv.SetBytes(dataCopy)
  232. return nil
  233. }
  234. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  235. }
  236. func marshalSmallInt(info TypeInfo, value interface{}) ([]byte, error) {
  237. switch v := value.(type) {
  238. case Marshaler:
  239. return v.MarshalCQL(info)
  240. case unsetColumn:
  241. return nil, nil
  242. case int16:
  243. return encShort(v), nil
  244. case uint16:
  245. return encShort(int16(v)), nil
  246. case int8:
  247. return encShort(int16(v)), nil
  248. case uint8:
  249. return encShort(int16(v)), nil
  250. case int:
  251. if v > math.MaxInt16 || v < math.MinInt16 {
  252. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  253. }
  254. return encShort(int16(v)), nil
  255. case int32:
  256. if v > math.MaxInt16 || v < math.MinInt16 {
  257. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  258. }
  259. return encShort(int16(v)), nil
  260. case int64:
  261. if v > math.MaxInt16 || v < math.MinInt16 {
  262. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  263. }
  264. return encShort(int16(v)), nil
  265. case uint:
  266. if v > math.MaxUint16 {
  267. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  268. }
  269. return encShort(int16(v)), nil
  270. case uint32:
  271. if v > math.MaxUint16 {
  272. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  273. }
  274. return encShort(int16(v)), nil
  275. case uint64:
  276. if v > math.MaxUint16 {
  277. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  278. }
  279. return encShort(int16(v)), nil
  280. case string:
  281. n, err := strconv.ParseInt(v, 10, 16)
  282. if err != nil {
  283. return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err)
  284. }
  285. return encShort(int16(n)), nil
  286. }
  287. if value == nil {
  288. return nil, nil
  289. }
  290. switch rv := reflect.ValueOf(value); rv.Type().Kind() {
  291. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  292. v := rv.Int()
  293. if v > math.MaxInt16 || v < math.MinInt16 {
  294. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  295. }
  296. return encShort(int16(v)), nil
  297. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  298. v := rv.Uint()
  299. if v > math.MaxUint16 {
  300. return nil, marshalErrorf("marshal smallint: value %d out of range", v)
  301. }
  302. return encShort(int16(v)), nil
  303. default:
  304. if rv.IsNil() {
  305. return nil, nil
  306. }
  307. }
  308. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  309. }
  310. func marshalTinyInt(info TypeInfo, value interface{}) ([]byte, error) {
  311. switch v := value.(type) {
  312. case Marshaler:
  313. return v.MarshalCQL(info)
  314. case unsetColumn:
  315. return nil, nil
  316. case int8:
  317. return []byte{byte(v)}, nil
  318. case uint8:
  319. return []byte{byte(v)}, nil
  320. case int16:
  321. if v > math.MaxInt8 || v < math.MinInt8 {
  322. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  323. }
  324. return []byte{byte(v)}, nil
  325. case uint16:
  326. if v > math.MaxUint8 {
  327. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  328. }
  329. return []byte{byte(v)}, nil
  330. case int:
  331. if v > math.MaxInt8 || v < math.MinInt8 {
  332. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  333. }
  334. return []byte{byte(v)}, nil
  335. case int32:
  336. if v > math.MaxInt8 || v < math.MinInt8 {
  337. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  338. }
  339. return []byte{byte(v)}, nil
  340. case int64:
  341. if v > math.MaxInt8 || v < math.MinInt8 {
  342. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  343. }
  344. return []byte{byte(v)}, nil
  345. case uint:
  346. if v > math.MaxUint8 {
  347. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  348. }
  349. return []byte{byte(v)}, nil
  350. case uint32:
  351. if v > math.MaxUint8 {
  352. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  353. }
  354. return []byte{byte(v)}, nil
  355. case uint64:
  356. if v > math.MaxUint8 {
  357. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  358. }
  359. return []byte{byte(v)}, nil
  360. case string:
  361. n, err := strconv.ParseInt(v, 10, 8)
  362. if err != nil {
  363. return nil, marshalErrorf("can not marshal %T into %s: %v", value, info, err)
  364. }
  365. return []byte{byte(n)}, nil
  366. }
  367. if value == nil {
  368. return nil, nil
  369. }
  370. switch rv := reflect.ValueOf(value); rv.Type().Kind() {
  371. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  372. v := rv.Int()
  373. if v > math.MaxInt8 || v < math.MinInt8 {
  374. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  375. }
  376. return []byte{byte(v)}, nil
  377. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  378. v := rv.Uint()
  379. if v > math.MaxUint8 {
  380. return nil, marshalErrorf("marshal tinyint: value %d out of range", v)
  381. }
  382. return []byte{byte(v)}, nil
  383. default:
  384. if rv.IsNil() {
  385. return nil, nil
  386. }
  387. }
  388. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  389. }
  390. func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
  391. switch v := value.(type) {
  392. case Marshaler:
  393. return v.MarshalCQL(info)
  394. case unsetColumn:
  395. return nil, nil
  396. case int:
  397. if v > math.MaxInt32 || v < math.MinInt32 {
  398. return nil, marshalErrorf("marshal int: value %d out of range", v)
  399. }
  400. return encInt(int32(v)), nil
  401. case uint:
  402. if v > math.MaxUint32 {
  403. return nil, marshalErrorf("marshal int: value %d out of range", v)
  404. }
  405. return encInt(int32(v)), nil
  406. case int64:
  407. if v > math.MaxInt32 || v < math.MinInt32 {
  408. return nil, marshalErrorf("marshal int: value %d out of range", v)
  409. }
  410. return encInt(int32(v)), nil
  411. case uint64:
  412. if v > math.MaxUint32 {
  413. return nil, marshalErrorf("marshal int: value %d out of range", v)
  414. }
  415. return encInt(int32(v)), nil
  416. case int32:
  417. return encInt(v), nil
  418. case uint32:
  419. return encInt(int32(v)), nil
  420. case int16:
  421. return encInt(int32(v)), nil
  422. case uint16:
  423. return encInt(int32(v)), nil
  424. case int8:
  425. return encInt(int32(v)), nil
  426. case uint8:
  427. return encInt(int32(v)), nil
  428. case string:
  429. i, err := strconv.ParseInt(v, 10, 32)
  430. if err != nil {
  431. return nil, marshalErrorf("can not marshal string to int: %s", err)
  432. }
  433. return encInt(int32(i)), nil
  434. }
  435. if value == nil {
  436. return nil, nil
  437. }
  438. switch rv := reflect.ValueOf(value); rv.Type().Kind() {
  439. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  440. v := rv.Int()
  441. if v > math.MaxInt32 || v < math.MinInt32 {
  442. return nil, marshalErrorf("marshal int: value %d out of range", v)
  443. }
  444. return encInt(int32(v)), nil
  445. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  446. v := rv.Uint()
  447. if v > math.MaxInt32 {
  448. return nil, marshalErrorf("marshal int: value %d out of range", v)
  449. }
  450. return encInt(int32(v)), nil
  451. default:
  452. if rv.IsNil() {
  453. return nil, nil
  454. }
  455. }
  456. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  457. }
  458. func encInt(x int32) []byte {
  459. return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  460. }
  461. func decInt(x []byte) int32 {
  462. if len(x) != 4 {
  463. return 0
  464. }
  465. return int32(x[0])<<24 | int32(x[1])<<16 | int32(x[2])<<8 | int32(x[3])
  466. }
  467. func encShort(x int16) []byte {
  468. p := make([]byte, 2)
  469. p[0] = byte(x >> 8)
  470. p[1] = byte(x)
  471. return p
  472. }
  473. func decShort(p []byte) int16 {
  474. if len(p) != 2 {
  475. return 0
  476. }
  477. return int16(p[0])<<8 | int16(p[1])
  478. }
  479. func decTiny(p []byte) int8 {
  480. if len(p) != 1 {
  481. return 0
  482. }
  483. return int8(p[0])
  484. }
  485. func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) {
  486. switch v := value.(type) {
  487. case Marshaler:
  488. return v.MarshalCQL(info)
  489. case unsetColumn:
  490. return nil, nil
  491. case int:
  492. return encBigInt(int64(v)), nil
  493. case uint:
  494. if uint64(v) > math.MaxInt64 {
  495. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  496. }
  497. return encBigInt(int64(v)), nil
  498. case int64:
  499. return encBigInt(v), nil
  500. case uint64:
  501. return encBigInt(int64(v)), nil
  502. case int32:
  503. return encBigInt(int64(v)), nil
  504. case uint32:
  505. return encBigInt(int64(v)), nil
  506. case int16:
  507. return encBigInt(int64(v)), nil
  508. case uint16:
  509. return encBigInt(int64(v)), nil
  510. case int8:
  511. return encBigInt(int64(v)), nil
  512. case uint8:
  513. return encBigInt(int64(v)), nil
  514. case big.Int:
  515. return encBigInt2C(&v), nil
  516. case string:
  517. i, err := strconv.ParseInt(value.(string), 10, 64)
  518. if err != nil {
  519. return nil, marshalErrorf("can not marshal string to bigint: %s", err)
  520. }
  521. return encBigInt(i), nil
  522. }
  523. if value == nil {
  524. return nil, nil
  525. }
  526. rv := reflect.ValueOf(value)
  527. switch rv.Type().Kind() {
  528. case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
  529. v := rv.Int()
  530. return encBigInt(v), nil
  531. case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
  532. v := rv.Uint()
  533. if v > math.MaxInt64 {
  534. return nil, marshalErrorf("marshal bigint: value %d out of range", v)
  535. }
  536. return encBigInt(int64(v)), nil
  537. }
  538. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  539. }
  540. func encBigInt(x int64) []byte {
  541. return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40), byte(x >> 32),
  542. byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}
  543. }
  544. func bytesToInt64(data []byte) (ret int64) {
  545. for i := range data {
  546. ret |= int64(data[i]) << (8 * uint(len(data)-i-1))
  547. }
  548. return ret
  549. }
  550. func bytesToUint64(data []byte) (ret uint64) {
  551. for i := range data {
  552. ret |= uint64(data[i]) << (8 * uint(len(data)-i-1))
  553. }
  554. return ret
  555. }
  556. func unmarshalBigInt(info TypeInfo, data []byte, value interface{}) error {
  557. return unmarshalIntlike(info, decBigInt(data), data, value)
  558. }
  559. func unmarshalInt(info TypeInfo, data []byte, value interface{}) error {
  560. return unmarshalIntlike(info, int64(decInt(data)), data, value)
  561. }
  562. func unmarshalSmallInt(info TypeInfo, data []byte, value interface{}) error {
  563. return unmarshalIntlike(info, int64(decShort(data)), data, value)
  564. }
  565. func unmarshalTinyInt(info TypeInfo, data []byte, value interface{}) error {
  566. return unmarshalIntlike(info, int64(decTiny(data)), data, value)
  567. }
  568. func unmarshalVarint(info TypeInfo, data []byte, value interface{}) error {
  569. switch v := value.(type) {
  570. case *big.Int:
  571. return unmarshalIntlike(info, 0, data, value)
  572. case *uint64:
  573. if len(data) == 9 && data[0] == 0 {
  574. *v = bytesToUint64(data[1:])
  575. return nil
  576. }
  577. }
  578. if len(data) > 8 {
  579. return unmarshalErrorf("unmarshal int: varint value %v out of range for %T (use big.Int)", data, value)
  580. }
  581. int64Val := bytesToInt64(data)
  582. if len(data) > 0 && len(data) < 8 && data[0]&0x80 > 0 {
  583. int64Val -= (1 << uint(len(data)*8))
  584. }
  585. return unmarshalIntlike(info, int64Val, data, value)
  586. }
  587. func marshalVarint(info TypeInfo, value interface{}) ([]byte, error) {
  588. var (
  589. retBytes []byte
  590. err error
  591. )
  592. switch v := value.(type) {
  593. case unsetColumn:
  594. return nil, nil
  595. case uint64:
  596. if v > uint64(math.MaxInt64) {
  597. retBytes = make([]byte, 9)
  598. binary.BigEndian.PutUint64(retBytes[1:], v)
  599. } else {
  600. retBytes = make([]byte, 8)
  601. binary.BigEndian.PutUint64(retBytes, v)
  602. }
  603. default:
  604. retBytes, err = marshalBigInt(info, value)
  605. }
  606. if err == nil {
  607. // trim down to most significant byte
  608. i := 0
  609. for ; i < len(retBytes)-1; i++ {
  610. b0 := retBytes[i]
  611. if b0 != 0 && b0 != 0xFF {
  612. break
  613. }
  614. b1 := retBytes[i+1]
  615. if b0 == 0 && b1 != 0 {
  616. if b1&0x80 == 0 {
  617. i++
  618. }
  619. break
  620. }
  621. if b0 == 0xFF && b1 != 0xFF {
  622. if b1&0x80 > 0 {
  623. i++
  624. }
  625. break
  626. }
  627. }
  628. retBytes = retBytes[i:]
  629. }
  630. return retBytes, err
  631. }
  632. func unmarshalIntlike(info TypeInfo, int64Val int64, data []byte, value interface{}) error {
  633. switch v := value.(type) {
  634. case *int:
  635. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  636. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  637. }
  638. *v = int(int64Val)
  639. return nil
  640. case *uint:
  641. unitVal := uint64(int64Val)
  642. if ^uint(0) == math.MaxUint32 && unitVal > math.MaxUint32 {
  643. return unmarshalErrorf("unmarshal int: value %d out of range for %T", unitVal, *v)
  644. }
  645. switch info.Type() {
  646. case TypeInt:
  647. *v = uint(unitVal) & 0xFFFFFFFF
  648. case TypeSmallInt:
  649. *v = uint(unitVal) & 0xFFFF
  650. case TypeTinyInt:
  651. *v = uint(unitVal) & 0xFF
  652. default:
  653. *v = uint(unitVal)
  654. }
  655. return nil
  656. case *int64:
  657. *v = int64Val
  658. return nil
  659. case *uint64:
  660. switch info.Type() {
  661. case TypeInt:
  662. *v = uint64(int64Val) & 0xFFFFFFFF
  663. case TypeSmallInt:
  664. *v = uint64(int64Val) & 0xFFFF
  665. case TypeTinyInt:
  666. *v = uint64(int64Val) & 0xFF
  667. default:
  668. *v = uint64(int64Val)
  669. }
  670. return nil
  671. case *int32:
  672. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  673. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  674. }
  675. *v = int32(int64Val)
  676. return nil
  677. case *uint32:
  678. if int64Val > math.MaxUint32 {
  679. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  680. }
  681. switch info.Type() {
  682. case TypeSmallInt:
  683. *v = uint32(int64Val) & 0xFFFF
  684. case TypeTinyInt:
  685. *v = uint32(int64Val) & 0xFF
  686. default:
  687. *v = uint32(int64Val) & 0xFFFFFFFF
  688. }
  689. return nil
  690. case *int16:
  691. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  692. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  693. }
  694. *v = int16(int64Val)
  695. return nil
  696. case *uint16:
  697. if int64Val > math.MaxUint16 {
  698. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  699. }
  700. switch info.Type() {
  701. case TypeTinyInt:
  702. *v = uint16(int64Val) & 0xFF
  703. default:
  704. *v = uint16(int64Val) & 0xFFFF
  705. }
  706. return nil
  707. case *int8:
  708. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  709. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  710. }
  711. *v = int8(int64Val)
  712. return nil
  713. case *uint8:
  714. if int64Val > math.MaxUint8 {
  715. return unmarshalErrorf("unmarshal int: value %d out of range for %T", int64Val, *v)
  716. }
  717. *v = uint8(int64Val) & 0xFF
  718. return nil
  719. case *big.Int:
  720. decBigInt2C(data, v)
  721. return nil
  722. case *string:
  723. *v = strconv.FormatInt(int64Val, 10)
  724. return nil
  725. }
  726. rv := reflect.ValueOf(value)
  727. if rv.Kind() != reflect.Ptr {
  728. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  729. }
  730. rv = rv.Elem()
  731. switch rv.Type().Kind() {
  732. case reflect.Int:
  733. if ^uint(0) == math.MaxUint32 && (int64Val < math.MinInt32 || int64Val > math.MaxInt32) {
  734. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  735. }
  736. rv.SetInt(int64Val)
  737. return nil
  738. case reflect.Int64:
  739. rv.SetInt(int64Val)
  740. return nil
  741. case reflect.Int32:
  742. if int64Val < math.MinInt32 || int64Val > math.MaxInt32 {
  743. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  744. }
  745. rv.SetInt(int64Val)
  746. return nil
  747. case reflect.Int16:
  748. if int64Val < math.MinInt16 || int64Val > math.MaxInt16 {
  749. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  750. }
  751. rv.SetInt(int64Val)
  752. return nil
  753. case reflect.Int8:
  754. if int64Val < math.MinInt8 || int64Val > math.MaxInt8 {
  755. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  756. }
  757. rv.SetInt(int64Val)
  758. return nil
  759. case reflect.Uint:
  760. if int64Val < 0 || (^uint(0) == math.MaxUint32 && int64Val > math.MaxUint32) {
  761. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  762. }
  763. rv.SetUint(uint64(int64Val))
  764. return nil
  765. case reflect.Uint64:
  766. if int64Val < 0 {
  767. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  768. }
  769. rv.SetUint(uint64(int64Val))
  770. return nil
  771. case reflect.Uint32:
  772. if int64Val < 0 || int64Val > math.MaxUint32 {
  773. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  774. }
  775. rv.SetUint(uint64(int64Val))
  776. return nil
  777. case reflect.Uint16:
  778. if int64Val < 0 || int64Val > math.MaxUint16 {
  779. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  780. }
  781. rv.SetUint(uint64(int64Val))
  782. return nil
  783. case reflect.Uint8:
  784. if int64Val < 0 || int64Val > math.MaxUint8 {
  785. return unmarshalErrorf("unmarshal int: value %d out of range", int64Val)
  786. }
  787. rv.SetUint(uint64(int64Val))
  788. return nil
  789. }
  790. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  791. }
  792. func decBigInt(data []byte) int64 {
  793. if len(data) != 8 {
  794. return 0
  795. }
  796. return int64(data[0])<<56 | int64(data[1])<<48 |
  797. int64(data[2])<<40 | int64(data[3])<<32 |
  798. int64(data[4])<<24 | int64(data[5])<<16 |
  799. int64(data[6])<<8 | int64(data[7])
  800. }
  801. func marshalBool(info TypeInfo, value interface{}) ([]byte, error) {
  802. switch v := value.(type) {
  803. case Marshaler:
  804. return v.MarshalCQL(info)
  805. case unsetColumn:
  806. return nil, nil
  807. case bool:
  808. return encBool(v), nil
  809. }
  810. if value == nil {
  811. return nil, nil
  812. }
  813. rv := reflect.ValueOf(value)
  814. switch rv.Type().Kind() {
  815. case reflect.Bool:
  816. return encBool(rv.Bool()), nil
  817. }
  818. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  819. }
  820. func encBool(v bool) []byte {
  821. if v {
  822. return []byte{1}
  823. }
  824. return []byte{0}
  825. }
  826. func unmarshalBool(info TypeInfo, data []byte, value interface{}) error {
  827. switch v := value.(type) {
  828. case Unmarshaler:
  829. return v.UnmarshalCQL(info, data)
  830. case *bool:
  831. *v = decBool(data)
  832. return nil
  833. }
  834. rv := reflect.ValueOf(value)
  835. if rv.Kind() != reflect.Ptr {
  836. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  837. }
  838. rv = rv.Elem()
  839. switch rv.Type().Kind() {
  840. case reflect.Bool:
  841. rv.SetBool(decBool(data))
  842. return nil
  843. }
  844. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  845. }
  846. func decBool(v []byte) bool {
  847. if len(v) == 0 {
  848. return false
  849. }
  850. return v[0] != 0
  851. }
  852. func marshalFloat(info TypeInfo, value interface{}) ([]byte, error) {
  853. switch v := value.(type) {
  854. case Marshaler:
  855. return v.MarshalCQL(info)
  856. case unsetColumn:
  857. return nil, nil
  858. case float32:
  859. return encInt(int32(math.Float32bits(v))), nil
  860. }
  861. if value == nil {
  862. return nil, nil
  863. }
  864. rv := reflect.ValueOf(value)
  865. switch rv.Type().Kind() {
  866. case reflect.Float32:
  867. return encInt(int32(math.Float32bits(float32(rv.Float())))), nil
  868. }
  869. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  870. }
  871. func unmarshalFloat(info TypeInfo, data []byte, value interface{}) error {
  872. switch v := value.(type) {
  873. case Unmarshaler:
  874. return v.UnmarshalCQL(info, data)
  875. case *float32:
  876. *v = math.Float32frombits(uint32(decInt(data)))
  877. return nil
  878. }
  879. rv := reflect.ValueOf(value)
  880. if rv.Kind() != reflect.Ptr {
  881. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  882. }
  883. rv = rv.Elem()
  884. switch rv.Type().Kind() {
  885. case reflect.Float32:
  886. rv.SetFloat(float64(math.Float32frombits(uint32(decInt(data)))))
  887. return nil
  888. }
  889. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  890. }
  891. func marshalDouble(info TypeInfo, value interface{}) ([]byte, error) {
  892. switch v := value.(type) {
  893. case Marshaler:
  894. return v.MarshalCQL(info)
  895. case unsetColumn:
  896. return nil, nil
  897. case float64:
  898. return encBigInt(int64(math.Float64bits(v))), nil
  899. }
  900. if value == nil {
  901. return nil, nil
  902. }
  903. rv := reflect.ValueOf(value)
  904. switch rv.Type().Kind() {
  905. case reflect.Float64:
  906. return encBigInt(int64(math.Float64bits(rv.Float()))), nil
  907. }
  908. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  909. }
  910. func unmarshalDouble(info TypeInfo, data []byte, value interface{}) error {
  911. switch v := value.(type) {
  912. case Unmarshaler:
  913. return v.UnmarshalCQL(info, data)
  914. case *float64:
  915. *v = math.Float64frombits(uint64(decBigInt(data)))
  916. return nil
  917. }
  918. rv := reflect.ValueOf(value)
  919. if rv.Kind() != reflect.Ptr {
  920. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  921. }
  922. rv = rv.Elem()
  923. switch rv.Type().Kind() {
  924. case reflect.Float64:
  925. rv.SetFloat(math.Float64frombits(uint64(decBigInt(data))))
  926. return nil
  927. }
  928. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  929. }
  930. func marshalDecimal(info TypeInfo, value interface{}) ([]byte, error) {
  931. if value == nil {
  932. return nil, nil
  933. }
  934. switch v := value.(type) {
  935. case Marshaler:
  936. return v.MarshalCQL(info)
  937. case unsetColumn:
  938. return nil, nil
  939. case inf.Dec:
  940. unscaled := encBigInt2C(v.UnscaledBig())
  941. if unscaled == nil {
  942. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  943. }
  944. buf := make([]byte, 4+len(unscaled))
  945. copy(buf[0:4], encInt(int32(v.Scale())))
  946. copy(buf[4:], unscaled)
  947. return buf, nil
  948. }
  949. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  950. }
  951. func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
  952. switch v := value.(type) {
  953. case Unmarshaler:
  954. return v.UnmarshalCQL(info, data)
  955. case *inf.Dec:
  956. scale := decInt(data[0:4])
  957. unscaled := decBigInt2C(data[4:], nil)
  958. *v = *inf.NewDecBig(unscaled, inf.Scale(scale))
  959. return nil
  960. }
  961. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  962. }
  963. // decBigInt2C sets the value of n to the big-endian two's complement
  964. // value stored in the given data. If data[0]&80 != 0, the number
  965. // is negative. If data is empty, the result will be 0.
  966. func decBigInt2C(data []byte, n *big.Int) *big.Int {
  967. if n == nil {
  968. n = new(big.Int)
  969. }
  970. n.SetBytes(data)
  971. if len(data) > 0 && data[0]&0x80 > 0 {
  972. n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8))
  973. }
  974. return n
  975. }
  976. // encBigInt2C returns the big-endian two's complement
  977. // form of n.
  978. func encBigInt2C(n *big.Int) []byte {
  979. switch n.Sign() {
  980. case 0:
  981. return []byte{0}
  982. case 1:
  983. b := n.Bytes()
  984. if b[0]&0x80 > 0 {
  985. b = append([]byte{0}, b...)
  986. }
  987. return b
  988. case -1:
  989. length := uint(n.BitLen()/8+1) * 8
  990. b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes()
  991. // When the most significant bit is on a byte
  992. // boundary, we can get some extra significant
  993. // bits, so strip them off when that happens.
  994. if len(b) >= 2 && b[0] == 0xff && b[1]&0x80 != 0 {
  995. b = b[1:]
  996. }
  997. return b
  998. }
  999. return nil
  1000. }
  1001. func marshalTimestamp(info TypeInfo, value interface{}) ([]byte, error) {
  1002. switch v := value.(type) {
  1003. case Marshaler:
  1004. return v.MarshalCQL(info)
  1005. case unsetColumn:
  1006. return nil, nil
  1007. case int64:
  1008. return encBigInt(v), nil
  1009. case time.Time:
  1010. if v.IsZero() {
  1011. return []byte{}, nil
  1012. }
  1013. x := int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6)
  1014. return encBigInt(x), nil
  1015. }
  1016. if value == nil {
  1017. return nil, nil
  1018. }
  1019. rv := reflect.ValueOf(value)
  1020. switch rv.Type().Kind() {
  1021. case reflect.Int64:
  1022. return encBigInt(rv.Int()), nil
  1023. }
  1024. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1025. }
  1026. func unmarshalTimestamp(info TypeInfo, data []byte, value interface{}) error {
  1027. switch v := value.(type) {
  1028. case Unmarshaler:
  1029. return v.UnmarshalCQL(info, data)
  1030. case *int64:
  1031. *v = decBigInt(data)
  1032. return nil
  1033. case *time.Time:
  1034. if len(data) == 0 {
  1035. *v = time.Time{}
  1036. return nil
  1037. }
  1038. x := decBigInt(data)
  1039. sec := x / 1000
  1040. nsec := (x - sec*1000) * 1000000
  1041. *v = time.Unix(sec, nsec).In(time.UTC)
  1042. return nil
  1043. }
  1044. rv := reflect.ValueOf(value)
  1045. if rv.Kind() != reflect.Ptr {
  1046. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1047. }
  1048. rv = rv.Elem()
  1049. switch rv.Type().Kind() {
  1050. case reflect.Int64:
  1051. rv.SetInt(decBigInt(data))
  1052. return nil
  1053. }
  1054. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1055. }
  1056. func marshalDate(info TypeInfo, value interface{}) ([]byte, error) {
  1057. var timestamp int64
  1058. switch v := value.(type) {
  1059. case Marshaler:
  1060. return v.MarshalCQL(info)
  1061. case unsetColumn:
  1062. return nil, nil
  1063. case int64:
  1064. timestamp = v
  1065. x := timestamp/86400000 + int64(1<<31)
  1066. return encInt(int32(x)), nil
  1067. case time.Time:
  1068. if v.IsZero() {
  1069. return []byte{}, nil
  1070. }
  1071. timestamp = int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6)
  1072. x := timestamp/86400000 + int64(1<<31)
  1073. return encInt(int32(x)), nil
  1074. case *time.Time:
  1075. if v.IsZero() {
  1076. return []byte{}, nil
  1077. }
  1078. timestamp = int64(v.UTC().Unix()*1e3) + int64(v.UTC().Nanosecond()/1e6)
  1079. x := timestamp/86400000 + int64(1<<31)
  1080. return encInt(int32(x)), nil
  1081. case string:
  1082. if v == "" {
  1083. return []byte{}, nil
  1084. }
  1085. t, err := time.Parse("2006-01-02", v)
  1086. if err != nil {
  1087. return nil, marshalErrorf("can not marshal %T into %s, date layout must be '2006-01-02'", value, info)
  1088. }
  1089. timestamp = int64(t.UTC().Unix()*1e3) + int64(t.UTC().Nanosecond()/1e6)
  1090. x := timestamp/86400000 + int64(1<<31)
  1091. return encInt(int32(x)), nil
  1092. }
  1093. if value == nil {
  1094. return nil, nil
  1095. }
  1096. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1097. }
  1098. func unmarshalDate(info TypeInfo, data []byte, value interface{}) error {
  1099. switch v := value.(type) {
  1100. case Unmarshaler:
  1101. return v.UnmarshalCQL(info, data)
  1102. case *time.Time:
  1103. if len(data) == 0 {
  1104. *v = time.Time{}
  1105. return nil
  1106. }
  1107. var origin uint32 = 1 << 31
  1108. var current uint32 = binary.BigEndian.Uint32(data)
  1109. timestamp := (int64(current) - int64(origin)) * 86400000
  1110. *v = time.Unix(0, timestamp*int64(time.Millisecond)).In(time.UTC)
  1111. return nil
  1112. }
  1113. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1114. }
  1115. func writeCollectionSize(info CollectionType, n int, buf *bytes.Buffer) error {
  1116. if info.proto > protoVersion2 {
  1117. if n > math.MaxInt32 {
  1118. return marshalErrorf("marshal: collection too large")
  1119. }
  1120. buf.WriteByte(byte(n >> 24))
  1121. buf.WriteByte(byte(n >> 16))
  1122. buf.WriteByte(byte(n >> 8))
  1123. buf.WriteByte(byte(n))
  1124. } else {
  1125. if n > math.MaxUint16 {
  1126. return marshalErrorf("marshal: collection too large")
  1127. }
  1128. buf.WriteByte(byte(n >> 8))
  1129. buf.WriteByte(byte(n))
  1130. }
  1131. return nil
  1132. }
  1133. func marshalList(info TypeInfo, value interface{}) ([]byte, error) {
  1134. listInfo, ok := info.(CollectionType)
  1135. if !ok {
  1136. return nil, marshalErrorf("marshal: can not marshal non collection type into list")
  1137. }
  1138. if value == nil {
  1139. return nil, nil
  1140. } else if _, ok := value.(unsetColumn); ok {
  1141. return nil, nil
  1142. }
  1143. rv := reflect.ValueOf(value)
  1144. t := rv.Type()
  1145. k := t.Kind()
  1146. if k == reflect.Slice && rv.IsNil() {
  1147. return nil, nil
  1148. }
  1149. switch k {
  1150. case reflect.Slice, reflect.Array:
  1151. buf := &bytes.Buffer{}
  1152. n := rv.Len()
  1153. if err := writeCollectionSize(listInfo, n, buf); err != nil {
  1154. return nil, err
  1155. }
  1156. for i := 0; i < n; i++ {
  1157. item, err := Marshal(listInfo.Elem, rv.Index(i).Interface())
  1158. if err != nil {
  1159. return nil, err
  1160. }
  1161. if err := writeCollectionSize(listInfo, len(item), buf); err != nil {
  1162. return nil, err
  1163. }
  1164. buf.Write(item)
  1165. }
  1166. return buf.Bytes(), nil
  1167. case reflect.Map:
  1168. elem := t.Elem()
  1169. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  1170. rkeys := rv.MapKeys()
  1171. keys := make([]interface{}, len(rkeys))
  1172. for i := 0; i < len(keys); i++ {
  1173. keys[i] = rkeys[i].Interface()
  1174. }
  1175. return marshalList(listInfo, keys)
  1176. }
  1177. }
  1178. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1179. }
  1180. func readCollectionSize(info CollectionType, data []byte) (size, read int) {
  1181. if info.proto > protoVersion2 {
  1182. size = int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1183. read = 4
  1184. } else {
  1185. size = int(data[0])<<8 | int(data[1])
  1186. read = 2
  1187. }
  1188. return
  1189. }
  1190. func unmarshalList(info TypeInfo, data []byte, value interface{}) error {
  1191. listInfo, ok := info.(CollectionType)
  1192. if !ok {
  1193. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into list")
  1194. }
  1195. rv := reflect.ValueOf(value)
  1196. if rv.Kind() != reflect.Ptr {
  1197. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1198. }
  1199. rv = rv.Elem()
  1200. t := rv.Type()
  1201. k := t.Kind()
  1202. switch k {
  1203. case reflect.Slice, reflect.Array:
  1204. if data == nil {
  1205. if k == reflect.Array {
  1206. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  1207. }
  1208. if rv.IsNil() {
  1209. return nil
  1210. }
  1211. rv.Set(reflect.Zero(t))
  1212. return nil
  1213. }
  1214. if len(data) < 2 {
  1215. return unmarshalErrorf("unmarshal list: unexpected eof")
  1216. }
  1217. n, p := readCollectionSize(listInfo, data)
  1218. data = data[p:]
  1219. if k == reflect.Array {
  1220. if rv.Len() != n {
  1221. return unmarshalErrorf("unmarshal list: array with wrong size")
  1222. }
  1223. } else {
  1224. rv.Set(reflect.MakeSlice(t, n, n))
  1225. }
  1226. for i := 0; i < n; i++ {
  1227. if len(data) < 2 {
  1228. return unmarshalErrorf("unmarshal list: unexpected eof")
  1229. }
  1230. m, p := readCollectionSize(listInfo, data)
  1231. data = data[p:]
  1232. if err := Unmarshal(listInfo.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  1233. return err
  1234. }
  1235. data = data[m:]
  1236. }
  1237. return nil
  1238. }
  1239. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1240. }
  1241. func marshalMap(info TypeInfo, value interface{}) ([]byte, error) {
  1242. mapInfo, ok := info.(CollectionType)
  1243. if !ok {
  1244. return nil, marshalErrorf("marshal: can not marshal none collection type into map")
  1245. }
  1246. if value == nil {
  1247. return nil, nil
  1248. } else if _, ok := value.(unsetColumn); ok {
  1249. return nil, nil
  1250. }
  1251. rv := reflect.ValueOf(value)
  1252. if rv.IsNil() {
  1253. return nil, nil
  1254. }
  1255. t := rv.Type()
  1256. if t.Kind() != reflect.Map {
  1257. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1258. }
  1259. buf := &bytes.Buffer{}
  1260. n := rv.Len()
  1261. if err := writeCollectionSize(mapInfo, n, buf); err != nil {
  1262. return nil, err
  1263. }
  1264. keys := rv.MapKeys()
  1265. for _, key := range keys {
  1266. item, err := Marshal(mapInfo.Key, key.Interface())
  1267. if err != nil {
  1268. return nil, err
  1269. }
  1270. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  1271. return nil, err
  1272. }
  1273. buf.Write(item)
  1274. item, err = Marshal(mapInfo.Elem, rv.MapIndex(key).Interface())
  1275. if err != nil {
  1276. return nil, err
  1277. }
  1278. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  1279. return nil, err
  1280. }
  1281. buf.Write(item)
  1282. }
  1283. return buf.Bytes(), nil
  1284. }
  1285. func unmarshalMap(info TypeInfo, data []byte, value interface{}) error {
  1286. mapInfo, ok := info.(CollectionType)
  1287. if !ok {
  1288. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into map")
  1289. }
  1290. rv := reflect.ValueOf(value)
  1291. if rv.Kind() != reflect.Ptr {
  1292. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1293. }
  1294. rv = rv.Elem()
  1295. t := rv.Type()
  1296. if t.Kind() != reflect.Map {
  1297. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1298. }
  1299. if data == nil {
  1300. rv.Set(reflect.Zero(t))
  1301. return nil
  1302. }
  1303. rv.Set(reflect.MakeMap(t))
  1304. if len(data) < 2 {
  1305. return unmarshalErrorf("unmarshal map: unexpected eof")
  1306. }
  1307. n, p := readCollectionSize(mapInfo, data)
  1308. data = data[p:]
  1309. for i := 0; i < n; i++ {
  1310. if len(data) < 2 {
  1311. return unmarshalErrorf("unmarshal list: unexpected eof")
  1312. }
  1313. m, p := readCollectionSize(mapInfo, data)
  1314. data = data[p:]
  1315. key := reflect.New(t.Key())
  1316. if err := Unmarshal(mapInfo.Key, data[:m], key.Interface()); err != nil {
  1317. return err
  1318. }
  1319. data = data[m:]
  1320. m, p = readCollectionSize(mapInfo, data)
  1321. data = data[p:]
  1322. val := reflect.New(t.Elem())
  1323. if err := Unmarshal(mapInfo.Elem, data[:m], val.Interface()); err != nil {
  1324. return err
  1325. }
  1326. data = data[m:]
  1327. rv.SetMapIndex(key.Elem(), val.Elem())
  1328. }
  1329. return nil
  1330. }
  1331. func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) {
  1332. switch val := value.(type) {
  1333. case unsetColumn:
  1334. return nil, nil
  1335. case UUID:
  1336. return val.Bytes(), nil
  1337. case []byte:
  1338. if len(val) != 16 {
  1339. return nil, marshalErrorf("can not marshal []byte %d bytes long into %s, must be exactly 16 bytes long", len(val), info)
  1340. }
  1341. return val, nil
  1342. case string:
  1343. b, err := ParseUUID(val)
  1344. if err != nil {
  1345. return nil, err
  1346. }
  1347. return b[:], nil
  1348. }
  1349. if value == nil {
  1350. return nil, nil
  1351. }
  1352. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1353. }
  1354. func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
  1355. if data == nil || len(data) == 0 {
  1356. switch v := value.(type) {
  1357. case *string:
  1358. *v = ""
  1359. case *[]byte:
  1360. *v = nil
  1361. case *UUID:
  1362. *v = UUID{}
  1363. default:
  1364. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1365. }
  1366. return nil
  1367. }
  1368. u, err := UUIDFromBytes(data)
  1369. if err != nil {
  1370. return unmarshalErrorf("Unable to parse UUID: %s", err)
  1371. }
  1372. switch v := value.(type) {
  1373. case *string:
  1374. *v = u.String()
  1375. return nil
  1376. case *[]byte:
  1377. *v = u[:]
  1378. return nil
  1379. case *UUID:
  1380. *v = u
  1381. return nil
  1382. }
  1383. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1384. }
  1385. func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error {
  1386. switch v := value.(type) {
  1387. case Unmarshaler:
  1388. return v.UnmarshalCQL(info, data)
  1389. case *time.Time:
  1390. id, err := UUIDFromBytes(data)
  1391. if err != nil {
  1392. return err
  1393. } else if id.Version() != 1 {
  1394. return unmarshalErrorf("invalid timeuuid")
  1395. }
  1396. *v = id.Time()
  1397. return nil
  1398. default:
  1399. return unmarshalUUID(info, data, value)
  1400. }
  1401. }
  1402. func marshalInet(info TypeInfo, value interface{}) ([]byte, error) {
  1403. // we return either the 4 or 16 byte representation of an
  1404. // ip address here otherwise the db value will be prefixed
  1405. // with the remaining byte values e.g. ::ffff:127.0.0.1 and not 127.0.0.1
  1406. switch val := value.(type) {
  1407. case unsetColumn:
  1408. return nil, nil
  1409. case net.IP:
  1410. t := val.To4()
  1411. if t == nil {
  1412. return val.To16(), nil
  1413. }
  1414. return t, nil
  1415. case string:
  1416. b := net.ParseIP(val)
  1417. if b != nil {
  1418. t := b.To4()
  1419. if t == nil {
  1420. return b.To16(), nil
  1421. }
  1422. return t, nil
  1423. }
  1424. return nil, marshalErrorf("cannot marshal. invalid ip string %s", val)
  1425. }
  1426. if value == nil {
  1427. return nil, nil
  1428. }
  1429. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1430. }
  1431. func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
  1432. switch v := value.(type) {
  1433. case Unmarshaler:
  1434. return v.UnmarshalCQL(info, data)
  1435. case *net.IP:
  1436. if x := len(data); !(x == 4 || x == 16) {
  1437. return unmarshalErrorf("cannot unmarshal %s into %T: invalid sized IP: got %d bytes not 4 or 16", info, value, x)
  1438. }
  1439. buf := copyBytes(data)
  1440. ip := net.IP(buf)
  1441. if v4 := ip.To4(); v4 != nil {
  1442. *v = v4
  1443. return nil
  1444. }
  1445. *v = ip
  1446. return nil
  1447. case *string:
  1448. if len(data) == 0 {
  1449. *v = ""
  1450. return nil
  1451. }
  1452. ip := net.IP(data)
  1453. if v4 := ip.To4(); v4 != nil {
  1454. *v = v4.String()
  1455. return nil
  1456. }
  1457. *v = ip.String()
  1458. return nil
  1459. }
  1460. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1461. }
  1462. func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) {
  1463. tuple := info.(TupleTypeInfo)
  1464. switch v := value.(type) {
  1465. case unsetColumn:
  1466. return nil, unmarshalErrorf("Invalid request: UnsetValue is unsupported for tuples")
  1467. case []interface{}:
  1468. if len(v) != len(tuple.Elems) {
  1469. return nil, unmarshalErrorf("cannont marshal tuple: wrong number of elements")
  1470. }
  1471. var buf []byte
  1472. for i, elem := range v {
  1473. data, err := Marshal(tuple.Elems[i], elem)
  1474. if err != nil {
  1475. return nil, err
  1476. }
  1477. n := len(data)
  1478. buf = appendInt(buf, int32(n))
  1479. buf = append(buf, data...)
  1480. }
  1481. return buf, nil
  1482. }
  1483. rv := reflect.ValueOf(value)
  1484. t := rv.Type()
  1485. k := t.Kind()
  1486. switch k {
  1487. case reflect.Struct:
  1488. if v := t.NumField(); v != len(tuple.Elems) {
  1489. return nil, marshalErrorf("can not marshal tuple into struct %v, not enough fields have %d need %d", t, v, len(tuple.Elems))
  1490. }
  1491. var buf []byte
  1492. for i, elem := range tuple.Elems {
  1493. data, err := Marshal(elem, rv.Field(i).Interface())
  1494. if err != nil {
  1495. return nil, err
  1496. }
  1497. n := len(data)
  1498. buf = appendInt(buf, int32(n))
  1499. buf = append(buf, data...)
  1500. }
  1501. return buf, nil
  1502. case reflect.Slice, reflect.Array:
  1503. size := rv.Len()
  1504. if size != len(tuple.Elems) {
  1505. return nil, marshalErrorf("can not marshal tuple into %v of length %d need %d elements", k, size, len(tuple.Elems))
  1506. }
  1507. var buf []byte
  1508. for i, elem := range tuple.Elems {
  1509. data, err := Marshal(elem, rv.Index(i).Interface())
  1510. if err != nil {
  1511. return nil, err
  1512. }
  1513. n := len(data)
  1514. buf = appendInt(buf, int32(n))
  1515. buf = append(buf, data...)
  1516. }
  1517. return buf, nil
  1518. }
  1519. return nil, marshalErrorf("cannot marshal %T into %s", value, tuple)
  1520. }
  1521. // currently only support unmarshal into a list of values, this makes it possible
  1522. // to support tuples without changing the query API. In the future this can be extend
  1523. // to allow unmarshalling into custom tuple types.
  1524. func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
  1525. if v, ok := value.(Unmarshaler); ok {
  1526. return v.UnmarshalCQL(info, data)
  1527. }
  1528. tuple := info.(TupleTypeInfo)
  1529. switch v := value.(type) {
  1530. case []interface{}:
  1531. for i, elem := range tuple.Elems {
  1532. // each element inside data is a [bytes]
  1533. size := readInt(data)
  1534. data = data[4:]
  1535. err := Unmarshal(elem, data[:size], v[i])
  1536. if err != nil {
  1537. return err
  1538. }
  1539. data = data[size:]
  1540. }
  1541. return nil
  1542. }
  1543. rv := reflect.ValueOf(value)
  1544. if rv.Kind() != reflect.Ptr {
  1545. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1546. }
  1547. rv = rv.Elem()
  1548. t := rv.Type()
  1549. k := t.Kind()
  1550. switch k {
  1551. case reflect.Struct:
  1552. if v := t.NumField(); v != len(tuple.Elems) {
  1553. return unmarshalErrorf("can not unmarshal tuple into struct %v, not enough fields have %d need %d", t, v, len(tuple.Elems))
  1554. }
  1555. for i, elem := range tuple.Elems {
  1556. m := readInt(data)
  1557. data = data[4:]
  1558. v := elem.New()
  1559. if err := Unmarshal(elem, data[:m], v); err != nil {
  1560. return err
  1561. }
  1562. rv.Field(i).Set(reflect.ValueOf(v).Elem())
  1563. data = data[m:]
  1564. }
  1565. return nil
  1566. case reflect.Slice, reflect.Array:
  1567. if k == reflect.Array {
  1568. size := rv.Len()
  1569. if size != len(tuple.Elems) {
  1570. return unmarshalErrorf("can not unmarshal tuple into array of length %d need %d elements", size, len(tuple.Elems))
  1571. }
  1572. } else {
  1573. rv.Set(reflect.MakeSlice(t, len(tuple.Elems), len(tuple.Elems)))
  1574. }
  1575. for i, elem := range tuple.Elems {
  1576. m := readInt(data)
  1577. data = data[4:]
  1578. v := elem.New()
  1579. if err := Unmarshal(elem, data[:m], v); err != nil {
  1580. return err
  1581. }
  1582. rv.Index(i).Set(reflect.ValueOf(v).Elem())
  1583. data = data[m:]
  1584. }
  1585. return nil
  1586. }
  1587. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1588. }
  1589. // UDTMarshaler is an interface which should be implemented by users wishing to
  1590. // handle encoding UDT types to sent to Cassandra. Note: due to current implentations
  1591. // methods defined for this interface must be value receivers not pointer receivers.
  1592. type UDTMarshaler interface {
  1593. // MarshalUDT will be called for each field in the the UDT returned by Cassandra,
  1594. // the implementor should marshal the type to return by for example calling
  1595. // Marshal.
  1596. MarshalUDT(name string, info TypeInfo) ([]byte, error)
  1597. }
  1598. // UDTUnmarshaler should be implemented by users wanting to implement custom
  1599. // UDT unmarshaling.
  1600. type UDTUnmarshaler interface {
  1601. // UnmarshalUDT will be called for each field in the UDT return by Cassandra,
  1602. // the implementor should unmarshal the data into the value of their chosing,
  1603. // for example by calling Unmarshal.
  1604. UnmarshalUDT(name string, info TypeInfo, data []byte) error
  1605. }
  1606. func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
  1607. udt := info.(UDTTypeInfo)
  1608. switch v := value.(type) {
  1609. case Marshaler:
  1610. return v.MarshalCQL(info)
  1611. case unsetColumn:
  1612. return nil, unmarshalErrorf("Invalid request: UnsetValue is unsupported for user defined types")
  1613. case UDTMarshaler:
  1614. var buf []byte
  1615. for _, e := range udt.Elements {
  1616. data, err := v.MarshalUDT(e.Name, e.Type)
  1617. if err != nil {
  1618. return nil, err
  1619. }
  1620. buf = appendBytes(buf, data)
  1621. }
  1622. return buf, nil
  1623. case map[string]interface{}:
  1624. var buf []byte
  1625. for _, e := range udt.Elements {
  1626. val, ok := v[e.Name]
  1627. if !ok {
  1628. continue
  1629. }
  1630. data, err := Marshal(e.Type, val)
  1631. if err != nil {
  1632. return nil, err
  1633. }
  1634. buf = appendBytes(buf, data)
  1635. }
  1636. return buf, nil
  1637. }
  1638. k := reflect.ValueOf(value)
  1639. if k.Kind() == reflect.Ptr {
  1640. if k.IsNil() {
  1641. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1642. }
  1643. k = k.Elem()
  1644. }
  1645. if k.Kind() != reflect.Struct || !k.IsValid() {
  1646. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1647. }
  1648. fields := make(map[string]reflect.Value)
  1649. t := reflect.TypeOf(value)
  1650. for i := 0; i < t.NumField(); i++ {
  1651. sf := t.Field(i)
  1652. if tag := sf.Tag.Get("cql"); tag != "" {
  1653. fields[tag] = k.Field(i)
  1654. }
  1655. }
  1656. var buf []byte
  1657. for _, e := range udt.Elements {
  1658. f, ok := fields[e.Name]
  1659. if !ok {
  1660. f = k.FieldByName(e.Name)
  1661. }
  1662. var data []byte
  1663. if f.IsValid() && f.CanInterface() {
  1664. var err error
  1665. data, err = Marshal(e.Type, f.Interface())
  1666. if err != nil {
  1667. return nil, err
  1668. }
  1669. }
  1670. buf = appendBytes(buf, data)
  1671. }
  1672. return buf, nil
  1673. }
  1674. func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
  1675. switch v := value.(type) {
  1676. case Unmarshaler:
  1677. return v.UnmarshalCQL(info, data)
  1678. case UDTUnmarshaler:
  1679. udt := info.(UDTTypeInfo)
  1680. for _, e := range udt.Elements {
  1681. if len(data) == 0 {
  1682. return nil
  1683. }
  1684. size := readInt(data[:4])
  1685. data = data[4:]
  1686. var err error
  1687. if size < 0 {
  1688. err = v.UnmarshalUDT(e.Name, e.Type, nil)
  1689. } else {
  1690. err = v.UnmarshalUDT(e.Name, e.Type, data[:size])
  1691. data = data[size:]
  1692. }
  1693. if err != nil {
  1694. return err
  1695. }
  1696. }
  1697. return nil
  1698. case *map[string]interface{}:
  1699. udt := info.(UDTTypeInfo)
  1700. rv := reflect.ValueOf(value)
  1701. if rv.Kind() != reflect.Ptr {
  1702. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1703. }
  1704. rv = rv.Elem()
  1705. t := rv.Type()
  1706. if t.Kind() != reflect.Map {
  1707. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1708. } else if data == nil {
  1709. rv.Set(reflect.Zero(t))
  1710. return nil
  1711. }
  1712. rv.Set(reflect.MakeMap(t))
  1713. m := *v
  1714. for _, e := range udt.Elements {
  1715. if len(data) == 0 {
  1716. return nil
  1717. }
  1718. size := readInt(data[:4])
  1719. data = data[4:]
  1720. val := reflect.New(goType(e.Type))
  1721. var err error
  1722. if size < 0 {
  1723. err = Unmarshal(e.Type, nil, val.Interface())
  1724. } else {
  1725. err = Unmarshal(e.Type, data[:size], val.Interface())
  1726. data = data[size:]
  1727. }
  1728. if err != nil {
  1729. return err
  1730. }
  1731. m[e.Name] = val.Elem().Interface()
  1732. }
  1733. return nil
  1734. }
  1735. k := reflect.ValueOf(value).Elem()
  1736. if k.Kind() != reflect.Struct || !k.IsValid() {
  1737. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1738. }
  1739. fields := make(map[string]reflect.Value)
  1740. t := k.Type()
  1741. for i := 0; i < t.NumField(); i++ {
  1742. sf := t.Field(i)
  1743. if tag := sf.Tag.Get("cql"); tag != "" {
  1744. fields[tag] = k.Field(i)
  1745. }
  1746. }
  1747. if len(data) == 0 {
  1748. if k.CanSet() {
  1749. k.Set(reflect.Zero(k.Type()))
  1750. }
  1751. return nil
  1752. }
  1753. udt := info.(UDTTypeInfo)
  1754. for _, e := range udt.Elements {
  1755. if len(data) < 4 {
  1756. // UDT def does not match the column value
  1757. return nil
  1758. }
  1759. size := readInt(data[:4])
  1760. data = data[4:]
  1761. var err error
  1762. if size >= 0 {
  1763. f, ok := fields[e.Name]
  1764. if !ok {
  1765. f = k.FieldByName(e.Name)
  1766. }
  1767. if !f.IsValid() || !f.CanAddr() {
  1768. return unmarshalErrorf("cannot unmarshal %s into %T: field %v is not valid", info, value, e.Name)
  1769. }
  1770. fk := f.Addr().Interface()
  1771. if err := Unmarshal(e.Type, data[:size], fk); err != nil {
  1772. return err
  1773. }
  1774. data = data[size:]
  1775. }
  1776. if err != nil {
  1777. return err
  1778. }
  1779. }
  1780. return nil
  1781. }
  1782. // TypeInfo describes a Cassandra specific data type.
  1783. type TypeInfo interface {
  1784. Type() Type
  1785. Version() byte
  1786. Custom() string
  1787. // New creates a pointer to an empty version of whatever type
  1788. // is referenced by the TypeInfo receiver
  1789. New() interface{}
  1790. }
  1791. type NativeType struct {
  1792. proto byte
  1793. typ Type
  1794. custom string // only used for TypeCustom
  1795. }
  1796. func NewNativeType(proto byte, typ Type, custom string) NativeType {
  1797. return NativeType{proto, typ, custom}
  1798. }
  1799. func (t NativeType) New() interface{} {
  1800. return reflect.New(goType(t)).Interface()
  1801. }
  1802. func (s NativeType) Type() Type {
  1803. return s.typ
  1804. }
  1805. func (s NativeType) Version() byte {
  1806. return s.proto
  1807. }
  1808. func (s NativeType) Custom() string {
  1809. return s.custom
  1810. }
  1811. func (s NativeType) String() string {
  1812. switch s.typ {
  1813. case TypeCustom:
  1814. return fmt.Sprintf("%s(%s)", s.typ, s.custom)
  1815. default:
  1816. return s.typ.String()
  1817. }
  1818. }
  1819. type CollectionType struct {
  1820. NativeType
  1821. Key TypeInfo // only used for TypeMap
  1822. Elem TypeInfo // only used for TypeMap, TypeList and TypeSet
  1823. }
  1824. func (t CollectionType) New() interface{} {
  1825. return reflect.New(goType(t)).Interface()
  1826. }
  1827. func (c CollectionType) String() string {
  1828. switch c.typ {
  1829. case TypeMap:
  1830. return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem)
  1831. case TypeList, TypeSet:
  1832. return fmt.Sprintf("%s(%s)", c.typ, c.Elem)
  1833. case TypeCustom:
  1834. return fmt.Sprintf("%s(%s)", c.typ, c.custom)
  1835. default:
  1836. return c.typ.String()
  1837. }
  1838. }
  1839. type TupleTypeInfo struct {
  1840. NativeType
  1841. Elems []TypeInfo
  1842. }
  1843. func (t TupleTypeInfo) New() interface{} {
  1844. return reflect.New(goType(t)).Interface()
  1845. }
  1846. type UDTField struct {
  1847. Name string
  1848. Type TypeInfo
  1849. }
  1850. type UDTTypeInfo struct {
  1851. NativeType
  1852. KeySpace string
  1853. Name string
  1854. Elements []UDTField
  1855. }
  1856. func (u UDTTypeInfo) New() interface{} {
  1857. return reflect.New(goType(u)).Interface()
  1858. }
  1859. func (u UDTTypeInfo) String() string {
  1860. buf := &bytes.Buffer{}
  1861. fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name)
  1862. first := true
  1863. for _, e := range u.Elements {
  1864. if !first {
  1865. fmt.Fprint(buf, ",")
  1866. } else {
  1867. first = false
  1868. }
  1869. fmt.Fprintf(buf, "%s=%v", e.Name, e.Type)
  1870. }
  1871. fmt.Fprint(buf, "}")
  1872. return buf.String()
  1873. }
  1874. // String returns a human readable name for the Cassandra datatype
  1875. // described by t.
  1876. // Type is the identifier of a Cassandra internal datatype.
  1877. type Type int
  1878. const (
  1879. TypeCustom Type = 0x0000
  1880. TypeAscii Type = 0x0001
  1881. TypeBigInt Type = 0x0002
  1882. TypeBlob Type = 0x0003
  1883. TypeBoolean Type = 0x0004
  1884. TypeCounter Type = 0x0005
  1885. TypeDecimal Type = 0x0006
  1886. TypeDouble Type = 0x0007
  1887. TypeFloat Type = 0x0008
  1888. TypeInt Type = 0x0009
  1889. TypeText Type = 0x000A
  1890. TypeTimestamp Type = 0x000B
  1891. TypeUUID Type = 0x000C
  1892. TypeVarchar Type = 0x000D
  1893. TypeVarint Type = 0x000E
  1894. TypeTimeUUID Type = 0x000F
  1895. TypeInet Type = 0x0010
  1896. TypeDate Type = 0x0011
  1897. TypeTime Type = 0x0012
  1898. TypeSmallInt Type = 0x0013
  1899. TypeTinyInt Type = 0x0014
  1900. TypeList Type = 0x0020
  1901. TypeMap Type = 0x0021
  1902. TypeSet Type = 0x0022
  1903. TypeUDT Type = 0x0030
  1904. TypeTuple Type = 0x0031
  1905. )
  1906. // String returns the name of the identifier.
  1907. func (t Type) String() string {
  1908. switch t {
  1909. case TypeCustom:
  1910. return "custom"
  1911. case TypeAscii:
  1912. return "ascii"
  1913. case TypeBigInt:
  1914. return "bigint"
  1915. case TypeBlob:
  1916. return "blob"
  1917. case TypeBoolean:
  1918. return "boolean"
  1919. case TypeCounter:
  1920. return "counter"
  1921. case TypeDecimal:
  1922. return "decimal"
  1923. case TypeDouble:
  1924. return "double"
  1925. case TypeFloat:
  1926. return "float"
  1927. case TypeInt:
  1928. return "int"
  1929. case TypeText:
  1930. return "text"
  1931. case TypeTimestamp:
  1932. return "timestamp"
  1933. case TypeUUID:
  1934. return "uuid"
  1935. case TypeVarchar:
  1936. return "varchar"
  1937. case TypeTimeUUID:
  1938. return "timeuuid"
  1939. case TypeInet:
  1940. return "inet"
  1941. case TypeDate:
  1942. return "date"
  1943. case TypeTime:
  1944. return "time"
  1945. case TypeSmallInt:
  1946. return "smallint"
  1947. case TypeTinyInt:
  1948. return "tinyint"
  1949. case TypeList:
  1950. return "list"
  1951. case TypeMap:
  1952. return "map"
  1953. case TypeSet:
  1954. return "set"
  1955. case TypeVarint:
  1956. return "varint"
  1957. case TypeTuple:
  1958. return "tuple"
  1959. default:
  1960. return fmt.Sprintf("unknown_type_%d", t)
  1961. }
  1962. }
  1963. type MarshalError string
  1964. func (m MarshalError) Error() string {
  1965. return string(m)
  1966. }
  1967. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1968. return MarshalError(fmt.Sprintf(format, args...))
  1969. }
  1970. type UnmarshalError string
  1971. func (m UnmarshalError) Error() string {
  1972. return string(m)
  1973. }
  1974. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1975. return UnmarshalError(fmt.Sprintf(format, args...))
  1976. }