kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mpe...@apache.org
Subject [1/3] kudu git commit: Add ExtractBool method to JsonReader
Date Tue, 08 May 2018 20:48:27 GMT
Repository: kudu
Updated Branches:
  refs/heads/master e7ee66786 -> d83cd11a9


Add ExtractBool method to JsonReader

This will be used in a follow-up.

Change-Id: I2888fc150aaf3db5db01d6135c9d387db39cc179
Reviewed-on: http://gerrit.cloudera.org:8080/10286
Tested-by: Will Berkeley <wdberkeley@gmail.com>
Reviewed-by: Todd Lipcon <todd@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/fcb71942
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/fcb71942
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/fcb71942

Branch: refs/heads/master
Commit: fcb71942daaef4c8bdd60366d6c7a2930fdad116
Parents: e7ee667
Author: Will Berkeley <wdberkeley@apache.org>
Authored: Tue May 1 13:16:29 2018 -0700
Committer: Will Berkeley <wdberkeley@gmail.com>
Committed: Tue May 8 17:23:42 2018 +0000

----------------------------------------------------------------------
 src/kudu/util/jsonreader-test.cc | 33 ++++++++++++++++++++++++++-------
 src/kudu/util/jsonreader.cc      | 14 ++++++++++++++
 src/kudu/util/jsonreader.h       |  4 ++++
 3 files changed, 44 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/fcb71942/src/kudu/util/jsonreader-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonreader-test.cc b/src/kudu/util/jsonreader-test.cc
index d9bf65b..9f62c31 100644
--- a/src/kudu/util/jsonreader-test.cc
+++ b/src/kudu/util/jsonreader-test.cc
@@ -15,7 +15,8 @@
 // specific language governing permissions and limitations
 // under the License.
 
-#include <cstddef>
+#include "kudu/util/jsonreader.h"
+
 #include <cstdint>
 #include <string>
 #include <vector>
@@ -25,7 +26,6 @@
 
 #include "kudu/gutil/integral_types.h"
 #include "kudu/gutil/strings/substitute.h"
-#include "kudu/util/jsonreader.h"
 #include "kudu/util/status.h"
 #include "kudu/util/test_macros.h"
 
@@ -51,6 +51,7 @@ TEST(JsonReaderTest, Empty) {
   ASSERT_OK(r2.Init());
 
   // Not found.
+  ASSERT_TRUE(r.ExtractBool(r.root(), "foo", nullptr).IsNotFound());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsNotFound());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsNotFound());
   ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsNotFound());
@@ -66,6 +67,7 @@ TEST(JsonReaderTest, Basic) {
   ASSERT_EQ("bar", foo);
 
   // Bad types.
+  ASSERT_TRUE(r.ExtractBool(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObject(r.root(), "foo", nullptr).IsInvalidArgument());
@@ -74,7 +76,8 @@ TEST(JsonReaderTest, Basic) {
 
 TEST(JsonReaderTest, LessBasic) {
   string doc = Substitute(
-      "{ \"small\" : 1, \"big\" : $0, \"null\" : null, \"empty\" : \"\" }", kint64max);
+      "{ \"small\" : 1, \"big\" : $0, \"null\" : null, \"empty\" : \"\", \"bool\" : true
}",
+      kint64max);
   JsonReader r(doc);
   ASSERT_OK(r.Init());
   int32_t small;
@@ -88,26 +91,39 @@ TEST(JsonReaderTest, LessBasic) {
   ASSERT_EQ("", str);
   ASSERT_OK(r.ExtractString(r.root(), "empty", &str));
   ASSERT_EQ("", str);
+  bool b;
+  ASSERT_OK(r.ExtractBool(r.root(), "bool", &b));
+  ASSERT_TRUE(b);
 
   // Bad types.
+  ASSERT_TRUE(r.ExtractBool(r.root(), "small", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractString(r.root(), "small", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObject(r.root(), "small", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObjectArray(r.root(), "small", nullptr).IsInvalidArgument());
 
+  ASSERT_TRUE(r.ExtractBool(r.root(), "big", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "big", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractString(r.root(), "big", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObject(r.root(), "big", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObjectArray(r.root(), "big", nullptr).IsInvalidArgument());
 
+  ASSERT_TRUE(r.ExtractBool(r.root(), "null", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "null", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "null", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObject(r.root(), "null", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObjectArray(r.root(), "null", nullptr).IsInvalidArgument());
 
+  ASSERT_TRUE(r.ExtractBool(r.root(), "empty", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "empty", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "empty", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObject(r.root(), "empty", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractObjectArray(r.root(), "empty", nullptr).IsInvalidArgument());
+
+  ASSERT_TRUE(r.ExtractInt32(r.root(), "bool", nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractInt64(r.root(), "bool", nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractString(r.root(), "bool", nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractObject(r.root(), "bool", nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractObjectArray(r.root(), "bool", nullptr).IsInvalidArgument());
 }
 
 TEST(JsonReaderTest, Objects) {
@@ -123,6 +139,7 @@ TEST(JsonReaderTest, Objects) {
   ASSERT_EQ(1, one);
 
   // Bad types.
+  ASSERT_TRUE(r.ExtractBool(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsInvalidArgument());
@@ -143,10 +160,11 @@ TEST(JsonReaderTest, TopLevelArray) {
   ASSERT_EQ("bar", name);
 
   // Bad types.
-  ASSERT_TRUE(r.ExtractInt32(r.root(), nullptr, NULL).IsInvalidArgument());
-  ASSERT_TRUE(r.ExtractInt64(r.root(), nullptr, NULL).IsInvalidArgument());
-  ASSERT_TRUE(r.ExtractString(r.root(), nullptr, NULL).IsInvalidArgument());
-  ASSERT_TRUE(r.ExtractObject(r.root(), nullptr, NULL).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractBool(r.root(), nullptr, nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractInt32(r.root(), nullptr, nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractInt64(r.root(), nullptr, nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractString(r.root(), nullptr, nullptr).IsInvalidArgument());
+  ASSERT_TRUE(r.ExtractObject(r.root(), nullptr, nullptr).IsInvalidArgument());
 }
 
 TEST(JsonReaderTest, NestedArray) {
@@ -165,6 +183,7 @@ TEST(JsonReaderTest, NestedArray) {
   }
 
   // Bad types.
+  ASSERT_TRUE(r.ExtractBool(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt32(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractInt64(r.root(), "foo", nullptr).IsInvalidArgument());
   ASSERT_TRUE(r.ExtractString(r.root(), "foo", nullptr).IsInvalidArgument());

http://git-wip-us.apache.org/repos/asf/kudu/blob/fcb71942/src/kudu/util/jsonreader.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonreader.cc b/src/kudu/util/jsonreader.cc
index f65a9f3..acbc869 100644
--- a/src/kudu/util/jsonreader.cc
+++ b/src/kudu/util/jsonreader.cc
@@ -42,6 +42,20 @@ Status JsonReader::Init() {
   return Status::OK();
 }
 
+Status JsonReader::ExtractBool(const Value* object,
+                               const char* field,
+                               bool* result) const {
+  const Value* val;
+  RETURN_NOT_OK(ExtractField(object, field, &val));
+  if (PREDICT_FALSE(!val->IsBool())) {
+    return Status::InvalidArgument(Substitute(
+        "Wrong type during field extraction: expected bool but got $0",
+        val->GetType()));
+  }
+  *result = val->GetBool();
+  return Status::OK();
+}
+
 Status JsonReader::ExtractInt32(const Value* object,
                                 const char* field,
                                 int32_t* result) const {

http://git-wip-us.apache.org/repos/asf/kudu/blob/fcb71942/src/kudu/util/jsonreader.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/jsonreader.h b/src/kudu/util/jsonreader.h
index 4799f73..e389b57 100644
--- a/src/kudu/util/jsonreader.h
+++ b/src/kudu/util/jsonreader.h
@@ -48,6 +48,10 @@ class JsonReader {
   // 'field' is NULL, will try to convert 'object' directly into the
   // desire type.
 
+  Status ExtractBool(const rapidjson::Value* object,
+                     const char* field,
+                     bool* result) const;
+
   Status ExtractInt32(const rapidjson::Value* object,
                       const char* field,
                       int32_t* result) const;


Mime
View raw message