marshal.go 34 KB

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