Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rapidjson parity #205

Merged
merged 17 commits into from
Nov 30, 2016
Merged

Rapidjson parity #205

merged 17 commits into from
Nov 30, 2016

Conversation

ccaraman
Copy link
Contributor

@ccaraman ccaraman commented Nov 9, 2016

No description provided.

error.line, error.column));
char buffer[4096];

FILE* fp = fopen(file_path.c_str(), "r");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are leaking here. Can you use C++ iostream?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not, you can use CSmartPtr and supply fclose not to leak fd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to switch to iostream.

throw Exception(fmt::format("File:{} doesn't exist.", file_path));
}

rapidjson::FileReadStream fs(fp, buffer, sizeof(buffer));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is buffer used for here? Why does it have to be specified? Why 4K? Seems odd there is no API to just deal with buffering internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Switched to using iostream.

rapidjson::FileReadStream fs(fp, buffer, sizeof(buffer));
if (document_.ParseStream(fs).HasParseError()) {
throw Exception(fmt::format("Error(offset {}): {}\n",
static_cast<unsigned>(document_.GetErrorOffset()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is offset? Does it include line number?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering, why casting to unsigned_int?

@@ -20,6 +20,7 @@ target_link_libraries(envoy rt)
target_link_libraries(envoy dl)

include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR})
include_directories(SYSTEM ${ENVOY_RAPIDJSON_INCLUDE_DIR})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. It was due to the initial integration of rapidjson. No longer needed now that rapidjson headers are only in the .cc file.

@@ -24,6 +24,7 @@ include_directories(SYSTEM ${ENVOY_HTTP_PARSER_INCLUDE_DIR})
include_directories(${ENVOY_NGHTTP2_INCLUDE_DIR})
include_directories(SYSTEM ${ENVOY_OPENSSL_INCLUDE_DIR})
include_directories(SYSTEM ${ENVOY_LIGHTSTEP_TRACER_INCLUDE_DIR})
include_directories(SYSTEM ${ENVOY_RAPIDJSON_INCLUDE_DIR})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It made the compiler happy, back when I was playing with it. I don't think this will be necessary once I find a way to get rid of the refernce to rapidjson/document.h in the json_loader.h

@@ -128,20 +136,10 @@ class Object {
bool hasObject(const std::string& name) const;

protected:
Object() : name_("root") {}
Object() : name_("root"), document_() {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default initializer not needed

}

// Empty Object
Object(const std::string& name) : name_(name), document_() {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default initializer not needed

@@ -25,7 +28,12 @@ typedef std::function<bool(const std::string&, const Object&)> ObjectCallback;
*/
class Object {
public:
Object(json_t* json, const std::string& name) : json_(json), name_(name) {}
Object(const rapidjson::Value& value, const std::string& name) : name_(name), document_() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default initializer not needed

struct json_t;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#include "rapidjson/document.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not leak this in header. Find a way around this even if you need to expose an interface.

@@ -172,7 +160,7 @@ void Object::iterate(const ObjectCallback& callback) {
}

bool Object::hasObject(const std::string& name) const {
return json_object_get(json_, name.c_str()) != nullptr;
return document_.HasMember(name.c_str()) && document_.IsObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know API but seems wrong to be calling document_.IsObject() here.

}

return string_array;
}

double Object::getDouble(const std::string& name) const {
json_t* real = json_object_get(json_, name.c_str());
if (!real || !json_is_real(real)) {
if (!document_.HasMember(name.c_str()) || !document_[name.c_str()].IsDouble()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you avoid multiple hash lookups here and elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed multiple hash lookups.

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#include "jansson.h"
#include <rapidjson/error/en.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: s/</"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


/**
* Get a boolean value by name.
* @param name supplies the key name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: two spaces :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


Object::EmptyObject::~EmptyObject() { json_decref(json_); }
std::vector<AbstractObjectPtr> object_array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, i'd reserve vector capacity upfront as we know the Size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here and in json_loader.h

for (size_t i = 0; i < json_array_size(json_); i++) {
object_array.emplace_back(json_array_get(json_, i), name_ + " (array item)");
bool getBoolean(const std::string& name) const override {
if (!document_[name.c_str()].IsBool()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not to familiar with api, but can we

  1. auto &blah = document_[name.c_str()];
    and query internal map or something once.

if (!blah.IsBool()) {
}

return blah.GetBool();

@@ -14,10 +14,11 @@ Envoy has the following requirements:
* `libevent <http://libevent.org/>`_ (last tested with 2.0.22)
* `tclap <http://tclap.sourceforge.net/>`_ (last tested with 1.2.1)
* `gperftools <https://github.com/gperftools/gperftools>`_ (last tested with 2.5.0)
* `jansson <https://github.com/akheron/jansson>`_ (last tesed with 2.7)
* `openssl <https://www.openssl.org/>`_ (last tesed with 1.0.2i)
* `jansson <https://github.com/akheron/jansson>`_ (last tested with 2.7)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete all references to jansson from docs, cmake, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that clean up in Jira 615.

/**
* Convert a generic object into an array of objects. This is useful for dealing with arrays
* of arrays.
* @return std::vector<Object> the converted object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

* Get a sub-object by name.
* @param name supplies the key name.
* @param allow_empty supplies whether to return an empty object if the key does not exist.
* @return Object the sub-object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong type

/**
* Get an array by name.
* @param name supplies the key name.
* @return std::vector<Object> the array of JSON objects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

virtual double getDouble(const std::string& name, double default_value) const PURE;

/**
* Iterate Object and call callback on key-value pairs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong type / please fix grammar

/**
* @return a vector of the AbstractObject that are members of the currect AbstractObject.
*/
virtual std::vector<AbstractObjectPtr> getMembers() const PURE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be implemented in terms of iterate? Why is this needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or get rid of iterate() from the public interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

class AbstractObjectImpl : public AbstractObject {
public:
AbstractObjectImpl(const rapidjson::Document& document, const std::string& name) : name_(name) {
document_.CopyFrom(document, document_.GetAllocator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be copying anything. If needed a reference to the parent document should be used.

if (!json_) {
throw Exception(fmt::format("json parsing error: {}", error.text));
AbstractObjectImpl(const rapidjson::Value& value, const std::string& name) : name_(name) {
document_.CopyFrom(value, document_.GetAllocator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no copying

@@ -122,11 +124,6 @@ TEST(JsonLoaderTest, Integer) {
EXPECT_EQ(std::numeric_limits<int64_t>::max(), json.getInteger("max"));
EXPECT_EQ(std::numeric_limits<int64_t>::min(), json.getInteger("min"));
}

{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was this test testing and why is it no longer failing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was testing Max and Min Integer. Unfortunately, rapidjson doesn't throw an exception during Parsing. We should push this validation to the schema. :(

};

/**
* Loads JSON from a string.
*/
class StringLoader : NonCopyable, public Object {
public:
StringLoader(const std::string& json);
~StringLoader();
StringLoader(const std::string& json) { abstract_object_ = Factory::LoadFromString(json); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializer list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

};

/**
* Loads a JSON file into memory.
*/
class FileLoader : NonCopyable, public Object {
public:
FileLoader(const std::string& file_path);
~FileLoader();
FileLoader(const std::string& file_path) { abstract_object_ = Factory::LoadFromFile(file_path); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializer list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


std::vector<Object> getObjectArray(const std::string& name) const {
std::vector<AbstractObjectPtr> abstract_object_array = abstract_object_->getObjectArray(name);
std::vector<Object> return_vector;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reserve capacity ahead of time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reserved

}

Object getObject(const std::string& name, bool allow_empty = false) const {
AbstractObjectPtr abstract_object = abstract_object_->getObject(name, allow_empty);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local var not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed extra local var.


std::vector<Object> asObjectArray() const {
std::vector<AbstractObjectPtr> abstract_object_array = abstract_object_->asObjectArray();
std::vector<Object> return_vector;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reserve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Object() {}

Object(AbstractObjectPtr&& abstract_object_ptr) {
abstract_object_ = std::move(abstract_object_ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializer list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Object object_value(value, "root");
std::string object_key(key);
private:
std::string name_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

namespace Json {
class Object;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split object out into public include and only define the factory stuff in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split.

}

{
EXPECT_THROW(StringLoader("{\"val\":9223372036854775808}"), Exception);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What actually happens here? We shouldn't delete this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readded.

@mattklein123 mattklein123 merged commit 88ed42a into master Nov 30, 2016
@mattklein123 mattklein123 deleted the rapidjson-parity branch November 30, 2016 03:04
rshriram pushed a commit to rshriram/envoy that referenced this pull request Oct 30, 2018
PiotrSikora pushed a commit to PiotrSikora/envoy that referenced this pull request May 12, 2020
* worker: provide removeFilterChain interface (envoyproxy#10528)

Signed-off-by: Yuchen Dai <[email protected]>

* cherry-pick in place filter chain update

Signed-off-by: Yuchen Dai <[email protected]>

* fix conficts

Signed-off-by: Yuchen Dai <[email protected]>
jpsim pushed a commit that referenced this pull request Nov 28, 2022
Signed-off-by: Jose Nino [email protected]

Description: previously the CI runs would only build the demos. However we did not run them, or check for liveliness. This PR adds running all the Android demos in the emulator for Mac CI.
Risk Level: low - improved CI
Testing: tested locally and on CI

Fixes #117

Signed-off-by: JP Simard <[email protected]>
jpsim pushed a commit that referenced this pull request Nov 29, 2022
Signed-off-by: Jose Nino [email protected]

Description: previously the CI runs would only build the demos. However we did not run them, or check for liveliness. This PR adds running all the Android demos in the emulator for Mac CI.
Risk Level: low - improved CI
Testing: tested locally and on CI

Fixes #117

Signed-off-by: JP Simard <[email protected]>
arminabf pushed a commit to arminabf/envoy that referenced this pull request Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants