Subject: | boolean::true and boolean::false are accepted to put boolean values in, but they are not returned to get boolean values back out |
To place boolean values "true" and "false" into MongoDB using insert,
save, etc., boolean::true and boolean::false can be used.
However, when these boolean values are read back out, they are
translated into 1 and 0, which are functionally equivalent, but somewhat
defeat the purpose of using boolean::true and boolean::false to save the
values in the first place.
To solve this, I have provided the following patch, which will attempt
to call boolean::true or boolean::false appropriately for BSON_BOOL
items returned from the database (and if those fail, hopefully fall back
on the current behavior).
This is my first time writing C to call a Perl function, so comments or
suggestions are very much welcome. All I can say is that this is
working great for me.
Subject: | return-boolean-objects.patch |
diff --git perl_mongo.c perl_mongo.c
index 88c3e1b..021d30b 100644
--- perl_mongo.c
+++ perl_mongo.c
@@ -346,7 +346,29 @@ elem_to_sv (int type, buffer *buf)
}
case BSON_BOOL: {
char d = *buf->pos++;
- value = newSViv(d);
+ int count;
+ dSP;
+
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ PUTBACK;
+ if (d) {
+ count = call_pv("boolean::true", G_SCALAR);
+ }
+ else {
+ count = call_pv("boolean::false", G_SCALAR);
+ }
+ SPAGAIN;
+ if (count == 1)
+ value = newSVsv(POPs);
+
+ if (count != 1 || !SvOK(value)) {
+ value = newSViv(d);
+ }
+
+ PUTBACK;
+ FREETMPS;
break;
}
case BSON_UNDEF: