Skip to content

Commit

Permalink
incorporate slice unmarshal code from https://bitbucket.org/lavalamp/…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejs committed Jan 12, 2011
1 parent be568cb commit 8df918b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mongo/bson-struct.go
Expand Up @@ -15,6 +15,7 @@ import (
"bytes" "bytes"
"time" "time"
"container/vector" "container/vector"
"strconv"
) )


type structBuilder struct { type structBuilder struct {
Expand Down Expand Up @@ -238,6 +239,26 @@ func (self *structBuilder) Key(k string) Builder {
elem = v.Elem(key) elem = v.Elem(key)
} }
return &structBuilder{val: elem, map_: v, key: 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 return nobuilder
} }
Expand Down

0 comments on commit 8df918b

Please sign in to comment.