-
Notifications
You must be signed in to change notification settings - Fork 308
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
Breakup load into separate functions #360
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,27 +17,25 @@ bool Config::_spiffsBegin() { | |
return _spiffsBegan; | ||
} | ||
|
||
bool Config::load() { | ||
if (!_spiffsBegin()) { return false; } | ||
|
||
_valid = false; | ||
JsonObject& Config::loadJSONObject() { | ||
if (!_spiffsBegin()) { return JsonObject::invalid(); } | ||
|
||
if (!SPIFFS.exists(CONFIG_FILE_PATH)) { | ||
Interface::get().getLogger() << F("✖ ") << CONFIG_FILE_PATH << F(" doesn't exist") << endl; | ||
return false; | ||
return JsonObject::invalid(); | ||
} | ||
|
||
File configFile = SPIFFS.open(CONFIG_FILE_PATH, "r"); | ||
if (!configFile) { | ||
Interface::get().getLogger() << F("✖ Cannot open config file") << endl; | ||
return false; | ||
return JsonObject::invalid(); | ||
} | ||
|
||
size_t configSize = configFile.size(); | ||
|
||
if (configSize > MAX_JSON_CONFIG_FILE_SIZE) { | ||
Interface::get().getLogger() << F("✖ Config file too big") << endl; | ||
return false; | ||
return JsonObject::invalid(); | ||
} | ||
|
||
char buf[MAX_JSON_CONFIG_FILE_SIZE]; | ||
|
@@ -49,6 +47,17 @@ bool Config::load() { | |
JsonObject& parsedJson = jsonBuffer.parseObject(buf); | ||
if (!parsedJson.success()) { | ||
Interface::get().getLogger() << F("✖ Invalid JSON in the config file") << endl; | ||
return JsonObject::invalid(); | ||
} | ||
return parsedJson; | ||
} | ||
|
||
|
||
bool Config::load() { | ||
_valid = false; | ||
JsonObject& parsedJson = Config::loadJSONObject(); | ||
if (!parsedJson.success()) { | ||
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.
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. It's been a decade or two since I've done any real C++ coding. Thanks for the sanity check. |
||
// Config::loadJSONObject would already log a specific error so we don't need to here | ||
return false; | ||
} | ||
|
||
|
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.
jsonBuffer
is a local variable, so you create a reference to something internal to this local variable.The local variable gets destroyed when leaving
Config::load()
, this (hopefully ;-) frees the buffer ofparsedJson
.