marshal.go 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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 Unmarshaler:
  1077. return v.UnmarshalCQL(info, data)
  1078. case *time.Time:
  1079. if len(data) == 0 {
  1080. *v = time.Time{}
  1081. return nil
  1082. }
  1083. var origin uint32 = 1 << 31
  1084. var current uint32 = binary.BigEndian.Uint32(data)
  1085. timestamp := (int64(current) - int64(origin)) * 86400000
  1086. *v = time.Unix(0, timestamp*int64(time.Millisecond)).In(time.UTC)
  1087. return nil
  1088. }
  1089. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1090. }
  1091. func writeCollectionSize(info CollectionType, n int, buf *bytes.Buffer) error {
  1092. if info.proto > protoVersion2 {
  1093. if n > math.MaxInt32 {
  1094. return marshalErrorf("marshal: collection too large")
  1095. }
  1096. buf.WriteByte(byte(n >> 24))
  1097. buf.WriteByte(byte(n >> 16))
  1098. buf.WriteByte(byte(n >> 8))
  1099. buf.WriteByte(byte(n))
  1100. } else {
  1101. if n > math.MaxUint16 {
  1102. return marshalErrorf("marshal: collection too large")
  1103. }
  1104. buf.WriteByte(byte(n >> 8))
  1105. buf.WriteByte(byte(n))
  1106. }
  1107. return nil
  1108. }
  1109. func marshalList(info TypeInfo, value interface{}) ([]byte, error) {
  1110. listInfo, ok := info.(CollectionType)
  1111. if !ok {
  1112. return nil, marshalErrorf("marshal: can not marshal non collection type into list")
  1113. }
  1114. if value == nil {
  1115. return nil, nil
  1116. }
  1117. rv := reflect.ValueOf(value)
  1118. t := rv.Type()
  1119. k := t.Kind()
  1120. if k == reflect.Slice && rv.IsNil() {
  1121. return nil, nil
  1122. }
  1123. switch k {
  1124. case reflect.Slice, reflect.Array:
  1125. buf := &bytes.Buffer{}
  1126. n := rv.Len()
  1127. if err := writeCollectionSize(listInfo, n, buf); err != nil {
  1128. return nil, err
  1129. }
  1130. for i := 0; i < n; i++ {
  1131. item, err := Marshal(listInfo.Elem, rv.Index(i).Interface())
  1132. if err != nil {
  1133. return nil, err
  1134. }
  1135. if err := writeCollectionSize(listInfo, len(item), buf); err != nil {
  1136. return nil, err
  1137. }
  1138. buf.Write(item)
  1139. }
  1140. return buf.Bytes(), nil
  1141. case reflect.Map:
  1142. elem := t.Elem()
  1143. if elem.Kind() == reflect.Struct && elem.NumField() == 0 {
  1144. rkeys := rv.MapKeys()
  1145. keys := make([]interface{}, len(rkeys))
  1146. for i := 0; i < len(keys); i++ {
  1147. keys[i] = rkeys[i].Interface()
  1148. }
  1149. return marshalList(listInfo, keys)
  1150. }
  1151. }
  1152. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1153. }
  1154. func readCollectionSize(info CollectionType, data []byte) (size, read int) {
  1155. if info.proto > protoVersion2 {
  1156. size = int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 | int(data[3])
  1157. read = 4
  1158. } else {
  1159. size = int(data[0])<<8 | int(data[1])
  1160. read = 2
  1161. }
  1162. return
  1163. }
  1164. func unmarshalList(info TypeInfo, data []byte, value interface{}) error {
  1165. listInfo, ok := info.(CollectionType)
  1166. if !ok {
  1167. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into list")
  1168. }
  1169. rv := reflect.ValueOf(value)
  1170. if rv.Kind() != reflect.Ptr {
  1171. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1172. }
  1173. rv = rv.Elem()
  1174. t := rv.Type()
  1175. k := t.Kind()
  1176. switch k {
  1177. case reflect.Slice, reflect.Array:
  1178. if data == nil {
  1179. if k == reflect.Array {
  1180. return unmarshalErrorf("unmarshal list: can not store nil in array value")
  1181. }
  1182. if rv.IsNil() {
  1183. return nil
  1184. }
  1185. rv.Set(reflect.Zero(t))
  1186. return nil
  1187. }
  1188. if len(data) < 2 {
  1189. return unmarshalErrorf("unmarshal list: unexpected eof")
  1190. }
  1191. n, p := readCollectionSize(listInfo, data)
  1192. data = data[p:]
  1193. if k == reflect.Array {
  1194. if rv.Len() != n {
  1195. return unmarshalErrorf("unmarshal list: array with wrong size")
  1196. }
  1197. } else {
  1198. rv.Set(reflect.MakeSlice(t, n, n))
  1199. }
  1200. for i := 0; i < n; i++ {
  1201. if len(data) < 2 {
  1202. return unmarshalErrorf("unmarshal list: unexpected eof")
  1203. }
  1204. m, p := readCollectionSize(listInfo, data)
  1205. data = data[p:]
  1206. if err := Unmarshal(listInfo.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
  1207. return err
  1208. }
  1209. data = data[m:]
  1210. }
  1211. return nil
  1212. }
  1213. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1214. }
  1215. func marshalMap(info TypeInfo, value interface{}) ([]byte, error) {
  1216. mapInfo, ok := info.(CollectionType)
  1217. if !ok {
  1218. return nil, marshalErrorf("marshal: can not marshal none collection type into map")
  1219. }
  1220. if value == nil {
  1221. return nil, nil
  1222. }
  1223. rv := reflect.ValueOf(value)
  1224. if rv.IsNil() {
  1225. return nil, nil
  1226. }
  1227. t := rv.Type()
  1228. if t.Kind() != reflect.Map {
  1229. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1230. }
  1231. buf := &bytes.Buffer{}
  1232. n := rv.Len()
  1233. if err := writeCollectionSize(mapInfo, n, buf); err != nil {
  1234. return nil, err
  1235. }
  1236. keys := rv.MapKeys()
  1237. for _, key := range keys {
  1238. item, err := Marshal(mapInfo.Key, key.Interface())
  1239. if err != nil {
  1240. return nil, err
  1241. }
  1242. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  1243. return nil, err
  1244. }
  1245. buf.Write(item)
  1246. item, err = Marshal(mapInfo.Elem, rv.MapIndex(key).Interface())
  1247. if err != nil {
  1248. return nil, err
  1249. }
  1250. if err := writeCollectionSize(mapInfo, len(item), buf); err != nil {
  1251. return nil, err
  1252. }
  1253. buf.Write(item)
  1254. }
  1255. return buf.Bytes(), nil
  1256. }
  1257. func unmarshalMap(info TypeInfo, data []byte, value interface{}) error {
  1258. mapInfo, ok := info.(CollectionType)
  1259. if !ok {
  1260. return unmarshalErrorf("unmarshal: can not unmarshal none collection type into map")
  1261. }
  1262. rv := reflect.ValueOf(value)
  1263. if rv.Kind() != reflect.Ptr {
  1264. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1265. }
  1266. rv = rv.Elem()
  1267. t := rv.Type()
  1268. if t.Kind() != reflect.Map {
  1269. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1270. }
  1271. if data == nil {
  1272. rv.Set(reflect.Zero(t))
  1273. return nil
  1274. }
  1275. rv.Set(reflect.MakeMap(t))
  1276. if len(data) < 2 {
  1277. return unmarshalErrorf("unmarshal map: unexpected eof")
  1278. }
  1279. n, p := readCollectionSize(mapInfo, data)
  1280. data = data[p:]
  1281. for i := 0; i < n; i++ {
  1282. if len(data) < 2 {
  1283. return unmarshalErrorf("unmarshal list: unexpected eof")
  1284. }
  1285. m, p := readCollectionSize(mapInfo, data)
  1286. data = data[p:]
  1287. key := reflect.New(t.Key())
  1288. if err := Unmarshal(mapInfo.Key, data[:m], key.Interface()); err != nil {
  1289. return err
  1290. }
  1291. data = data[m:]
  1292. m, p = readCollectionSize(mapInfo, data)
  1293. data = data[p:]
  1294. val := reflect.New(t.Elem())
  1295. if err := Unmarshal(mapInfo.Elem, data[:m], val.Interface()); err != nil {
  1296. return err
  1297. }
  1298. data = data[m:]
  1299. rv.SetMapIndex(key.Elem(), val.Elem())
  1300. }
  1301. return nil
  1302. }
  1303. func marshalUUID(info TypeInfo, value interface{}) ([]byte, error) {
  1304. switch val := value.(type) {
  1305. case UUID:
  1306. return val.Bytes(), nil
  1307. case []byte:
  1308. if len(val) != 16 {
  1309. return nil, marshalErrorf("can not marshal []byte %d bytes long into %s, must be exactly 16 bytes long", len(val), info)
  1310. }
  1311. return val, nil
  1312. case string:
  1313. b, err := ParseUUID(val)
  1314. if err != nil {
  1315. return nil, err
  1316. }
  1317. return b[:], nil
  1318. }
  1319. if value == nil {
  1320. return nil, nil
  1321. }
  1322. return nil, marshalErrorf("can not marshal %T into %s", value, info)
  1323. }
  1324. func unmarshalUUID(info TypeInfo, data []byte, value interface{}) error {
  1325. if data == nil || len(data) == 0 {
  1326. switch v := value.(type) {
  1327. case *string:
  1328. *v = ""
  1329. case *[]byte:
  1330. *v = nil
  1331. case *UUID:
  1332. *v = UUID{}
  1333. default:
  1334. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1335. }
  1336. return nil
  1337. }
  1338. u, err := UUIDFromBytes(data)
  1339. if err != nil {
  1340. return unmarshalErrorf("Unable to parse UUID: %s", err)
  1341. }
  1342. switch v := value.(type) {
  1343. case *string:
  1344. *v = u.String()
  1345. return nil
  1346. case *[]byte:
  1347. *v = u[:]
  1348. return nil
  1349. case *UUID:
  1350. *v = u
  1351. return nil
  1352. }
  1353. return unmarshalErrorf("can not unmarshal X %s into %T", info, value)
  1354. }
  1355. func unmarshalTimeUUID(info TypeInfo, data []byte, value interface{}) error {
  1356. switch v := value.(type) {
  1357. case Unmarshaler:
  1358. return v.UnmarshalCQL(info, data)
  1359. case *time.Time:
  1360. id, err := UUIDFromBytes(data)
  1361. if err != nil {
  1362. return err
  1363. } else if id.Version() != 1 {
  1364. return unmarshalErrorf("invalid timeuuid")
  1365. }
  1366. *v = id.Time()
  1367. return nil
  1368. default:
  1369. return unmarshalUUID(info, data, value)
  1370. }
  1371. }
  1372. func marshalInet(info TypeInfo, value interface{}) ([]byte, error) {
  1373. // we return either the 4 or 16 byte representation of an
  1374. // ip address here otherwise the db value will be prefixed
  1375. // with the remaining byte values e.g. ::ffff:127.0.0.1 and not 127.0.0.1
  1376. switch val := value.(type) {
  1377. case net.IP:
  1378. t := val.To4()
  1379. if t == nil {
  1380. return val.To16(), nil
  1381. }
  1382. return t, nil
  1383. case string:
  1384. b := net.ParseIP(val)
  1385. if b != nil {
  1386. t := b.To4()
  1387. if t == nil {
  1388. return b.To16(), nil
  1389. }
  1390. return t, nil
  1391. }
  1392. return nil, marshalErrorf("cannot marshal. invalid ip string %s", val)
  1393. }
  1394. if value == nil {
  1395. return nil, nil
  1396. }
  1397. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1398. }
  1399. func unmarshalInet(info TypeInfo, data []byte, value interface{}) error {
  1400. switch v := value.(type) {
  1401. case Unmarshaler:
  1402. return v.UnmarshalCQL(info, data)
  1403. case *net.IP:
  1404. if x := len(data); !(x == 4 || x == 16) {
  1405. return unmarshalErrorf("cannot unmarshal %s into %T: invalid sized IP: got %d bytes not 4 or 16", info, value, x)
  1406. }
  1407. buf := copyBytes(data)
  1408. ip := net.IP(buf)
  1409. if v4 := ip.To4(); v4 != nil {
  1410. *v = v4
  1411. return nil
  1412. }
  1413. *v = ip
  1414. return nil
  1415. case *string:
  1416. if len(data) == 0 {
  1417. *v = ""
  1418. return nil
  1419. }
  1420. ip := net.IP(data)
  1421. if v4 := ip.To4(); v4 != nil {
  1422. *v = v4.String()
  1423. return nil
  1424. }
  1425. *v = ip.String()
  1426. return nil
  1427. }
  1428. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1429. }
  1430. func marshalTuple(info TypeInfo, value interface{}) ([]byte, error) {
  1431. tuple := info.(TupleTypeInfo)
  1432. switch v := value.(type) {
  1433. case []interface{}:
  1434. var buf []byte
  1435. if len(v) != len(tuple.Elems) {
  1436. return nil, unmarshalErrorf("cannont marshal tuple: wrong number of elements")
  1437. }
  1438. for i, elem := range v {
  1439. data, err := Marshal(tuple.Elems[i], elem)
  1440. if err != nil {
  1441. return nil, err
  1442. }
  1443. n := len(data)
  1444. buf = appendInt(buf, int32(n))
  1445. buf = append(buf, data...)
  1446. }
  1447. return buf, nil
  1448. }
  1449. return nil, unmarshalErrorf("cannot marshal %T into %s", value, tuple)
  1450. }
  1451. // currently only support unmarshal into a list of values, this makes it possible
  1452. // to support tuples without changing the query API. In the future this can be extend
  1453. // to allow unmarshalling into custom tuple types.
  1454. func unmarshalTuple(info TypeInfo, data []byte, value interface{}) error {
  1455. if v, ok := value.(Unmarshaler); ok {
  1456. return v.UnmarshalCQL(info, data)
  1457. }
  1458. tuple := info.(TupleTypeInfo)
  1459. switch v := value.(type) {
  1460. case []interface{}:
  1461. for i, elem := range tuple.Elems {
  1462. // each element inside data is a [bytes]
  1463. size := readInt(data)
  1464. data = data[4:]
  1465. err := Unmarshal(elem, data[:size], v[i])
  1466. if err != nil {
  1467. return err
  1468. }
  1469. data = data[size:]
  1470. }
  1471. return nil
  1472. }
  1473. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1474. }
  1475. // UDTMarshaler is an interface which should be implemented by users wishing to
  1476. // handle encoding UDT types to sent to Cassandra. Note: due to current implentations
  1477. // methods defined for this interface must be value receivers not pointer receivers.
  1478. type UDTMarshaler interface {
  1479. // MarshalUDT will be called for each field in the the UDT returned by Cassandra,
  1480. // the implementor should marshal the type to return by for example calling
  1481. // Marshal.
  1482. MarshalUDT(name string, info TypeInfo) ([]byte, error)
  1483. }
  1484. // UDTUnmarshaler should be implemented by users wanting to implement custom
  1485. // UDT unmarshaling.
  1486. type UDTUnmarshaler interface {
  1487. // UnmarshalUDT will be called for each field in the UDT return by Cassandra,
  1488. // the implementor should unmarshal the data into the value of their chosing,
  1489. // for example by calling Unmarshal.
  1490. UnmarshalUDT(name string, info TypeInfo, data []byte) error
  1491. }
  1492. func marshalUDT(info TypeInfo, value interface{}) ([]byte, error) {
  1493. udt := info.(UDTTypeInfo)
  1494. switch v := value.(type) {
  1495. case Marshaler:
  1496. return v.MarshalCQL(info)
  1497. case UDTMarshaler:
  1498. var buf []byte
  1499. for _, e := range udt.Elements {
  1500. data, err := v.MarshalUDT(e.Name, e.Type)
  1501. if err != nil {
  1502. return nil, err
  1503. }
  1504. buf = appendBytes(buf, data)
  1505. }
  1506. return buf, nil
  1507. case map[string]interface{}:
  1508. var buf []byte
  1509. for _, e := range udt.Elements {
  1510. val, ok := v[e.Name]
  1511. if !ok {
  1512. continue
  1513. }
  1514. data, err := Marshal(e.Type, val)
  1515. if err != nil {
  1516. return nil, err
  1517. }
  1518. buf = appendBytes(buf, data)
  1519. }
  1520. return buf, nil
  1521. }
  1522. k := reflect.ValueOf(value)
  1523. if k.Kind() == reflect.Ptr {
  1524. if k.IsNil() {
  1525. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1526. }
  1527. k = k.Elem()
  1528. }
  1529. if k.Kind() != reflect.Struct || !k.IsValid() {
  1530. return nil, marshalErrorf("cannot marshal %T into %s", value, info)
  1531. }
  1532. fields := make(map[string]reflect.Value)
  1533. t := reflect.TypeOf(value)
  1534. for i := 0; i < t.NumField(); i++ {
  1535. sf := t.Field(i)
  1536. if tag := sf.Tag.Get("cql"); tag != "" {
  1537. fields[tag] = k.Field(i)
  1538. }
  1539. }
  1540. var buf []byte
  1541. for _, e := range udt.Elements {
  1542. f, ok := fields[e.Name]
  1543. if !ok {
  1544. f = k.FieldByName(e.Name)
  1545. }
  1546. var data []byte
  1547. if f.IsValid() && f.CanInterface() {
  1548. var err error
  1549. data, err = Marshal(e.Type, f.Interface())
  1550. if err != nil {
  1551. return nil, err
  1552. }
  1553. }
  1554. buf = appendBytes(buf, data)
  1555. }
  1556. return buf, nil
  1557. }
  1558. func unmarshalUDT(info TypeInfo, data []byte, value interface{}) error {
  1559. switch v := value.(type) {
  1560. case Unmarshaler:
  1561. return v.UnmarshalCQL(info, data)
  1562. case UDTUnmarshaler:
  1563. udt := info.(UDTTypeInfo)
  1564. for _, e := range udt.Elements {
  1565. if len(data) == 0 {
  1566. return nil
  1567. }
  1568. size := readInt(data[:4])
  1569. data = data[4:]
  1570. var err error
  1571. if size < 0 {
  1572. err = v.UnmarshalUDT(e.Name, e.Type, nil)
  1573. } else {
  1574. err = v.UnmarshalUDT(e.Name, e.Type, data[:size])
  1575. data = data[size:]
  1576. }
  1577. if err != nil {
  1578. return err
  1579. }
  1580. }
  1581. return nil
  1582. case *map[string]interface{}:
  1583. udt := info.(UDTTypeInfo)
  1584. rv := reflect.ValueOf(value)
  1585. if rv.Kind() != reflect.Ptr {
  1586. return unmarshalErrorf("can not unmarshal into non-pointer %T", value)
  1587. }
  1588. rv = rv.Elem()
  1589. t := rv.Type()
  1590. if t.Kind() != reflect.Map {
  1591. return unmarshalErrorf("can not unmarshal %s into %T", info, value)
  1592. } else if data == nil {
  1593. rv.Set(reflect.Zero(t))
  1594. return nil
  1595. }
  1596. rv.Set(reflect.MakeMap(t))
  1597. m := *v
  1598. for _, e := range udt.Elements {
  1599. if len(data) == 0 {
  1600. return nil
  1601. }
  1602. size := readInt(data[:4])
  1603. data = data[4:]
  1604. val := reflect.New(goType(e.Type))
  1605. var err error
  1606. if size < 0 {
  1607. err = Unmarshal(e.Type, nil, val.Interface())
  1608. } else {
  1609. err = Unmarshal(e.Type, data[:size], val.Interface())
  1610. data = data[size:]
  1611. }
  1612. if err != nil {
  1613. return err
  1614. }
  1615. m[e.Name] = val.Elem().Interface()
  1616. }
  1617. return nil
  1618. }
  1619. k := reflect.ValueOf(value).Elem()
  1620. if k.Kind() != reflect.Struct || !k.IsValid() {
  1621. return unmarshalErrorf("cannot unmarshal %s into %T", info, value)
  1622. }
  1623. fields := make(map[string]reflect.Value)
  1624. t := k.Type()
  1625. for i := 0; i < t.NumField(); i++ {
  1626. sf := t.Field(i)
  1627. if tag := sf.Tag.Get("cql"); tag != "" {
  1628. fields[tag] = k.Field(i)
  1629. }
  1630. }
  1631. if len(data) == 0 {
  1632. if k.CanSet() {
  1633. k.Set(reflect.Zero(k.Type()))
  1634. }
  1635. return nil
  1636. }
  1637. udt := info.(UDTTypeInfo)
  1638. for _, e := range udt.Elements {
  1639. if len(data) < 4 {
  1640. // UDT def does not match the column value
  1641. return nil
  1642. }
  1643. size := readInt(data[:4])
  1644. data = data[4:]
  1645. var err error
  1646. if size >= 0 {
  1647. f, ok := fields[e.Name]
  1648. if !ok {
  1649. f = k.FieldByName(e.Name)
  1650. }
  1651. if !f.IsValid() || !f.CanAddr() {
  1652. return unmarshalErrorf("cannot unmarshal %s into %T: field %v is not valid", info, value, e.Name)
  1653. }
  1654. fk := f.Addr().Interface()
  1655. if err := Unmarshal(e.Type, data[:size], fk); err != nil {
  1656. return err
  1657. }
  1658. data = data[size:]
  1659. }
  1660. if err != nil {
  1661. return err
  1662. }
  1663. }
  1664. return nil
  1665. }
  1666. // TypeInfo describes a Cassandra specific data type.
  1667. type TypeInfo interface {
  1668. Type() Type
  1669. Version() byte
  1670. Custom() string
  1671. // New creates a pointer to an empty version of whatever type
  1672. // is referenced by the TypeInfo receiver
  1673. New() interface{}
  1674. }
  1675. type NativeType struct {
  1676. proto byte
  1677. typ Type
  1678. custom string // only used for TypeCustom
  1679. }
  1680. func NewNativeType(proto byte, typ Type, custom string) NativeType {
  1681. return NativeType{proto, typ, custom}
  1682. }
  1683. func (t NativeType) New() interface{} {
  1684. return reflect.New(goType(t)).Interface()
  1685. }
  1686. func (s NativeType) Type() Type {
  1687. return s.typ
  1688. }
  1689. func (s NativeType) Version() byte {
  1690. return s.proto
  1691. }
  1692. func (s NativeType) Custom() string {
  1693. return s.custom
  1694. }
  1695. func (s NativeType) String() string {
  1696. switch s.typ {
  1697. case TypeCustom:
  1698. return fmt.Sprintf("%s(%s)", s.typ, s.custom)
  1699. default:
  1700. return s.typ.String()
  1701. }
  1702. }
  1703. type CollectionType struct {
  1704. NativeType
  1705. Key TypeInfo // only used for TypeMap
  1706. Elem TypeInfo // only used for TypeMap, TypeList and TypeSet
  1707. }
  1708. func (t CollectionType) New() interface{} {
  1709. return reflect.New(goType(t)).Interface()
  1710. }
  1711. func (c CollectionType) String() string {
  1712. switch c.typ {
  1713. case TypeMap:
  1714. return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem)
  1715. case TypeList, TypeSet:
  1716. return fmt.Sprintf("%s(%s)", c.typ, c.Elem)
  1717. case TypeCustom:
  1718. return fmt.Sprintf("%s(%s)", c.typ, c.custom)
  1719. default:
  1720. return c.typ.String()
  1721. }
  1722. }
  1723. type TupleTypeInfo struct {
  1724. NativeType
  1725. Elems []TypeInfo
  1726. }
  1727. func (t TupleTypeInfo) New() interface{} {
  1728. return reflect.New(goType(t)).Interface()
  1729. }
  1730. type UDTField struct {
  1731. Name string
  1732. Type TypeInfo
  1733. }
  1734. type UDTTypeInfo struct {
  1735. NativeType
  1736. KeySpace string
  1737. Name string
  1738. Elements []UDTField
  1739. }
  1740. func (u UDTTypeInfo) New() interface{} {
  1741. return reflect.New(goType(u)).Interface()
  1742. }
  1743. func (u UDTTypeInfo) String() string {
  1744. buf := &bytes.Buffer{}
  1745. fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name)
  1746. first := true
  1747. for _, e := range u.Elements {
  1748. if !first {
  1749. fmt.Fprint(buf, ",")
  1750. } else {
  1751. first = false
  1752. }
  1753. fmt.Fprintf(buf, "%s=%v", e.Name, e.Type)
  1754. }
  1755. fmt.Fprint(buf, "}")
  1756. return buf.String()
  1757. }
  1758. // String returns a human readable name for the Cassandra datatype
  1759. // described by t.
  1760. // Type is the identifier of a Cassandra internal datatype.
  1761. type Type int
  1762. const (
  1763. TypeCustom Type = 0x0000
  1764. TypeAscii Type = 0x0001
  1765. TypeBigInt Type = 0x0002
  1766. TypeBlob Type = 0x0003
  1767. TypeBoolean Type = 0x0004
  1768. TypeCounter Type = 0x0005
  1769. TypeDecimal Type = 0x0006
  1770. TypeDouble Type = 0x0007
  1771. TypeFloat Type = 0x0008
  1772. TypeInt Type = 0x0009
  1773. TypeText Type = 0x000A
  1774. TypeTimestamp Type = 0x000B
  1775. TypeUUID Type = 0x000C
  1776. TypeVarchar Type = 0x000D
  1777. TypeVarint Type = 0x000E
  1778. TypeTimeUUID Type = 0x000F
  1779. TypeInet Type = 0x0010
  1780. TypeDate Type = 0x0011
  1781. TypeTime Type = 0x0012
  1782. TypeSmallInt Type = 0x0013
  1783. TypeTinyInt Type = 0x0014
  1784. TypeList Type = 0x0020
  1785. TypeMap Type = 0x0021
  1786. TypeSet Type = 0x0022
  1787. TypeUDT Type = 0x0030
  1788. TypeTuple Type = 0x0031
  1789. )
  1790. // String returns the name of the identifier.
  1791. func (t Type) String() string {
  1792. switch t {
  1793. case TypeCustom:
  1794. return "custom"
  1795. case TypeAscii:
  1796. return "ascii"
  1797. case TypeBigInt:
  1798. return "bigint"
  1799. case TypeBlob:
  1800. return "blob"
  1801. case TypeBoolean:
  1802. return "boolean"
  1803. case TypeCounter:
  1804. return "counter"
  1805. case TypeDecimal:
  1806. return "decimal"
  1807. case TypeDouble:
  1808. return "double"
  1809. case TypeFloat:
  1810. return "float"
  1811. case TypeInt:
  1812. return "int"
  1813. case TypeText:
  1814. return "text"
  1815. case TypeTimestamp:
  1816. return "timestamp"
  1817. case TypeUUID:
  1818. return "uuid"
  1819. case TypeVarchar:
  1820. return "varchar"
  1821. case TypeTimeUUID:
  1822. return "timeuuid"
  1823. case TypeInet:
  1824. return "inet"
  1825. case TypeDate:
  1826. return "date"
  1827. case TypeTime:
  1828. return "time"
  1829. case TypeSmallInt:
  1830. return "smallint"
  1831. case TypeTinyInt:
  1832. return "tinyint"
  1833. case TypeList:
  1834. return "list"
  1835. case TypeMap:
  1836. return "map"
  1837. case TypeSet:
  1838. return "set"
  1839. case TypeVarint:
  1840. return "varint"
  1841. case TypeTuple:
  1842. return "tuple"
  1843. default:
  1844. return fmt.Sprintf("unknown_type_%d", t)
  1845. }
  1846. }
  1847. type MarshalError string
  1848. func (m MarshalError) Error() string {
  1849. return string(m)
  1850. }
  1851. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1852. return MarshalError(fmt.Sprintf(format, args...))
  1853. }
  1854. type UnmarshalError string
  1855. func (m UnmarshalError) Error() string {
  1856. return string(m)
  1857. }
  1858. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1859. return UnmarshalError(fmt.Sprintf(format, args...))
  1860. }