marshal.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  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 (t NativeType) New() interface{} {
  1632. return reflect.New(goType(t)).Interface()
  1633. }
  1634. func (s NativeType) Type() Type {
  1635. return s.typ
  1636. }
  1637. func (s NativeType) Version() byte {
  1638. return s.proto
  1639. }
  1640. func (s NativeType) Custom() string {
  1641. return s.custom
  1642. }
  1643. func (s NativeType) String() string {
  1644. switch s.typ {
  1645. case TypeCustom:
  1646. return fmt.Sprintf("%s(%s)", s.typ, s.custom)
  1647. default:
  1648. return s.typ.String()
  1649. }
  1650. }
  1651. type CollectionType struct {
  1652. NativeType
  1653. Key TypeInfo // only used for TypeMap
  1654. Elem TypeInfo // only used for TypeMap, TypeList and TypeSet
  1655. }
  1656. func (t CollectionType) New() interface{} {
  1657. return reflect.New(goType(t)).Interface()
  1658. }
  1659. func (c CollectionType) String() string {
  1660. switch c.typ {
  1661. case TypeMap:
  1662. return fmt.Sprintf("%s(%s, %s)", c.typ, c.Key, c.Elem)
  1663. case TypeList, TypeSet:
  1664. return fmt.Sprintf("%s(%s)", c.typ, c.Elem)
  1665. case TypeCustom:
  1666. return fmt.Sprintf("%s(%s)", c.typ, c.custom)
  1667. default:
  1668. return c.typ.String()
  1669. }
  1670. }
  1671. type TupleTypeInfo struct {
  1672. NativeType
  1673. Elems []TypeInfo
  1674. }
  1675. func (t TupleTypeInfo) New() interface{} {
  1676. return reflect.New(goType(t)).Interface()
  1677. }
  1678. type UDTField struct {
  1679. Name string
  1680. Type TypeInfo
  1681. }
  1682. type UDTTypeInfo struct {
  1683. NativeType
  1684. KeySpace string
  1685. Name string
  1686. Elements []UDTField
  1687. }
  1688. func (u UDTTypeInfo) New() interface{} {
  1689. return reflect.New(goType(u)).Interface()
  1690. }
  1691. func (u UDTTypeInfo) String() string {
  1692. buf := &bytes.Buffer{}
  1693. fmt.Fprintf(buf, "%s.%s{", u.KeySpace, u.Name)
  1694. first := true
  1695. for _, e := range u.Elements {
  1696. if !first {
  1697. fmt.Fprint(buf, ",")
  1698. } else {
  1699. first = false
  1700. }
  1701. fmt.Fprintf(buf, "%s=%v", e.Name, e.Type)
  1702. }
  1703. fmt.Fprint(buf, "}")
  1704. return buf.String()
  1705. }
  1706. // String returns a human readable name for the Cassandra datatype
  1707. // described by t.
  1708. // Type is the identifier of a Cassandra internal datatype.
  1709. type Type int
  1710. const (
  1711. TypeCustom Type = 0x0000
  1712. TypeAscii Type = 0x0001
  1713. TypeBigInt Type = 0x0002
  1714. TypeBlob Type = 0x0003
  1715. TypeBoolean Type = 0x0004
  1716. TypeCounter Type = 0x0005
  1717. TypeDecimal Type = 0x0006
  1718. TypeDouble Type = 0x0007
  1719. TypeFloat Type = 0x0008
  1720. TypeInt Type = 0x0009
  1721. TypeText Type = 0x000A
  1722. TypeTimestamp Type = 0x000B
  1723. TypeUUID Type = 0x000C
  1724. TypeVarchar Type = 0x000D
  1725. TypeVarint Type = 0x000E
  1726. TypeTimeUUID Type = 0x000F
  1727. TypeInet Type = 0x0010
  1728. TypeDate Type = 0x0011
  1729. TypeTime Type = 0x0012
  1730. TypeSmallInt Type = 0x0013
  1731. TypeTinyInt Type = 0x0014
  1732. TypeList Type = 0x0020
  1733. TypeMap Type = 0x0021
  1734. TypeSet Type = 0x0022
  1735. TypeUDT Type = 0x0030
  1736. TypeTuple Type = 0x0031
  1737. )
  1738. // String returns the name of the identifier.
  1739. func (t Type) String() string {
  1740. switch t {
  1741. case TypeCustom:
  1742. return "custom"
  1743. case TypeAscii:
  1744. return "ascii"
  1745. case TypeBigInt:
  1746. return "bigint"
  1747. case TypeBlob:
  1748. return "blob"
  1749. case TypeBoolean:
  1750. return "boolean"
  1751. case TypeCounter:
  1752. return "counter"
  1753. case TypeDecimal:
  1754. return "decimal"
  1755. case TypeDouble:
  1756. return "double"
  1757. case TypeFloat:
  1758. return "float"
  1759. case TypeInt:
  1760. return "int"
  1761. case TypeText:
  1762. return "text"
  1763. case TypeTimestamp:
  1764. return "timestamp"
  1765. case TypeUUID:
  1766. return "uuid"
  1767. case TypeVarchar:
  1768. return "varchar"
  1769. case TypeTimeUUID:
  1770. return "timeuuid"
  1771. case TypeInet:
  1772. return "inet"
  1773. case TypeDate:
  1774. return "date"
  1775. case TypeTime:
  1776. return "time"
  1777. case TypeSmallInt:
  1778. return "smallint"
  1779. case TypeTinyInt:
  1780. return "tinyint"
  1781. case TypeList:
  1782. return "list"
  1783. case TypeMap:
  1784. return "map"
  1785. case TypeSet:
  1786. return "set"
  1787. case TypeVarint:
  1788. return "varint"
  1789. case TypeTuple:
  1790. return "tuple"
  1791. default:
  1792. return fmt.Sprintf("unknown_type_%d", t)
  1793. }
  1794. }
  1795. type MarshalError string
  1796. func (m MarshalError) Error() string {
  1797. return string(m)
  1798. }
  1799. func marshalErrorf(format string, args ...interface{}) MarshalError {
  1800. return MarshalError(fmt.Sprintf(format, args...))
  1801. }
  1802. type UnmarshalError string
  1803. func (m UnmarshalError) Error() string {
  1804. return string(m)
  1805. }
  1806. func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
  1807. return UnmarshalError(fmt.Sprintf(format, args...))
  1808. }