Skip to content

Commit 83d7a3a

Browse files
committed
Add non panicking BinaryOK function to bson
Add method to retrieve binary data without hazard of panicking. GODRIVER-539 Signed-off-by: Tobias Kohlbau <[email protected]>
1 parent 3e1dd84 commit 83d7a3a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

bson/value.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,16 @@ func (v *Value) Binary() (subtype byte, data []byte) {
553553
return st, b
554554
}
555555

556+
// BinaryOK is the same as Binary, except it returns a boolean instead of
557+
// panicking.
558+
func (v *Value) BinaryOK() (subtype byte, data []byte, ok bool) {
559+
if v == nil || v.offset == 0 || v.data == nil || Type(v.data[v.start]) != TypeBinary {
560+
return 0x00, nil, false
561+
}
562+
st, b := v.Binary()
563+
return st, b, true
564+
}
565+
556566
// ObjectID returns the BSON objectid value the Value represents. It panics if the value is a BSON
557567
// type other than objectid.
558568
func (v *Value) ObjectID() objectid.ObjectID {

0 commit comments

Comments
 (0)