This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 488
FirebaseObject: simplify API #136
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab6b537
FirebaseObject: simplify API
proppy 37248b3
examples: update FirebaseDemo to use getFloat
proppy 11baa52
FirebaseArduino: fix get methods signature
proppy ad75ff6
FirebaseObject: expose getJsonVariant
proppy a8d7557
FirebaseObject: fix spacing and add comments
proppy e4898ed
examples: fix stream FirebaseObject usage
proppy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,57 +16,81 @@ | |
|
|
||
| #include "FirebaseObject.h" | ||
|
|
||
| namespace { | ||
| template<typename T> | ||
| T decodeJsonLiteral(const String& json) { | ||
| return JsonVariant{ArduinoJson::RawJson{json.c_str()}}; | ||
| } | ||
|
|
||
| // ugly workaround to https://github.com/bblanchon/ArduinoJson/issues/265 | ||
| template<> | ||
| String decodeJsonLiteral<String>(const String& json) { | ||
| StaticJsonBuffer<JSON_ARRAY_SIZE(1)> buf; | ||
| String array = "[" + json + "]"; | ||
| return buf.parseArray(&array[0])[0]; | ||
| } | ||
| } // namespace | ||
|
|
||
| FirebaseObject::FirebaseObject(const String& data) : data_{data} { | ||
| if (data_[0] == '{') { | ||
| json_ = &buffer_.parseObject(&data_[0]); | ||
| } else if (data_[0] == '"') { | ||
| data_ = decodeJsonLiteral<String>(data_); | ||
| } | ||
| json_ = buffer_.parse(&data_[0]); | ||
| // TODO(proppy): find a way to check decoding error, tricky because | ||
| // ArduinoJson doesn't surface error for variant parsing. | ||
| // See: https://github.com/bblanchon/ArduinoJson/issues/279 | ||
| } | ||
|
|
||
| FirebaseObject::operator bool() { | ||
| return decodeJsonLiteral<bool>(data_); | ||
| bool FirebaseObject::getBool(const String& path) { | ||
| JsonVariant variant = getJsonVariant(path); | ||
| if (!variant.is<bool>()) { | ||
| error_ = "failed to convert to bool"; | ||
| return 0; | ||
| } | ||
| return static_cast<bool>(variant); | ||
| } | ||
|
|
||
| FirebaseObject::operator int() { | ||
| return decodeJsonLiteral<int>(data_); | ||
| int FirebaseObject::getInt(const String& path) { | ||
| JsonVariant variant = getJsonVariant(path); | ||
| if (!variant.is<int>()) { | ||
| error_ = "failed to convert to int"; | ||
| return 0; | ||
| } | ||
| return static_cast<int>(variant); | ||
| } | ||
|
|
||
| FirebaseObject::operator float() { | ||
| return decodeJsonLiteral<float>(data_); | ||
| float FirebaseObject::getFloat(const String& path) { | ||
| JsonVariant variant = getJsonVariant(path); | ||
| if (!variant.is<float>()) { | ||
| error_ = "failed to convert to float"; | ||
| return 0; | ||
| } | ||
| return static_cast<float>(variant); | ||
| } | ||
|
|
||
| FirebaseObject::operator const String&() { | ||
| return data_; | ||
| String FirebaseObject::getString(const String& path) { | ||
| JsonVariant variant = getJsonVariant(path); | ||
| if (!variant.is<const char *>()) { | ||
| error_ = "failed to convert to string"; | ||
| return ""; | ||
| } | ||
| return static_cast<const char*>(variant); | ||
| } | ||
|
|
||
| FirebaseObject::operator const JsonObject&() { | ||
| return *json_; | ||
| JsonVariant FirebaseObject::getJsonVariant(const String& path) { | ||
| String key(path); | ||
| char* start = &key[0]; | ||
| char* end = start + key.length(); | ||
| // skip first `/`. | ||
| if (*start == '/') { | ||
| start++; | ||
| } | ||
| JsonVariant json = json_; | ||
| while (start < end) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this block of code seems valid it is pretty tricky and could probably be made clearer, at the least it could use some comments.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| // TODO(proppy) split in a separate function. | ||
| char* p = start; | ||
| // advance to next `/`. | ||
| while (*p && (*p != '/')) p++; | ||
| // make `start` a C string. | ||
| *p = 0; | ||
| // return json variant at `start`. | ||
| json = json.asObject().get(start); | ||
| // advance to next path element. | ||
| start = p + 1; | ||
| } | ||
| return json; | ||
| } | ||
|
|
||
| JsonObjectSubscript<const char*> FirebaseObject::operator[](const char* key) { | ||
| return json_->operator[](key); | ||
| bool FirebaseObject::failed() const { | ||
| return error_.length() > 0; | ||
| } | ||
|
|
||
| JsonObjectSubscript<const String&> FirebaseObject::operator[](const String& key) { | ||
| return json_->operator[](key); | ||
| bool FirebaseObject::success() const { | ||
| return error_.length() == 0; | ||
| } | ||
|
|
||
| JsonVariant FirebaseObject::operator[](JsonObjectKey key) const { | ||
| return json_->operator[](key); | ||
| const String& FirebaseObject::error() const { | ||
| return error_; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
extra line here too.
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.
Done.