marshal.go 50 KB

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