marshal.go 55 KB

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