Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

incorporate slice unmarshal code from https://bitbucket.org/lavalamp/…

  • Loading branch information...
commit 8df918b788f4b5bcc1fd63e5e525bb0c9e7149d1 1 parent be568cb
@mikejs mikejs authored
Showing with 21 additions and 0 deletions.
  1. +21 −0 mongo/bson-struct.go
View
21 mongo/bson-struct.go
@@ -15,6 +15,7 @@ import (
"bytes"
"time"
"container/vector"
+ "strconv"
)
type structBuilder struct {
@@ -238,6 +239,26 @@ func (self *structBuilder) Key(k string) Builder {
elem = v.Elem(key)
}
return &structBuilder{val: elem, map_: v, key: key}
+ case *reflect.SliceValue:
+ index, err := strconv.Atoi(k)
+ if err != nil {
+ return nobuilder
+ }
+ if index < v.Len() {
+ return &structBuilder{val: v.Elem(index)}
+ }
+ if index < v.Cap() {
+ v.SetLen(index + 1)
+ return &structBuilder{val: v.Elem(index)}
+ }
+ newCap := v.Cap() * 2
+ if index >= newCap {
+ newCap = index*2 + 1
+ }
+ temp := reflect.MakeSlice(v.Type().(*reflect.SliceType), index+1, newCap)
+ reflect.Copy(temp, v)
+ v.Set(temp)
+ return &structBuilder{val: v.Elem(index)}
}
return nobuilder
}
Please sign in to comment.
Something went wrong with that request. Please try again.