From 7340694bf7c62160f8dc178d574981cc17b26893 Mon Sep 17 00:00:00 2001 From: Richard De Avila <70982763+rdeavilafloqast@users.noreply.github.com> Date: Mon, 6 Dec 2021 12:19:39 -0600 Subject: [PATCH] Updated IsIsMasterDoc function to work with drivers that send IsMaster documents with 'isMaster' set to a boolean value instead of a number (#41) --- mongo/command.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mongo/command.go b/mongo/command.go index 940d968..31f6fb8 100644 --- a/mongo/command.go +++ b/mongo/command.go @@ -73,7 +73,17 @@ func CommandAndCollection(msg bsoncore.Document) (Command, string) { } func IsIsMasterDoc(doc bsoncore.Document) bool { - isMaster, _ := doc.Lookup(string(IsMaster)).Int32OK() - ismaster, _ := doc.Lookup(string(Ismaster)).Int32OK() - return ismaster+isMaster > 0 + isMaster := doc.Lookup(string(IsMaster)) + ismaster := doc.Lookup(string(Ismaster)) + return IsIsMasterValueTruthy(isMaster) || IsIsMasterValueTruthy(ismaster) } + +func IsIsMasterValueTruthy(val bsoncore.Value) bool { + if intValue, isInt := val.Int32OK(); intValue > 0 { + return true; + } else if !isInt { + boolValue, isBool := val.BooleanOK() + return boolValue && isBool + } + return false; +} \ No newline at end of file