marshal.go 49 KB

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