Skip to content

Commit

Permalink
settings: explicit prefix check
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Apr 11, 2024
1 parent 256f64f commit 3646e4f
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion code/espurna/api_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace web {
STRING_VIEW_INLINE(Prefix, "api");

bool onKeyCheck(espurna::StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ static constexpr espurna::settings::query::IndexedSetting IndexedSettings[] PROG
};

bool checkSamePrefix(StringView key) {
return espurna::settings::query::samePrefix(key, espurna::button::settings::Prefix);
return key.startsWith(espurna::button::settings::Prefix);
}

String findValueFrom(StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/curtain_kingart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void _curtainWebSocketOnConnected(JsonObject& root) {

//------------------------------------------------------------------------------
bool _curtainWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, CurtainPrefix);
return key.startsWith(CurtainPrefix);
}

//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/domoticz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ namespace {
STRING_VIEW_INLINE(Prefix, "dcz");

bool onKeyCheck(espurna::StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
6 changes: 3 additions & 3 deletions code/espurna/garland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ void _garlandWebSocketOnConnected(JsonObject& root) {

//------------------------------------------------------------------------------
bool _garlandWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, NAME_GARLAND_ENABLED)
|| espurna::settings::query::samePrefix(key, NAME_GARLAND_BRIGHTNESS)
|| espurna::settings::query::samePrefix(key, NAME_GARLAND_SPEED);
return key.equals(NAME_GARLAND_ENABLED)
|| key.equals(NAME_GARLAND_BRIGHTNESS)
|| key.equals(NAME_GARLAND_SPEED);
}

//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/homeassistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static constexpr espurna::settings::query::Setting Settings[] PROGMEM {
STRING_VIEW_INLINE(Prefix, "ha");

bool checkSamePrefix(espurna::StringView key) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

String findValueFrom(espurna::StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/influxdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void _idbInitClient() {
STRING_VIEW_INLINE(IdbPrefix, "idb");

bool _idbWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, IdbPrefix);
return key.startsWith(IdbPrefix);
}

void _idbWebSocketOnVisible(JsonObject& root) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ static constexpr espurna::settings::query::IndexedSetting IndexedSettings[] PROG
};

bool checkSamePrefix(StringView key) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

String findValueFrom(StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ static constexpr espurna::settings::query::Setting Settings[] PROGMEM {
};

bool checkSamePrefix(espurna::StringView key) {
return espurna::settings::query::samePrefix(key, settings::Prefix);
return key.startsWith(settings::Prefix);
}

String findValueFrom(espurna::StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/nofuss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool _nofussEnabled = false;
STRING_VIEW_INLINE(NofussPrefix, "nofuss");

bool _nofussWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, NofussPrefix);
return key.startsWith(NofussPrefix);
}

void _nofussWebSocketOnVisible(JsonObject& root) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ time_t now() {
namespace web {

bool onKeyCheck(StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, settings::Prefix);
return key.startsWith(settings::Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2334,7 +2334,7 @@ STRING_VIEW_INLINE(RelayPrefix, "relay");
STRING_VIEW_INLINE(MultiRelay, "multirelay");

bool _relayWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, RelayPrefix);
return key.startsWith(RelayPrefix);
}

void _relayWebSocketUpdate(JsonObject& root) {
Expand Down Expand Up @@ -3318,7 +3318,7 @@ namespace {

bool checkSamePrefix(StringView key) {
PROGMEM_STRING(Prefix, "relay");
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

String findIndexedValueFrom(StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/rfbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ void _rfbWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& d
}

bool _rfbWebSocketOnKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, rfbridge::settings::Prefix);
return key.startsWith(rfbridge::settings::Prefix);
}

#endif // WEB_SUPPORT
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/rfm69.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void _rfm69WebSocketOnConnected(JsonObject& root) {
}

bool _rfm69WebSocketOnKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, Rfm69Prefix);
return key.startsWith(Rfm69Prefix);
}

void _rfm69WebSocketOnAction(uint32_t client_id, const char* action, JsonObject& data) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/rpnrules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ size_t countMqttNames() {
#endif

bool onKeyCheck(espurna::StringView key, const JsonVariant& value) {
return espurna::settings::query::samePrefix(key, settings::Prefix);
return key.startsWith(settings::Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ void migrate(int version) {
namespace query {

bool checkSamePrefix(StringView key) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

String findIndexedValueFrom(StringView key) {
Expand Down Expand Up @@ -576,7 +576,7 @@ void setup() {
namespace web {

bool onKey(StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, settings::Prefix);
return key.startsWith(settings::Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
12 changes: 6 additions & 6 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3140,18 +3140,18 @@ bool check(StringView key) {
return false;
}

using espurna::settings::query::samePrefix;
if (samePrefix(key, settings::prefix::Sensor)) {
if (key.startsWith(settings::prefix::Sensor)) {
return true;
}

if (samePrefix(key, settings::prefix::Power)) {
if (key.startsWith(settings::prefix::Power)) {
return true;
}

return magnitude::forEachCountedCheck([&](unsigned char type) {
return samePrefix(key, prefix::get(type));
});
return magnitude::forEachCountedCheck(
[&](unsigned char type) {
return key.startsWith(prefix::get(type));
});
}

String get(StringView key) {
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ String Setting::findValueFrom(const Setting* begin, const Setting* end, StringVi

bool IndexedSetting::findSamePrefix(const IndexedSetting* begin, const IndexedSetting* end, StringView key) {
for (auto it = begin; it != end; ++it) {
if (samePrefix(key, (*it).prefix())) {
if (key.startsWith((*it).prefix())) {
return true;
}
}
Expand Down Expand Up @@ -180,7 +180,7 @@ void foreach_prefix(PrefixResultCallback&& callback, query::StringViewIterator p
kv_store.foreach([&](kvs_type::KeyValueResult&& kv) {
auto key = kv.key.read();
for (auto it = prefixes.begin(); it != prefixes.end(); ++it) {
if (query::samePrefix(StringView{key}, (*it))) {
if (StringView{key}.startsWith(*it)) {
callback((*it), std::move(key), kv.value);
}
}
Expand Down
4 changes: 0 additions & 4 deletions code/espurna/settings_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ struct alignas(8) Enumeration {

namespace query {

inline bool samePrefix(StringView key, StringView prefix) {
return key.startsWith(prefix);
}

struct StringViewIterator {
using Element = const StringView*;

Expand Down
2 changes: 1 addition & 1 deletion code/espurna/telnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static constexpr std::array<espurna::settings::query::Setting, 3> Settings PROGM
}};

bool checkExactPrefix(StringView key) {
return espurna::settings::query::samePrefix(key, settings::Prefix);
return key.startsWith(settings::Prefix);
}

String findValueFrom(StringView key) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/thingspeak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ namespace {
STRING_VIEW_INLINE(Prefix, "tspk");

bool onKeyCheck(StringView key, const JsonVariant&) {
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

void onVisible(JsonObject& root) {
Expand Down
6 changes: 4 additions & 2 deletions code/espurna/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ size_t ports() {
}

namespace settings {

STRING_VIEW_INLINE(Prefix, "uart");

namespace query {

#define ID_VALUE(NAME)\
Expand Down Expand Up @@ -454,8 +457,7 @@ static constexpr espurna::settings::query::IndexedSetting IndexedSettings[] PROG
};

bool checkSamePrefix(StringView key) {
PROGMEM_STRING(Prefix, "uart");
return espurna::settings::query::samePrefix(key, Prefix);
return key.startsWith(Prefix);
}

String findIndexedValueFrom(StringView key) {
Expand Down
10 changes: 4 additions & 6 deletions code/espurna/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,9 @@ void configure() {
// -----------------------------------------------------------------------------

namespace settings {

STRING_VIEW_INLINE(Prefix, "wifi");

namespace query {

static constexpr std::array<espurna::settings::query::Setting, 11> Settings PROGMEM {
Expand All @@ -2267,12 +2270,7 @@ bool checkIndexedPrefix(StringView key) {

// generic 'ap' and 'modem' configuration
bool checkExactPrefix(StringView key) {
PROGMEM_STRING(Prefix, "wifi");
if (espurna::settings::query::samePrefix(key, Prefix)) {
return true;
}

return false;
return key.startsWith(Prefix);
}

String findIndexedValueFrom(StringView key) {
Expand Down

0 comments on commit 3646e4f

Please sign in to comment.