Skip to content

Commit 15512a5

Browse files
author
iwysiu
authored
Change codecs to convert between numbers and bools (#244)
GODRIVER-1357 GODRIVER-1358 GODRIVER-1359 GODRIVER-1360
1 parent c548ef5 commit 15512a5

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

bson/bsoncodec/default_value_decoders.go

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,41 @@ func (dvd DefaultValueDecoders) RegisterDefaultDecoders(rb *RegistryBuilder) {
105105

106106
// BooleanDecodeValue is the ValueDecoderFunc for bool types.
107107
func (dvd DefaultValueDecoders) BooleanDecodeValue(dctx DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
108-
if vr.Type() != bsontype.Boolean {
109-
return fmt.Errorf("cannot decode %v into a boolean", vr.Type())
110-
}
111108
if !val.IsValid() || !val.CanSet() || val.Kind() != reflect.Bool {
112109
return ValueDecoderError{Name: "BooleanDecodeValue", Kinds: []reflect.Kind{reflect.Bool}, Received: val}
113110
}
114111

115-
b, err := vr.ReadBoolean()
112+
var b bool
113+
var err error
114+
switch vr.Type() {
115+
case bsontype.Int32:
116+
i32, err := vr.ReadInt32()
117+
if err != nil {
118+
return err
119+
}
120+
b = (i32 != 0)
121+
case bsontype.Int64:
122+
i64, err := vr.ReadInt64()
123+
if err != nil {
124+
return err
125+
}
126+
b = (i64 != 0)
127+
case bsontype.Double:
128+
f64, err := vr.ReadDouble()
129+
if err != nil {
130+
return err
131+
}
132+
b = (f64 != 0)
133+
case bsontype.Boolean:
134+
b, err = vr.ReadBoolean()
135+
if err != nil {
136+
return err
137+
}
138+
default:
139+
return fmt.Errorf("cannot decode %v into a boolean", vr.Type())
140+
}
116141
val.SetBool(b)
117-
return err
142+
return nil
118143
}
119144

120145
// IntDecodeValue is the ValueDecoderFunc for int types.
@@ -145,6 +170,14 @@ func (dvd DefaultValueDecoders) IntDecodeValue(dc DecodeContext, vr bsonrw.Value
145170
return fmt.Errorf("%g overflows int64", f64)
146171
}
147172
i64 = int64(f64)
173+
case bsontype.Boolean:
174+
b, err := vr.ReadBoolean()
175+
if err != nil {
176+
return err
177+
}
178+
if b {
179+
i64 = 1
180+
}
148181
default:
149182
return fmt.Errorf("cannot decode %v into an integer type", vr.Type())
150183
}
@@ -215,6 +248,14 @@ func (dvd DefaultValueDecoders) UintDecodeValue(dc DecodeContext, vr bsonrw.Valu
215248
return fmt.Errorf("%g overflows int64", f64)
216249
}
217250
i64 = int64(f64)
251+
case bsontype.Boolean:
252+
b, err := vr.ReadBoolean()
253+
if err != nil {
254+
return err
255+
}
256+
if b {
257+
i64 = 1
258+
}
218259
default:
219260
return fmt.Errorf("cannot decode %v into an integer type", vr.Type())
220261
}
@@ -282,6 +323,14 @@ func (dvd DefaultValueDecoders) FloatDecodeValue(ec DecodeContext, vr bsonrw.Val
282323
if err != nil {
283324
return err
284325
}
326+
case bsontype.Boolean:
327+
b, err := vr.ReadBoolean()
328+
if err != nil {
329+
return err
330+
}
331+
if b {
332+
f = 1
333+
}
285334
default:
286335
return fmt.Errorf("cannot decode %v into a float32 or float64 type", vr.Type())
287336
}
@@ -555,7 +604,7 @@ func (dvd DefaultValueDecoders) JSONNumberDecodeValue(dc DecodeContext, vr bsonr
555604
if err != nil {
556605
return err
557606
}
558-
val.Set(reflect.ValueOf(json.Number(strconv.FormatFloat(f64, 'g', -1, 64))))
607+
val.Set(reflect.ValueOf(json.Number(strconv.FormatFloat(f64, 'f', -1, 64))))
559608
case bsontype.Int32:
560609
i32, err := vr.ReadInt32()
561610
if err != nil {

bson/mgocompat/bson_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,19 +1334,19 @@ var twoWayCrossItems = []crossTypeItem{
13341334
{&struct{ I int }{42}, &struct{ I float64 }{42}},
13351335

13361336
// int <=> bool
1337-
// {&struct{ I int }{1}, &struct{ I bool }{true}},
1338-
// {&struct{ I int }{0}, &struct{ I bool }{false}},
1337+
{&struct{ I int }{1}, &struct{ I bool }{true}},
1338+
{&struct{ I int }{0}, &struct{ I bool }{false}},
13391339

13401340
// uint <=> float64
13411341
{&struct{ I uint }{42}, &struct{ I float64 }{42}},
13421342

13431343
// uint <=> bool
1344-
// {&struct{ I uint }{1}, &struct{ I bool }{true}},
1345-
// {&struct{ I uint }{0}, &struct{ I bool }{false}},
1344+
{&struct{ I uint }{1}, &struct{ I bool }{true}},
1345+
{&struct{ I uint }{0}, &struct{ I bool }{false}},
13461346

13471347
// float64 <=> bool
1348-
// {&struct{ I float64 }{1}, &struct{ I bool }{true}},
1349-
// {&struct{ I float64 }{0}, &struct{ I bool }{false}},
1348+
{&struct{ I float64 }{1}, &struct{ I bool }{true}},
1349+
{&struct{ I float64 }{0}, &struct{ I bool }{false}},
13501350

13511351
// string <=> string and string <=> []byte
13521352
// {&struct{ S []byte }{[]byte("abc")}, &struct{ S string }{"abc"}},
@@ -1366,17 +1366,17 @@ var twoWayCrossItems = []crossTypeItem{
13661366
// {&struct{ A []byte }{[]byte("abc")}, map[string]string{"a": "abc"}},
13671367
{&struct{ A uint }{42}, map[string]int{"a": 42}},
13681368
{&struct{ A uint }{42}, map[string]float64{"a": 42}},
1369-
// {&struct{ A uint }{1}, map[string]bool{"a": true}},
1369+
{&struct{ A uint }{1}, map[string]bool{"a": true}},
13701370
{&struct{ A int }{42}, map[string]uint{"a": 42}},
13711371
{&struct{ A int }{42}, map[string]float64{"a": 42}},
1372-
// {&struct{ A int }{1}, map[string]bool{"a": true}},
1372+
{&struct{ A int }{1}, map[string]bool{"a": true}},
13731373
{&struct{ A float64 }{42}, map[string]float32{"a": 42}},
13741374
{&struct{ A float64 }{42}, map[string]int{"a": 42}},
13751375
{&struct{ A float64 }{42}, map[string]uint{"a": 42}},
1376-
// {&struct{ A float64 }{1}, map[string]bool{"a": true}},
1377-
// {&struct{ A bool }{true}, map[string]int{"a": 1}},
1378-
// {&struct{ A bool }{true}, map[string]uint{"a": 1}},
1379-
// {&struct{ A bool }{true}, map[string]float64{"a": 1}},
1376+
{&struct{ A float64 }{1}, map[string]bool{"a": true}},
1377+
{&struct{ A bool }{true}, map[string]int{"a": 1}},
1378+
{&struct{ A bool }{true}, map[string]uint{"a": 1}},
1379+
{&struct{ A bool }{true}, map[string]float64{"a": 1}},
13801380
{&struct{ A **byte }{&byteptr}, map[string]byte{"a": 8}},
13811381

13821382
// url.URL <=> string
@@ -1483,7 +1483,7 @@ var twoWayCrossItems = []crossTypeItem{
14831483
// json.Number <=> int64, float64
14841484
{&struct{ N json.Number }{"5"}, map[string]interface{}{"n": int64(5)}},
14851485
{&struct{ N json.Number }{"5.05"}, map[string]interface{}{"n": 5.05}},
1486-
// {&struct{ N json.Number }{"9223372036854776000"}, map[string]interface{}{"n": float64(1 << 63)}},
1486+
{&struct{ N json.Number }{"9223372036854776000"}, map[string]interface{}{"n": float64(1 << 63)}},
14871487

14881488
// bson.D <=> non-struct getter/setter
14891489
// {&bson.D{{"a", 1}}, &getterSetterD{{"a", 1}, {"suffix", true}}},

0 commit comments

Comments
 (0)