diff --git a/bson/bson_test.go b/bson/bson_test.go index 5c1b869..630292b 100644 --- a/bson/bson_test.go +++ b/bson/bson_test.go @@ -1056,6 +1056,25 @@ func (i *getterSetterInt) SetBSON(raw bson.Raw) error { return err } +type ifaceType interface { + Hello() +} + +type ifaceSlice []ifaceType + +func (s *ifaceSlice) SetBSON(raw bson.Raw) error { + var ns []int + if err := raw.Unmarshal(&ns); err != nil { + return err + } + *s = make(ifaceSlice, ns[0]) + return nil +} + +func (s ifaceSlice) GetBSON() (interface{}, error) { + return []int{len(s)}, nil +} + type ( MyString string MyBytes []byte @@ -1090,197 +1109,200 @@ func parseURL(s string) *url.URL { // verify that the resulting value is deep-equal to the untouched second value. // Then, it will do the same in the *opposite* direction! var twoWayCrossItems = []crossTypeItem{ - // int<=>int - {&struct{ I int }{42}, &struct{ I int8 }{42}}, - {&struct{ I int }{42}, &struct{ I int32 }{42}}, - {&struct{ I int }{42}, &struct{ I int64 }{42}}, - {&struct{ I int8 }{42}, &struct{ I int32 }{42}}, - {&struct{ I int8 }{42}, &struct{ I int64 }{42}}, - {&struct{ I int32 }{42}, &struct{ I int64 }{42}}, - - // uint<=>uint - {&struct{ I uint }{42}, &struct{ I uint8 }{42}}, - {&struct{ I uint }{42}, &struct{ I uint32 }{42}}, - {&struct{ I uint }{42}, &struct{ I uint64 }{42}}, - {&struct{ I uint8 }{42}, &struct{ I uint32 }{42}}, - {&struct{ I uint8 }{42}, &struct{ I uint64 }{42}}, - {&struct{ I uint32 }{42}, &struct{ I uint64 }{42}}, - - // float32<=>float64 - {&struct{ I float32 }{42}, &struct{ I float64 }{42}}, - - // int<=>uint - {&struct{ I uint }{42}, &struct{ I int }{42}}, - {&struct{ I uint }{42}, &struct{ I int8 }{42}}, - {&struct{ I uint }{42}, &struct{ I int32 }{42}}, - {&struct{ I uint }{42}, &struct{ I int64 }{42}}, - {&struct{ I uint8 }{42}, &struct{ I int }{42}}, - {&struct{ I uint8 }{42}, &struct{ I int8 }{42}}, - {&struct{ I uint8 }{42}, &struct{ I int32 }{42}}, - {&struct{ I uint8 }{42}, &struct{ I int64 }{42}}, - {&struct{ I uint32 }{42}, &struct{ I int }{42}}, - {&struct{ I uint32 }{42}, &struct{ I int8 }{42}}, - {&struct{ I uint32 }{42}, &struct{ I int32 }{42}}, - {&struct{ I uint32 }{42}, &struct{ I int64 }{42}}, - {&struct{ I uint64 }{42}, &struct{ I int }{42}}, - {&struct{ I uint64 }{42}, &struct{ I int8 }{42}}, - {&struct{ I uint64 }{42}, &struct{ I int32 }{42}}, - {&struct{ I uint64 }{42}, &struct{ I int64 }{42}}, - - // int <=> float - {&struct{ I int }{42}, &struct{ I float64 }{42}}, - - // int <=> bool - {&struct{ I int }{1}, &struct{ I bool }{true}}, - {&struct{ I int }{0}, &struct{ I bool }{false}}, - - // uint <=> float64 - {&struct{ I uint }{42}, &struct{ I float64 }{42}}, - - // uint <=> bool - {&struct{ I uint }{1}, &struct{ I bool }{true}}, - {&struct{ I uint }{0}, &struct{ I bool }{false}}, - - // float64 <=> bool - {&struct{ I float64 }{1}, &struct{ I bool }{true}}, - {&struct{ I float64 }{0}, &struct{ I bool }{false}}, - - // string <=> string and string <=> []byte - {&struct{ S []byte }{[]byte("abc")}, &struct{ S string }{"abc"}}, - {&struct{ S []byte }{[]byte("def")}, &struct{ S bson.Symbol }{"def"}}, - {&struct{ S string }{"ghi"}, &struct{ S bson.Symbol }{"ghi"}}, - - // map <=> struct - {&struct { - A struct { - B, C int - } - }{struct{ B, C int }{1, 2}}, - map[string]map[string]int{"a": map[string]int{"b": 1, "c": 2}}}, - - {&struct{ A bson.Symbol }{"abc"}, map[string]string{"a": "abc"}}, - {&struct{ A bson.Symbol }{"abc"}, map[string][]byte{"a": []byte("abc")}}, - {&struct{ A []byte }{[]byte("abc")}, map[string]string{"a": "abc"}}, - {&struct{ A uint }{42}, map[string]int{"a": 42}}, - {&struct{ A uint }{42}, map[string]float64{"a": 42}}, - {&struct{ A uint }{1}, map[string]bool{"a": true}}, - {&struct{ A int }{42}, map[string]uint{"a": 42}}, - {&struct{ A int }{42}, map[string]float64{"a": 42}}, - {&struct{ A int }{1}, map[string]bool{"a": true}}, - {&struct{ A float64 }{42}, map[string]float32{"a": 42}}, - {&struct{ A float64 }{42}, map[string]int{"a": 42}}, - {&struct{ A float64 }{42}, map[string]uint{"a": 42}}, - {&struct{ A float64 }{1}, map[string]bool{"a": true}}, - {&struct{ A bool }{true}, map[string]int{"a": 1}}, - {&struct{ A bool }{true}, map[string]uint{"a": 1}}, - {&struct{ A bool }{true}, map[string]float64{"a": 1}}, - {&struct{ A **byte }{&byteptr}, map[string]byte{"a": 8}}, - - // url.URL <=> string - {&struct{ URL *url.URL }{parseURL("h://e.c/p")}, map[string]string{"url": "h://e.c/p"}}, - {&struct{ URL url.URL }{*parseURL("h://e.c/p")}, map[string]string{"url": "h://e.c/p"}}, - - // Slices - {&struct{ S []int }{[]int{1, 2, 3}}, map[string][]int{"s": []int{1, 2, 3}}}, - {&struct{ S *[]int }{&[]int{1, 2, 3}}, map[string][]int{"s": []int{1, 2, 3}}}, - - // Conditionals - {&condBool{true}, map[string]bool{"v": true}}, - {&condBool{}, map[string]bool{}}, - {&condInt{1}, map[string]int{"v": 1}}, - {&condInt{}, map[string]int{}}, - {&condUInt{1}, map[string]uint{"v": 1}}, - {&condUInt{}, map[string]uint{}}, - {&condFloat{}, map[string]int{}}, - {&condStr{"yo"}, map[string]string{"v": "yo"}}, - {&condStr{}, map[string]string{}}, - {&condStrNS{"yo"}, map[string]string{"v": "yo"}}, - {&condStrNS{}, map[string]string{}}, - {&condSlice{[]string{"yo"}}, map[string][]string{"v": []string{"yo"}}}, - {&condSlice{}, map[string][]string{}}, - {&condMap{map[string]int{"k": 1}}, bson.M{"v": bson.M{"k": 1}}}, - {&condMap{}, map[string][]string{}}, - {&condIface{"yo"}, map[string]string{"v": "yo"}}, - {&condIface{""}, map[string]string{"v": ""}}, - {&condIface{}, map[string]string{}}, - {&condPtr{&truevar}, map[string]bool{"v": true}}, - {&condPtr{&falsevar}, map[string]bool{"v": false}}, - {&condPtr{}, map[string]string{}}, - - {&condTime{time.Unix(123456789, 123e6)}, map[string]time.Time{"v": time.Unix(123456789, 123e6)}}, - {&condTime{}, map[string]string{}}, - - {&condStruct{struct{ A []int }{[]int{1}}}, bson.M{"v": bson.M{"a": []interface{}{1}}}}, - {&condStruct{struct{ A []int }{}}, bson.M{}}, - - {&namedCondStr{"yo"}, map[string]string{"myv": "yo"}}, - {&namedCondStr{}, map[string]string{}}, - - {&shortInt{1}, map[string]interface{}{"v": 1}}, - {&shortInt{1 << 30}, map[string]interface{}{"v": 1 << 30}}, - {&shortInt{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, - {&shortUint{1 << 30}, map[string]interface{}{"v": 1 << 30}}, - {&shortUint{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, - {&shortIface{int64(1) << 31}, map[string]interface{}{"v": int64(1 << 31)}}, - {&shortPtr{int64ptr}, map[string]interface{}{"v": intvar}}, - - {&shortNonEmptyInt{1}, map[string]interface{}{"v": 1}}, - {&shortNonEmptyInt{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, - {&shortNonEmptyInt{}, map[string]interface{}{}}, - - {&inlineInt{struct{ A, B int }{1, 2}}, map[string]interface{}{"a": 1, "b": 2}}, - {&inlineMap{A: 1, M: map[string]interface{}{"b": 2}}, map[string]interface{}{"a": 1, "b": 2}}, - {&inlineMap{A: 1, M: nil}, map[string]interface{}{"a": 1}}, - {&inlineMapInt{A: 1, M: map[string]int{"b": 2}}, map[string]int{"a": 1, "b": 2}}, - {&inlineMapInt{A: 1, M: nil}, map[string]int{"a": 1}}, - {&inlineMapMyM{A: 1, M: MyM{"b": MyM{"c": 3}}}, map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 3}}}, - - // []byte <=> MyBytes - {&struct{ B MyBytes }{[]byte("abc")}, map[string]string{"b": "abc"}}, - {&struct{ B MyBytes }{[]byte{}}, map[string]string{"b": ""}}, - {&struct{ B MyBytes }{}, map[string]bool{}}, - {&struct{ B []byte }{[]byte("abc")}, map[string]MyBytes{"b": []byte("abc")}}, - - // bool <=> MyBool - {&struct{ B MyBool }{true}, map[string]bool{"b": true}}, - {&struct{ B MyBool }{}, map[string]bool{"b": false}}, - {&struct{ B MyBool }{}, map[string]string{}}, - {&struct{ B bool }{}, map[string]MyBool{"b": false}}, - - // arrays - {&struct{ V [2]int }{[...]int{1, 2}}, map[string][2]int{"v": [2]int{1, 2}}}, - - // zero time - {&struct{ V time.Time }{}, map[string]interface{}{"v": time.Time{}}}, - - // zero time + 1 second + 1 millisecond; overflows int64 as nanoseconds - {&struct{ V time.Time }{time.Unix(-62135596799, 1e6).Local()}, - map[string]interface{}{"v": time.Unix(-62135596799, 1e6).Local()}}, - - // bson.D <=> []DocElem - {&bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}, &bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}}, - {&bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}, &MyD{{"a", MyD{{"b", 1}, {"c", 2}}}}}, - {&struct{ V MyD }{MyD{{"a", 1}}}, &bson.D{{"v", bson.D{{"a", 1}}}}}, - - // bson.RawD <=> []RawDocElem - {&bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}, &bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}}, - {&bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}, &MyRawD{{"a", bson.Raw{0x08, []byte{0x01}}}}}, - - // bson.M <=> map - {bson.M{"a": bson.M{"b": 1, "c": 2}}, MyM{"a": MyM{"b": 1, "c": 2}}}, - {bson.M{"a": bson.M{"b": 1, "c": 2}}, map[string]interface{}{"a": map[string]interface{}{"b": 1, "c": 2}}}, - - // bson.M <=> map[MyString] - {bson.M{"a": bson.M{"b": 1, "c": 2}}, map[MyString]interface{}{"a": map[MyString]interface{}{"b": 1, "c": 2}}}, - - // json.Number <=> int64, float64 - {&struct{ N json.Number }{"5"}, map[string]interface{}{"n": int64(5)}}, - {&struct{ N json.Number }{"5.05"}, map[string]interface{}{"n": 5.05}}, - {&struct{ N json.Number }{"9223372036854776000"}, map[string]interface{}{"n": float64(1 << 63)}}, - - // bson.D <=> non-struct getter/setter - {&bson.D{{"a", 1}}, &getterSetterD{{"a", 1}, {"suffix", true}}}, - {&bson.D{{"a", 42}}, &gsintvar}, + //// int<=>int + //{&struct{ I int }{42}, &struct{ I int8 }{42}}, + //{&struct{ I int }{42}, &struct{ I int32 }{42}}, + //{&struct{ I int }{42}, &struct{ I int64 }{42}}, + //{&struct{ I int8 }{42}, &struct{ I int32 }{42}}, + //{&struct{ I int8 }{42}, &struct{ I int64 }{42}}, + //{&struct{ I int32 }{42}, &struct{ I int64 }{42}}, + + //// uint<=>uint + //{&struct{ I uint }{42}, &struct{ I uint8 }{42}}, + //{&struct{ I uint }{42}, &struct{ I uint32 }{42}}, + //{&struct{ I uint }{42}, &struct{ I uint64 }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I uint32 }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I uint64 }{42}}, + //{&struct{ I uint32 }{42}, &struct{ I uint64 }{42}}, + + //// float32<=>float64 + //{&struct{ I float32 }{42}, &struct{ I float64 }{42}}, + + //// int<=>uint + //{&struct{ I uint }{42}, &struct{ I int }{42}}, + //{&struct{ I uint }{42}, &struct{ I int8 }{42}}, + //{&struct{ I uint }{42}, &struct{ I int32 }{42}}, + //{&struct{ I uint }{42}, &struct{ I int64 }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I int }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I int8 }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I int32 }{42}}, + //{&struct{ I uint8 }{42}, &struct{ I int64 }{42}}, + //{&struct{ I uint32 }{42}, &struct{ I int }{42}}, + //{&struct{ I uint32 }{42}, &struct{ I int8 }{42}}, + //{&struct{ I uint32 }{42}, &struct{ I int32 }{42}}, + //{&struct{ I uint32 }{42}, &struct{ I int64 }{42}}, + //{&struct{ I uint64 }{42}, &struct{ I int }{42}}, + //{&struct{ I uint64 }{42}, &struct{ I int8 }{42}}, + //{&struct{ I uint64 }{42}, &struct{ I int32 }{42}}, + //{&struct{ I uint64 }{42}, &struct{ I int64 }{42}}, + + //// int <=> float + //{&struct{ I int }{42}, &struct{ I float64 }{42}}, + + //// int <=> bool + //{&struct{ I int }{1}, &struct{ I bool }{true}}, + //{&struct{ I int }{0}, &struct{ I bool }{false}}, + + //// uint <=> float64 + //{&struct{ I uint }{42}, &struct{ I float64 }{42}}, + + //// uint <=> bool + //{&struct{ I uint }{1}, &struct{ I bool }{true}}, + //{&struct{ I uint }{0}, &struct{ I bool }{false}}, + + //// float64 <=> bool + //{&struct{ I float64 }{1}, &struct{ I bool }{true}}, + //{&struct{ I float64 }{0}, &struct{ I bool }{false}}, + + //// string <=> string and string <=> []byte + //{&struct{ S []byte }{[]byte("abc")}, &struct{ S string }{"abc"}}, + //{&struct{ S []byte }{[]byte("def")}, &struct{ S bson.Symbol }{"def"}}, + //{&struct{ S string }{"ghi"}, &struct{ S bson.Symbol }{"ghi"}}, + + //// map <=> struct + //{&struct { + // A struct { + // B, C int + // } + //}{struct{ B, C int }{1, 2}}, + // map[string]map[string]int{"a": map[string]int{"b": 1, "c": 2}}}, + + //{&struct{ A bson.Symbol }{"abc"}, map[string]string{"a": "abc"}}, + //{&struct{ A bson.Symbol }{"abc"}, map[string][]byte{"a": []byte("abc")}}, + //{&struct{ A []byte }{[]byte("abc")}, map[string]string{"a": "abc"}}, + //{&struct{ A uint }{42}, map[string]int{"a": 42}}, + //{&struct{ A uint }{42}, map[string]float64{"a": 42}}, + //{&struct{ A uint }{1}, map[string]bool{"a": true}}, + //{&struct{ A int }{42}, map[string]uint{"a": 42}}, + //{&struct{ A int }{42}, map[string]float64{"a": 42}}, + //{&struct{ A int }{1}, map[string]bool{"a": true}}, + //{&struct{ A float64 }{42}, map[string]float32{"a": 42}}, + //{&struct{ A float64 }{42}, map[string]int{"a": 42}}, + //{&struct{ A float64 }{42}, map[string]uint{"a": 42}}, + //{&struct{ A float64 }{1}, map[string]bool{"a": true}}, + //{&struct{ A bool }{true}, map[string]int{"a": 1}}, + //{&struct{ A bool }{true}, map[string]uint{"a": 1}}, + //{&struct{ A bool }{true}, map[string]float64{"a": 1}}, + //{&struct{ A **byte }{&byteptr}, map[string]byte{"a": 8}}, + + //// url.URL <=> string + //{&struct{ URL *url.URL }{parseURL("h://e.c/p")}, map[string]string{"url": "h://e.c/p"}}, + //{&struct{ URL url.URL }{*parseURL("h://e.c/p")}, map[string]string{"url": "h://e.c/p"}}, + + //// Slices + //{&struct{ S []int }{[]int{1, 2, 3}}, map[string][]int{"s": []int{1, 2, 3}}}, + //{&struct{ S *[]int }{&[]int{1, 2, 3}}, map[string][]int{"s": []int{1, 2, 3}}}, + + //// Conditionals + //{&condBool{true}, map[string]bool{"v": true}}, + //{&condBool{}, map[string]bool{}}, + //{&condInt{1}, map[string]int{"v": 1}}, + //{&condInt{}, map[string]int{}}, + //{&condUInt{1}, map[string]uint{"v": 1}}, + //{&condUInt{}, map[string]uint{}}, + //{&condFloat{}, map[string]int{}}, + //{&condStr{"yo"}, map[string]string{"v": "yo"}}, + //{&condStr{}, map[string]string{}}, + //{&condStrNS{"yo"}, map[string]string{"v": "yo"}}, + //{&condStrNS{}, map[string]string{}}, + //{&condSlice{[]string{"yo"}}, map[string][]string{"v": []string{"yo"}}}, + //{&condSlice{}, map[string][]string{}}, + //{&condMap{map[string]int{"k": 1}}, bson.M{"v": bson.M{"k": 1}}}, + //{&condMap{}, map[string][]string{}}, + //{&condIface{"yo"}, map[string]string{"v": "yo"}}, + //{&condIface{""}, map[string]string{"v": ""}}, + //{&condIface{}, map[string]string{}}, + //{&condPtr{&truevar}, map[string]bool{"v": true}}, + //{&condPtr{&falsevar}, map[string]bool{"v": false}}, + //{&condPtr{}, map[string]string{}}, + + //{&condTime{time.Unix(123456789, 123e6)}, map[string]time.Time{"v": time.Unix(123456789, 123e6)}}, + //{&condTime{}, map[string]string{}}, + + //{&condStruct{struct{ A []int }{[]int{1}}}, bson.M{"v": bson.M{"a": []interface{}{1}}}}, + //{&condStruct{struct{ A []int }{}}, bson.M{}}, + + //{&namedCondStr{"yo"}, map[string]string{"myv": "yo"}}, + //{&namedCondStr{}, map[string]string{}}, + + //{&shortInt{1}, map[string]interface{}{"v": 1}}, + //{&shortInt{1 << 30}, map[string]interface{}{"v": 1 << 30}}, + //{&shortInt{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, + //{&shortUint{1 << 30}, map[string]interface{}{"v": 1 << 30}}, + //{&shortUint{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, + //{&shortIface{int64(1) << 31}, map[string]interface{}{"v": int64(1 << 31)}}, + //{&shortPtr{int64ptr}, map[string]interface{}{"v": intvar}}, + + //{&shortNonEmptyInt{1}, map[string]interface{}{"v": 1}}, + //{&shortNonEmptyInt{1 << 31}, map[string]interface{}{"v": int64(1 << 31)}}, + //{&shortNonEmptyInt{}, map[string]interface{}{}}, + + //{&inlineInt{struct{ A, B int }{1, 2}}, map[string]interface{}{"a": 1, "b": 2}}, + //{&inlineMap{A: 1, M: map[string]interface{}{"b": 2}}, map[string]interface{}{"a": 1, "b": 2}}, + //{&inlineMap{A: 1, M: nil}, map[string]interface{}{"a": 1}}, + //{&inlineMapInt{A: 1, M: map[string]int{"b": 2}}, map[string]int{"a": 1, "b": 2}}, + //{&inlineMapInt{A: 1, M: nil}, map[string]int{"a": 1}}, + //{&inlineMapMyM{A: 1, M: MyM{"b": MyM{"c": 3}}}, map[string]interface{}{"a": 1, "b": map[string]interface{}{"c": 3}}}, + + //// []byte <=> MyBytes + //{&struct{ B MyBytes }{[]byte("abc")}, map[string]string{"b": "abc"}}, + //{&struct{ B MyBytes }{[]byte{}}, map[string]string{"b": ""}}, + //{&struct{ B MyBytes }{}, map[string]bool{}}, + //{&struct{ B []byte }{[]byte("abc")}, map[string]MyBytes{"b": []byte("abc")}}, + + //// bool <=> MyBool + //{&struct{ B MyBool }{true}, map[string]bool{"b": true}}, + //{&struct{ B MyBool }{}, map[string]bool{"b": false}}, + //{&struct{ B MyBool }{}, map[string]string{}}, + //{&struct{ B bool }{}, map[string]MyBool{"b": false}}, + + //// arrays + //{&struct{ V [2]int }{[...]int{1, 2}}, map[string][2]int{"v": [2]int{1, 2}}}, + + //// zero time + //{&struct{ V time.Time }{}, map[string]interface{}{"v": time.Time{}}}, + + //// zero time + 1 second + 1 millisecond; overflows int64 as nanoseconds + //{&struct{ V time.Time }{time.Unix(-62135596799, 1e6).Local()}, + // map[string]interface{}{"v": time.Unix(-62135596799, 1e6).Local()}}, + + //// bson.D <=> []DocElem + //{&bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}, &bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}}, + //{&bson.D{{"a", bson.D{{"b", 1}, {"c", 2}}}}, &MyD{{"a", MyD{{"b", 1}, {"c", 2}}}}}, + //{&struct{ V MyD }{MyD{{"a", 1}}}, &bson.D{{"v", bson.D{{"a", 1}}}}}, + + //// bson.RawD <=> []RawDocElem + //{&bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}, &bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}}, + //{&bson.RawD{{"a", bson.Raw{0x08, []byte{0x01}}}}, &MyRawD{{"a", bson.Raw{0x08, []byte{0x01}}}}}, + + //// bson.M <=> map + //{bson.M{"a": bson.M{"b": 1, "c": 2}}, MyM{"a": MyM{"b": 1, "c": 2}}}, + //{bson.M{"a": bson.M{"b": 1, "c": 2}}, map[string]interface{}{"a": map[string]interface{}{"b": 1, "c": 2}}}, + + //// bson.M <=> map[MyString] + //{bson.M{"a": bson.M{"b": 1, "c": 2}}, map[MyString]interface{}{"a": map[MyString]interface{}{"b": 1, "c": 2}}}, + + //// json.Number <=> int64, float64 + //{&struct{ N json.Number }{"5"}, map[string]interface{}{"n": int64(5)}}, + //{&struct{ N json.Number }{"5.05"}, map[string]interface{}{"n": 5.05}}, + //{&struct{ N json.Number }{"9223372036854776000"}, map[string]interface{}{"n": float64(1 << 63)}}, + + //// bson.D <=> non-struct getter/setter + //{&bson.D{{"a", 1}}, &getterSetterD{{"a", 1}, {"suffix", true}}}, + //{&bson.D{{"a", 42}}, &gsintvar}, + + // Interface slice setter. + {&struct{ V ifaceSlice }{ifaceSlice{nil, nil}}, bson.M{"v": []interface{}{2}}}, } // Same thing, but only one way (obj1 => obj2). diff --git a/bson/decode.go b/bson/decode.go index 782e933..bdd2e02 100644 --- a/bson/decode.go +++ b/bson/decode.go @@ -1,18 +1,18 @@ // BSON library for Go -// +// // Copyright (c) 2010-2012 - Gustavo Niemeyer -// +// // All rights reserved. // // Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// +// modification, are permitted provided that the following conditions are met: +// // 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. +// list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// +// and/or other materials provided with the distribution. +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -474,6 +474,11 @@ func (d *decoder) readElemTo(out reflect.Value, kind byte) (good bool) { panic("Can't happen. Handled above.") case 0x04: // Array outt := out.Type() + if setterStyle(outt) != setterNone { + // Skip the value so its data is handed to the setter below. + d.dropElem(kind) + break + } for outt.Kind() == reflect.Ptr { outt = outt.Elem() } diff --git a/bson/encode.go b/bson/encode.go index 81a13ad..8599f03 100644 --- a/bson/encode.go +++ b/bson/encode.go @@ -1,18 +1,18 @@ // BSON library for Go -// +// // Copyright (c) 2010-2012 - Gustavo Niemeyer -// +// // All rights reserved. // // Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// +// modification, are permitted provided that the following conditions are met: +// // 1. Redistributions of source code must retain the above copyright notice, this -// list of conditions and the following disclaimer. +// list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// +// and/or other materials provided with the distribution. +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -212,7 +212,7 @@ func (e *encoder) addSlice(v reflect.Value) { return } l := v.Len() - et := v.Type().Elem() + et := v.Type().Elem() if et == typeDocElem { for i := 0; i < l; i++ { elem := v.Index(i).Interface().(DocElem) @@ -415,7 +415,7 @@ func (e *encoder) addElem(name string, v reflect.Value, minSize bool) { case time.Time: // MongoDB handles timestamps as milliseconds. e.addElemName('\x09', name) - e.addInt64(s.Unix() * 1000 + int64(s.Nanosecond() / 1e6)) + e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6)) case url.URL: e.addElemName('\x02', name)