marshal.go 42 KB

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