encode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // BSON library for Go
  2. //
  3. // Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net>
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // 2. Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. // gobson - BSON library for Go.
  27. package bson
  28. import (
  29. "fmt"
  30. "math"
  31. "net/url"
  32. "reflect"
  33. "strconv"
  34. "time"
  35. )
  36. // --------------------------------------------------------------------------
  37. // Some internal infrastructure.
  38. var (
  39. typeBinary = reflect.TypeOf(Binary{})
  40. typeObjectId = reflect.TypeOf(ObjectId(""))
  41. typeSymbol = reflect.TypeOf(Symbol(""))
  42. typeMongoTimestamp = reflect.TypeOf(MongoTimestamp(0))
  43. typeOrderKey = reflect.TypeOf(MinKey)
  44. typeDocElem = reflect.TypeOf(DocElem{})
  45. typeRawDocElem = reflect.TypeOf(RawDocElem{})
  46. typeRaw = reflect.TypeOf(Raw{})
  47. typeURL = reflect.TypeOf(url.URL{})
  48. typeTime = reflect.TypeOf(time.Time{})
  49. typeString = reflect.TypeOf("")
  50. )
  51. const itoaCacheSize = 32
  52. var itoaCache []string
  53. func init() {
  54. itoaCache = make([]string, itoaCacheSize)
  55. for i := 0; i != itoaCacheSize; i++ {
  56. itoaCache[i] = strconv.Itoa(i)
  57. }
  58. }
  59. func itoa(i int) string {
  60. if i < itoaCacheSize {
  61. return itoaCache[i]
  62. }
  63. return strconv.Itoa(i)
  64. }
  65. // --------------------------------------------------------------------------
  66. // Marshaling of the document value itself.
  67. type encoder struct {
  68. out []byte
  69. }
  70. func (e *encoder) addDoc(v reflect.Value) {
  71. for {
  72. if vi, ok := v.Interface().(Getter); ok {
  73. getv, err := vi.GetBSON()
  74. if err != nil {
  75. panic(err)
  76. }
  77. v = reflect.ValueOf(getv)
  78. continue
  79. }
  80. if v.Kind() == reflect.Ptr {
  81. v = v.Elem()
  82. continue
  83. }
  84. break
  85. }
  86. if v.Type() == typeRaw {
  87. raw := v.Interface().(Raw)
  88. if raw.Kind != 0x03 && raw.Kind != 0x00 {
  89. panic("Attempted to unmarshal Raw kind " + strconv.Itoa(int(raw.Kind)) + " as a document")
  90. }
  91. e.addBytes(raw.Data...)
  92. return
  93. }
  94. start := e.reserveInt32()
  95. switch v.Kind() {
  96. case reflect.Map:
  97. e.addMap(v)
  98. case reflect.Struct:
  99. e.addStruct(v)
  100. case reflect.Array, reflect.Slice:
  101. e.addSlice(v)
  102. default:
  103. panic("Can't marshal " + v.Type().String() + " as a BSON document")
  104. }
  105. e.addBytes(0)
  106. e.setInt32(start, int32(len(e.out)-start))
  107. }
  108. func (e *encoder) addMap(v reflect.Value) {
  109. for _, k := range v.MapKeys() {
  110. e.addElem(k.String(), v.MapIndex(k), false)
  111. }
  112. }
  113. func (e *encoder) addStruct(v reflect.Value) {
  114. sinfo, err := getStructInfo(v.Type())
  115. if err != nil {
  116. panic(err)
  117. }
  118. var value reflect.Value
  119. if sinfo.InlineMap >= 0 {
  120. m := v.Field(sinfo.InlineMap)
  121. if m.Len() > 0 {
  122. for _, k := range m.MapKeys() {
  123. ks := k.String()
  124. if _, found := sinfo.FieldsMap[ks]; found {
  125. panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", ks))
  126. }
  127. e.addElem(ks, m.MapIndex(k), false)
  128. }
  129. }
  130. }
  131. for _, info := range sinfo.FieldsList {
  132. if info.Inline == nil {
  133. value = v.Field(info.Num)
  134. } else {
  135. value = v.FieldByIndex(info.Inline)
  136. }
  137. if info.OmitEmpty && isZero(value) {
  138. continue
  139. }
  140. e.addElem(info.Key, value, info.MinSize)
  141. }
  142. }
  143. func isZero(v reflect.Value) bool {
  144. switch v.Kind() {
  145. case reflect.String:
  146. return len(v.String()) == 0
  147. case reflect.Ptr, reflect.Interface:
  148. return v.IsNil()
  149. case reflect.Slice:
  150. return v.Len() == 0
  151. case reflect.Map:
  152. return v.Len() == 0
  153. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  154. return v.Int() == 0
  155. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  156. return v.Uint() == 0
  157. case reflect.Float32, reflect.Float64:
  158. return v.Float() == 0
  159. case reflect.Bool:
  160. return !v.Bool()
  161. case reflect.Struct:
  162. if v.Type() == typeTime {
  163. return v.Interface().(time.Time).IsZero()
  164. }
  165. for i := v.NumField()-1; i >= 0; i-- {
  166. if !isZero(v.Field(i)) {
  167. return false
  168. }
  169. }
  170. return true
  171. }
  172. return false
  173. }
  174. func (e *encoder) addSlice(v reflect.Value) {
  175. vi := v.Interface()
  176. if d, ok := vi.(D); ok {
  177. for _, elem := range d {
  178. e.addElem(elem.Name, reflect.ValueOf(elem.Value), false)
  179. }
  180. return
  181. }
  182. if d, ok := vi.(RawD); ok {
  183. for _, elem := range d {
  184. e.addElem(elem.Name, reflect.ValueOf(elem.Value), false)
  185. }
  186. return
  187. }
  188. l := v.Len()
  189. et := v.Type().Elem()
  190. if et == typeDocElem {
  191. for i := 0; i < l; i++ {
  192. elem := v.Index(i).Interface().(DocElem)
  193. e.addElem(elem.Name, reflect.ValueOf(elem.Value), false)
  194. }
  195. return
  196. }
  197. if et == typeRawDocElem {
  198. for i := 0; i < l; i++ {
  199. elem := v.Index(i).Interface().(RawDocElem)
  200. e.addElem(elem.Name, reflect.ValueOf(elem.Value), false)
  201. }
  202. return
  203. }
  204. for i := 0; i < l; i++ {
  205. e.addElem(itoa(i), v.Index(i), false)
  206. }
  207. }
  208. // --------------------------------------------------------------------------
  209. // Marshaling of elements in a document.
  210. func (e *encoder) addElemName(kind byte, name string) {
  211. e.addBytes(kind)
  212. e.addBytes([]byte(name)...)
  213. e.addBytes(0)
  214. }
  215. func (e *encoder) addElem(name string, v reflect.Value, minSize bool) {
  216. if !v.IsValid() {
  217. e.addElemName('\x0A', name)
  218. return
  219. }
  220. if getter, ok := v.Interface().(Getter); ok {
  221. getv, err := getter.GetBSON()
  222. if err != nil {
  223. panic(err)
  224. }
  225. e.addElem(name, reflect.ValueOf(getv), minSize)
  226. return
  227. }
  228. switch v.Kind() {
  229. case reflect.Interface:
  230. e.addElem(name, v.Elem(), minSize)
  231. case reflect.Ptr:
  232. e.addElem(name, v.Elem(), minSize)
  233. case reflect.String:
  234. s := v.String()
  235. switch v.Type() {
  236. case typeObjectId:
  237. if len(s) != 12 {
  238. panic("ObjectIDs must be exactly 12 bytes long (got " +
  239. strconv.Itoa(len(s)) + ")")
  240. }
  241. e.addElemName('\x07', name)
  242. e.addBytes([]byte(s)...)
  243. case typeSymbol:
  244. e.addElemName('\x0E', name)
  245. e.addStr(s)
  246. default:
  247. e.addElemName('\x02', name)
  248. e.addStr(s)
  249. }
  250. case reflect.Float32, reflect.Float64:
  251. e.addElemName('\x01', name)
  252. e.addInt64(int64(math.Float64bits(v.Float())))
  253. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  254. u := v.Uint()
  255. if int64(u) < 0 {
  256. panic("BSON has no uint64 type, and value is too large to fit correctly in an int64")
  257. } else if u <= math.MaxInt32 && (minSize || v.Kind() <= reflect.Uint32) {
  258. e.addElemName('\x10', name)
  259. e.addInt32(int32(u))
  260. } else {
  261. e.addElemName('\x12', name)
  262. e.addInt64(int64(u))
  263. }
  264. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  265. switch v.Type() {
  266. case typeMongoTimestamp:
  267. e.addElemName('\x11', name)
  268. e.addInt64(v.Int())
  269. case typeOrderKey:
  270. if v.Int() == int64(MaxKey) {
  271. e.addElemName('\x7F', name)
  272. } else {
  273. e.addElemName('\xFF', name)
  274. }
  275. default:
  276. i := v.Int()
  277. if (minSize || v.Type().Kind() != reflect.Int64) && i >= math.MinInt32 && i <= math.MaxInt32 {
  278. // It fits into an int32, encode as such.
  279. e.addElemName('\x10', name)
  280. e.addInt32(int32(i))
  281. } else {
  282. e.addElemName('\x12', name)
  283. e.addInt64(i)
  284. }
  285. }
  286. case reflect.Bool:
  287. e.addElemName('\x08', name)
  288. if v.Bool() {
  289. e.addBytes(1)
  290. } else {
  291. e.addBytes(0)
  292. }
  293. case reflect.Map:
  294. e.addElemName('\x03', name)
  295. e.addDoc(v)
  296. case reflect.Slice:
  297. vt := v.Type()
  298. et := vt.Elem()
  299. if et.Kind() == reflect.Uint8 {
  300. e.addElemName('\x05', name)
  301. e.addBinary('\x00', v.Bytes())
  302. } else if et == typeDocElem || et == typeRawDocElem {
  303. e.addElemName('\x03', name)
  304. e.addDoc(v)
  305. } else {
  306. e.addElemName('\x04', name)
  307. e.addDoc(v)
  308. }
  309. case reflect.Array:
  310. et := v.Type().Elem()
  311. if et.Kind() == reflect.Uint8 {
  312. e.addElemName('\x05', name)
  313. e.addBinary('\x00', v.Slice(0, v.Len()).Interface().([]byte))
  314. } else {
  315. e.addElemName('\x04', name)
  316. e.addDoc(v)
  317. }
  318. case reflect.Struct:
  319. switch s := v.Interface().(type) {
  320. case Raw:
  321. kind := s.Kind
  322. if kind == 0x00 {
  323. kind = 0x03
  324. }
  325. e.addElemName(kind, name)
  326. e.addBytes(s.Data...)
  327. case Binary:
  328. e.addElemName('\x05', name)
  329. e.addBinary(s.Kind, s.Data)
  330. case RegEx:
  331. e.addElemName('\x0B', name)
  332. e.addCStr(s.Pattern)
  333. e.addCStr(s.Options)
  334. case JavaScript:
  335. if s.Scope == nil {
  336. e.addElemName('\x0D', name)
  337. e.addStr(s.Code)
  338. } else {
  339. e.addElemName('\x0F', name)
  340. start := e.reserveInt32()
  341. e.addStr(s.Code)
  342. e.addDoc(reflect.ValueOf(s.Scope))
  343. e.setInt32(start, int32(len(e.out)-start))
  344. }
  345. case time.Time:
  346. // MongoDB handles timestamps as milliseconds.
  347. e.addElemName('\x09', name)
  348. e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6))
  349. case url.URL:
  350. e.addElemName('\x02', name)
  351. e.addStr(s.String())
  352. case undefined:
  353. e.addElemName('\x06', name)
  354. default:
  355. e.addElemName('\x03', name)
  356. e.addDoc(v)
  357. }
  358. default:
  359. panic("Can't marshal " + v.Type().String() + " in a BSON document")
  360. }
  361. }
  362. // --------------------------------------------------------------------------
  363. // Marshaling of base types.
  364. func (e *encoder) addBinary(subtype byte, v []byte) {
  365. if subtype == 0x02 {
  366. // Wonder how that brilliant idea came to life. Obsolete, luckily.
  367. e.addInt32(int32(len(v) + 4))
  368. e.addBytes(subtype)
  369. e.addInt32(int32(len(v)))
  370. } else {
  371. e.addInt32(int32(len(v)))
  372. e.addBytes(subtype)
  373. }
  374. e.addBytes(v...)
  375. }
  376. func (e *encoder) addStr(v string) {
  377. e.addInt32(int32(len(v) + 1))
  378. e.addCStr(v)
  379. }
  380. func (e *encoder) addCStr(v string) {
  381. e.addBytes([]byte(v)...)
  382. e.addBytes(0)
  383. }
  384. func (e *encoder) reserveInt32() (pos int) {
  385. pos = len(e.out)
  386. e.addBytes(0, 0, 0, 0)
  387. return pos
  388. }
  389. func (e *encoder) setInt32(pos int, v int32) {
  390. e.out[pos+0] = byte(v)
  391. e.out[pos+1] = byte(v >> 8)
  392. e.out[pos+2] = byte(v >> 16)
  393. e.out[pos+3] = byte(v >> 24)
  394. }
  395. func (e *encoder) addInt32(v int32) {
  396. u := uint32(v)
  397. e.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24))
  398. }
  399. func (e *encoder) addInt64(v int64) {
  400. u := uint64(v)
  401. e.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24),
  402. byte(u>>32), byte(u>>40), byte(u>>48), byte(u>>56))
  403. }
  404. func (e *encoder) addBytes(v ...byte) {
  405. e.out = append(e.out, v...)
  406. }