-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Rapidjson parity #205
Conversation
error.line, error.column)); | ||
char buffer[4096]; | ||
|
||
FILE* fp = fopen(file_path.c_str(), "r"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
There was a problem hiding this comment.
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_() {} |
There was a problem hiding this comment.
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_() {} |
There was a problem hiding this comment.
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_() { |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: s/</"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
b92feb0
to
bac2199
Compare
|
||
/** | ||
* Get a boolean value by name. | ||
* @param name supplies the key name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: two spaces :-)
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
- 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong type
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong type
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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")); | |||
} | |||
|
|||
{ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializer list
There was a problem hiding this comment.
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); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializer list
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local var not needed
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reserve
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializer list
There was a problem hiding this comment.
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_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const
f311925
to
424f6db
Compare
namespace Json { | ||
class Object; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readded.
* 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]>
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]>
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]>
Co-authored-by: Matteo Pace <[email protected]>
No description provided.