You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by da...@apache.org on 2017/07/12 18:33:11 UTC
kudu git commit: Add json builder utility
Repository: kudu
Updated Branches:
refs/heads/master 69ee17dc7 -> 81939782a
Add json builder utility
The EasyJson class serves as a wrapper around rapidjson
Value objects, to simplify their usage. It is intended solely
for building json objects, not writing/parsing.
Simplifies code like this:
rapidjson::Document d;
rapidjson::Value v;
v.SetObject();
rapidjson::Value list;
list.SetArray();
v.AddMember("list", list, d.GetAllocator());
v["list"].PushBack(rapidjson::Value().SetString("element"), d.GetAllocator());
To this:
EasyJson ej;
ej["list"][0] = "element";
This commit is intended to facilitate the transition
of webserver path handlers from raw HTML to mustache
templates and json objects.
Change-Id: Ie195b06c5ba67af3043d2032c56b90c1a85f641a
Reviewed-on: http://gerrit.cloudera.org:8080/7387
Reviewed-by: Dan Burkert <da...@apache.org>
Reviewed-by: Andrew Wong <aw...@cloudera.com>
Tested-by: Kudu Jenkins
Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/81939782
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/81939782
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/81939782
Branch: refs/heads/master
Commit: 81939782a21d195371fb2fdf511c3e8cda203826
Parents: 69ee17d
Author: Sam Okrent <sa...@cloudera.com>
Authored: Thu Jul 6 14:13:29 2017 -0700
Committer: Dan Burkert <da...@apache.org>
Committed: Wed Jul 12 18:32:38 2017 +0000
----------------------------------------------------------------------
src/kudu/util/CMakeLists.txt | 2 +
src/kudu/util/easy_json-test.cc | 94 ++++++++++++++++++++
src/kudu/util/easy_json.cc | 153 ++++++++++++++++++++++++++++++++
src/kudu/util/easy_json.h | 163 +++++++++++++++++++++++++++++++++++
4 files changed, 412 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kudu/blob/81939782/src/kudu/util/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/src/kudu/util/CMakeLists.txt b/src/kudu/util/CMakeLists.txt
index e4fff0c..f3e9ec8 100644
--- a/src/kudu/util/CMakeLists.txt
+++ b/src/kudu/util/CMakeLists.txt
@@ -130,6 +130,7 @@ set(UTIL_SRCS
debug/trace_event_impl.cc
debug/trace_event_impl_constants.cc
debug/trace_event_synthetic_delay.cc
+ easy_json.cc
env.cc env_posix.cc env_util.cc
errno.cc
faststring.cc
@@ -335,6 +336,7 @@ ADD_KUDU_TEST(callback_bind-test)
ADD_KUDU_TEST(countdown_latch-test)
ADD_KUDU_TEST(crc-test RUN_SERIAL true) # has a benchmark
ADD_KUDU_TEST(debug-util-test)
+ADD_KUDU_TEST(easy_json-test)
ADD_KUDU_TEST(env-test LABELS no_tsan)
ADD_KUDU_TEST(env_util-test)
ADD_KUDU_TEST(errno-test)
http://git-wip-us.apache.org/repos/asf/kudu/blob/81939782/src/kudu/util/easy_json-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/easy_json-test.cc b/src/kudu/util/easy_json-test.cc
new file mode 100644
index 0000000..322c967
--- /dev/null
+++ b/src/kudu/util/easy_json-test.cc
@@ -0,0 +1,94 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <string>
+
+#include <gtest/gtest.h>
+#include <rapidjson/document.h>
+
+#include "kudu/gutil/integral_types.h"
+#include "kudu/gutil/strings/substitute.h"
+#include "kudu/util/easy_json.h"
+#include "kudu/util/test_util.h"
+
+using rapidjson::SizeType;
+using rapidjson::Value;
+using std::string;
+
+namespace kudu {
+
+class JsonBuilderTest: public KuduTest {};
+
+TEST_F(JsonBuilderTest, TestNull) {
+ EasyJson ej;
+ ASSERT_TRUE(ej.value().IsNull());
+}
+
+TEST_F(JsonBuilderTest, TestBasic) {
+ EasyJson ej;
+ ej.SetObject();
+ ej.Set("1", true);
+ ej.Set("2", kint32min);
+ ej.Set("4", kint64min);
+ ej.Set("6", 1.0);
+ ej.Set("7", "string");
+
+ Value& v = ej.value();
+
+ ASSERT_EQ(v["1"].GetBool(), true);
+ ASSERT_EQ(v["2"].GetInt(), kint32min);
+ ASSERT_EQ(v["4"].GetInt64(), kint64min);
+ ASSERT_EQ(v["6"].GetDouble(), 1.0);
+ ASSERT_EQ(string(v["7"].GetString()), "string");
+}
+
+TEST_F(JsonBuilderTest, TestNested) {
+ EasyJson ej;
+ ej.SetObject();
+ ej.Get("nested").SetObject();
+ ej.Get("nested").Set("nested_attr", true);
+ ASSERT_EQ(ej.value()["nested"]["nested_attr"].GetBool(), true);
+
+ ej.Get("nested_array").SetArray();
+ ej.Get("nested_array").PushBack(1);
+ ej.Get("nested_array").PushBack(2);
+ ASSERT_EQ(ej.value()["nested_array"][SizeType(0)].GetInt(), 1);
+ ASSERT_EQ(ej.value()["nested_array"][SizeType(1)].GetInt(), 2);
+}
+
+TEST_F(JsonBuilderTest, TestCompactSyntax) {
+ EasyJson ej;
+ ej["nested"]["nested_attr"] = true;
+ ASSERT_EQ(ej.value()["nested"]["nested_attr"].GetBool(), true);
+
+ for (int i = 0; i < 2; i++) {
+ ej["nested_array"][i] = i + 1;
+ }
+ ASSERT_EQ(ej.value()["nested_array"][SizeType(0)].GetInt(), 1);
+ ASSERT_EQ(ej.value()["nested_array"][SizeType(1)].GetInt(), 2);
+}
+
+TEST_F(JsonBuilderTest, TestAllocatorLifetime) {
+ EasyJson* root = new EasyJson;
+ EasyJson child = (*root)["child"];
+ delete root;
+
+ child["child_attr"] = 1;
+ ASSERT_EQ(child.value()["child_attr"].GetInt(), 1);
+}
+
+} // namespace kudu
http://git-wip-us.apache.org/repos/asf/kudu/blob/81939782/src/kudu/util/easy_json.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/easy_json.cc b/src/kudu/util/easy_json.cc
new file mode 100644
index 0000000..8c4382b
--- /dev/null
+++ b/src/kudu/util/easy_json.cc
@@ -0,0 +1,153 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "kudu/util/easy_json.h"
+
+#include <string>
+#include <utility>
+
+#include <rapidjson/document.h>
+
+using rapidjson::SizeType;
+using rapidjson::Value;
+using std::string;
+
+namespace kudu {
+
+EasyJson::EasyJson() : alloc_(new EasyJsonAllocator), value_(&alloc_->value()) {}
+
+EasyJson EasyJson::Get(const string& key) {
+ if (!value_->IsObject()) {
+ value_->SetObject();
+ }
+ if (!value_->HasMember(key.c_str())) {
+ Value key_val(key.c_str(), alloc_->allocator());
+ value_->AddMember(key_val, Value().SetNull(), alloc_->allocator());
+ }
+ return EasyJson(&(*value_)[key.c_str()], alloc_);
+}
+
+EasyJson EasyJson::Get(int index) {
+ if (!value_->IsArray()) {
+ value_->SetArray();
+ }
+ while (SizeType(index) >= value_->Size()) {
+ value_->PushBack(Value().SetNull(), alloc_->allocator());
+ }
+ return EasyJson(&(*value_)[index], alloc_);
+}
+
+EasyJson EasyJson::operator[](const string& key) {
+ return Get(key);
+}
+
+EasyJson EasyJson::operator[](int index) {
+ return Get(index);
+}
+
+template<typename T>
+EasyJson& EasyJson::operator=(T val) {
+ *value_ = val;
+ return *this;
+}
+template EasyJson& EasyJson::operator=<bool>(bool val);
+template EasyJson& EasyJson::operator=<int32_t>(int32_t val);
+template EasyJson& EasyJson::operator=<int64_t>(int64_t val);
+template EasyJson& EasyJson::operator=<double>(double val);
+template<> EasyJson& EasyJson::operator=<const char*>(const char* val) {
+ value_->SetString(val, alloc_->allocator());
+ return *this;
+}
+template<> EasyJson& EasyJson::operator=<const string&>(const string& val) {
+ value_->SetString(val.c_str(), alloc_->allocator());
+ return *this;
+}
+
+EasyJson& EasyJson::SetObject() {
+ if (!value_->IsObject()) {
+ value_->SetObject();
+ }
+ return *this;
+}
+
+EasyJson& EasyJson::SetArray() {
+ if (!value_->IsArray()) {
+ value_->SetArray();
+ }
+ return *this;
+}
+
+template<typename T>
+EasyJson EasyJson::Set(const string& key, T val) {
+ return (Get(key) = val);
+}
+template EasyJson EasyJson::Set<bool>(const string& key, bool val);
+template EasyJson EasyJson::Set<int32_t>(const string& key, int32_t val);
+template EasyJson EasyJson::Set<int64_t>(const string& key, int64_t val);
+template EasyJson EasyJson::Set<double>(const string& key, double val);
+template<> EasyJson EasyJson::Set<const char*>(const string& key, const char* val) {
+ return (Get(key).operator=<const char*>(val));
+}
+template<> EasyJson EasyJson::Set<const string&>(const string& key, const string& val) {
+ return (Get(key).operator=<const string&>(val));
+}
+
+template<typename T>
+EasyJson EasyJson::Set(int index, T val) {
+ return (Get(index) = val);
+}
+template EasyJson EasyJson::Set<bool>(int index, bool val);
+template EasyJson EasyJson::Set<int32_t>(int index, int32_t val);
+template EasyJson EasyJson::Set<int64_t>(int index, int64_t val);
+template EasyJson EasyJson::Set<double>(int index, double val);
+template<> EasyJson EasyJson::Set<const char*>(int index, const char* val) {
+ return (Get(index).operator=<const char*>(val));
+}
+template<> EasyJson EasyJson::Set<const string&>(int index, const string& val) {
+ return (Get(index).operator=<const string&>(val));
+}
+
+template<typename T>
+EasyJson EasyJson::PushBack(T val) {
+ if (!value_->IsArray()) {
+ value_->SetArray();
+ }
+ return EasyJson(&value_->PushBack(val, alloc_->allocator()), alloc_);
+}
+template EasyJson EasyJson::PushBack<bool>(bool val);
+template EasyJson EasyJson::PushBack<int32_t>(int32_t val);
+template EasyJson EasyJson::PushBack<int64_t>(int64_t val);
+template EasyJson EasyJson::PushBack<double>(double val);
+template<> EasyJson EasyJson::PushBack<const char*>(const char* val) {
+ if (!value_->IsArray()) {
+ value_->SetArray();
+ }
+ Value push_val(val, alloc_->allocator());
+ return EasyJson(&value_->PushBack(push_val, alloc_->allocator()), alloc_);
+}
+template<> EasyJson EasyJson::PushBack<const string&>(const string& val) {
+ if (!value_->IsArray()) {
+ value_->SetArray();
+ }
+ Value push_val(val.c_str(), alloc_->allocator());
+ return EasyJson(&value_->PushBack(push_val, alloc_->allocator()), alloc_);
+}
+
+EasyJson::EasyJson(Value* value, const scoped_refptr<EasyJsonAllocator>& alloc)
+ : alloc_(alloc), value_(value) {}
+
+} // namespace kudu
http://git-wip-us.apache.org/repos/asf/kudu/blob/81939782/src/kudu/util/easy_json.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/easy_json.h b/src/kudu/util/easy_json.h
new file mode 100644
index 0000000..e0380e9
--- /dev/null
+++ b/src/kudu/util/easy_json.h
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+#pragma once
+
+#include <string>
+
+#include <rapidjson/document.h>
+
+#include "kudu/gutil/ref_counted.h"
+
+namespace kudu {
+
+// A wrapper around rapidjson Value objects, to simplify usage.
+// Intended solely for building json objects, not writing/parsing.
+//
+// Simplifies code like this:
+//
+// rapidjson::Document d;
+// rapidjson::Value v;
+// v.SetObject();
+// rapidjson::Value list;
+// list.SetArray();
+// v.AddMember("list", list, d.GetAllocator());
+// v["list"].PushBack(rapidjson::Value().SetString("element"), d.GetAllocator());
+//
+// To this:
+//
+// EasyJson ej;
+// ej["list"][0] = "element";
+//
+// Client code should build objects as demonstrated above,
+// then call EasyJson::value() to obtain a reference to the
+// built rapidjson Value.
+class EasyJson {
+ public:
+ EasyJson();
+ ~EasyJson() = default;
+
+ // Returns the child EasyJson associated with key.
+ //
+ // Note: this method can mutate the EasyJson object
+ // as follows:
+ //
+ // If this EasyJson's underlying Value is not an object
+ // (i.e. !this->value().IsObject()), then its Value is
+ // coerced to an object, overwriting the old Value.
+ // If the given key does not exist, a Null-valued
+ // EasyJson associated with key is created.
+ EasyJson Get(const std::string& key);
+
+ // Returns the child EasyJson at index.
+ //
+ // Note: this method can mutate the EasyJson object
+ // as follows:
+ //
+ // If this EasyJson's underlying Value is not an array
+ // (i.e. !this->value().IsArray()), then its Value is
+ // coerced to an array, overwriting the old Value.
+ // If index >= this->value().Size(), then the underlying
+ // array's size is increased to index + 1 (new indices
+ // are filled with Null values).
+ EasyJson Get(int index);
+
+ // Same as Get(key).
+ EasyJson operator[](const std::string& key);
+ // Same as Get(index).
+ EasyJson operator[](int index);
+
+ // Sets the underlying Value equal to val.
+ //
+ // 'val' can be a bool, int32_t, int64_t, double,
+ // char*, or string.
+ template<typename T>
+ EasyJson& operator=(T val);
+
+ // Sets the underlying Value to an object.
+ //
+ // i.e. after calling SetObject(),
+ // value().IsObject() == true
+ EasyJson& SetObject();
+ // Sets the underlying Value to an array.
+ //
+ // i.e. after calling SetArray(),
+ // value().IsArray() == true
+ EasyJson& SetArray();
+
+ // Associates val with key.
+ //
+ // If this EasyJson's underlying Value is not an object
+ // (i.e. !this->value().IsObject()), then its Value is
+ // coerced to an object, overwriting the old Value.
+ // If the given key does not exist, a new child entry
+ // is created with the given value.
+ template<typename T>
+ EasyJson Set(const std::string& key, T val);
+
+ // Stores val at index.
+ //
+ // If this EasyJson's underlying Value is not an array
+ // (i.e. !this->value().IsArray()), then its Value is
+ // coerced to an array, overwriting the old Value.
+ // If index >= this->value().Size(), then the underlying
+ // array's size is increased to index + 1 (new indices
+ // are filled with Null values).
+ template<typename T>
+ EasyJson Set(int index, T val);
+
+ // Appends val to the underlying array.
+ //
+ // If this EasyJson's underlying Value is not an array
+ // (i.e. !this->value().IsArray()), then its Value is
+ // coerced to an array, overwriting the old Value.
+ template<typename T>
+ EasyJson PushBack(T val);
+
+ // Returns a reference to the underlying Value.
+ rapidjson::Value& value() const { return *value_; }
+
+ private:
+ // One instance of EasyJsonAllocator is shared among a root
+ // EasyJson object and all of its descendants. The allocator
+ // owns the underlying rapidjson Value, and a rapidjson
+ // allocator (via a rapidjson::Document).
+ class EasyJsonAllocator : public RefCounted<EasyJsonAllocator> {
+ public:
+ rapidjson::Value& value() { return value_; }
+ rapidjson::Document::AllocatorType& allocator() { return value_.GetAllocator(); }
+ private:
+ friend class RefCounted<EasyJsonAllocator>;
+ ~EasyJsonAllocator() = default;
+
+ // The underlying rapidjson::Value object (Document is
+ // a subclass of Value that has its own allocator).
+ rapidjson::Document value_;
+ };
+
+ // Used to instantiate descendant objects.
+ EasyJson(rapidjson::Value* value, const scoped_refptr<EasyJsonAllocator>& alloc);
+
+ // One allocator is shared among an EasyJson object and
+ // all of its descendants.
+ scoped_refptr<EasyJsonAllocator> alloc_;
+
+ // A pointer to the underlying Value in the object
+ // tree owned by alloc_.
+ rapidjson::Value* value_;
+};
+
+} // namespace kudu