marshal.go 48 KB

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