marshal.go 36 KB

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