Skip to content

Commit

Permalink
Suggest valid options when the configuration contains unknown keys
Browse files Browse the repository at this point in the history
This changeset is purely to improve the user experience. When the
configuration parsing fails with an unknown key we'll test all possible
keys and suggest one that looks a lot like it. This will be nice for
people that make simple typos.
  • Loading branch information
quinox committed Mar 9, 2024
1 parent 287f0c6 commit 7598ee8
Show file tree
Hide file tree
Showing 13 changed files with 1,884 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ add_executable(flashmq
globber.cpp
x509manager.h x509manager.cpp
backgroundworker.h backgroundworker.cpp

edlib/src/edlib.cpp
)

target_include_directories(flashmq PRIVATE edlib/include)

target_link_libraries(flashmq pthread dl ssl crypto resolv anl)

execute_process(COMMAND ../.get-os-codename-and-stamp.sh OUTPUT_VARIABLE OS_CODENAME)
Expand Down
3 changes: 3 additions & 0 deletions FlashMQTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ add_executable(flashmq-tests
../x509manager.cpp
../backgroundworker.h ../backgroundworker.cpp
../flashmqtestclient.cpp ../flashmqtestclient.h
../edlib/src/edlib.cpp
main.cpp
mainappinthread.h mainappinthread.cpp
maintests.h maintests.cpp
Expand All @@ -171,6 +172,8 @@ add_executable(flashmq-tests

)

target_include_directories(flashmq-tests PRIVATE ../edlib/include)

target_include_directories(flashmq-tests PUBLIC ..)

target_link_libraries(flashmq-tests pthread dl ssl crypto resolv anl)
Expand Down
41 changes: 41 additions & 0 deletions FlashMQTests/configtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,44 @@ void MainTests::test_parsing_numbers()
}
}
}

void MainTests::test_config_suggestion()
{
// User made a small typo: 'session' instead of 'sessions'
{
ConfFileTemp config;
config.writeLine("expire_session_after_seconds 180");
config.closeFile();

ConfigFileParser parser(config.getFilePath());

try
{
parser.loadFile(false);
FMQ_FAIL("The parser is too liberal");
}
catch (ConfigFileException &ex)
{
FMQ_COMPARE(ex.what(), "Config key 'expire_session_after_seconds' is not valid (here). Did you mean: expire_sessions_after_seconds ?");
}
}

// User entered gibberish. Let's not suggest gibberish back
{
ConfFileTemp config;
config.writeLine("foobarbaz 180");
config.closeFile();

ConfigFileParser parser(config.getFilePath());

try
{
parser.loadFile(false);
FMQ_FAIL("The parser is too liberal");
}
catch (ConfigFileException &ex)
{
FMQ_COMPARE(ex.what(), "Config key 'foobarbaz' is not valid (here).");
}
}
}
1 change: 1 addition & 0 deletions FlashMQTests/maintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ MainTests::MainTests()
REGISTER_FUNCTION(testOverrideWillDelayOnSessionDestructionByTakeOver);
REGISTER_FUNCTION(testDisabledWills);
REGISTER_FUNCTION(testMqtt5DelayedWillsDisabled);
REGISTER_FUNCTION(test_config_suggestion);
REGISTER_FUNCTION(testIncomingTopicAlias);
REGISTER_FUNCTION(testOutgoingTopicAlias);
REGISTER_FUNCTION(testOutgoingTopicAliasStoredPublishes);
Expand Down
1 change: 1 addition & 0 deletions FlashMQTests/maintests.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class MainTests
void testOverrideWillDelayOnSessionDestructionByTakeOver();
void testDisabledWills();
void testMqtt5DelayedWillsDisabled();
void test_config_suggestion();


void testIncomingTopicAlias();
Expand Down
18 changes: 16 additions & 2 deletions FlashMQTests/testhelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "testhelpers.h"

#include <iostream>
#include <cstring>

int assert_count;
int assert_fail_count;
Expand All @@ -16,8 +17,21 @@ bool fmq_assert(bool b, const char *failmsg, const char *actual, const char *exp

if (asserts_print)
{
std::cout << RED << "FAIL" << COLOR_END << ": '" << failmsg << "', " << actual << " != " << expected << std::endl
<< " in " << file << ", line " << line << std::endl;
// There are two types of failmsg: unformatted ones and formatted ones.
// By testing for a newline we can detect formatted ones.
if (strchr(failmsg, '\n') == nullptr)
{
// unformatted
std::cout << RED << "FAIL" << COLOR_END << ": '" << failmsg << "', " << actual << " != " << expected << std::endl
<< " in " << file << ", line " << line << std::endl;
}
else
{
// formatted
std::cout << RED << "FAIL" << COLOR_END << " in " << file << ", line " << line << std::endl;
std::cout << failmsg << std::endl;
std::cout << "Comparison: " << actual << " != " << expected << std::endl;
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion FlashMQTests/testhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ inline bool fmq_compare(const char *c1, const char *c2, const char *actual, cons
std::string s2(c2);

std::ostringstream oss;
oss << s1 << " != " << s2;

if (s1.length() + s2.length() < 100)
{
// short form
oss << s1 << " != " << s2;
}
else
{
oss << "Actual: " << s1 << std::endl;
oss << "Expected: " << s2;
}

return fmq_assert(s1 == s2, oss.str().c_str(), actual, expected, file, line);
}
Expand Down
1 change: 1 addition & 0 deletions FlashMQTests/willtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "testhelpers.h"
#include "flashmqtestclient.h"
#include "conffiletemp.h"
#include "exceptions.h"

void MainTests::testMqtt3will()
{
Expand Down
27 changes: 27 additions & 0 deletions configfileparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See LICENSE for license details.
#include "exceptions.h"
#include "utils.h"
#include "globber.h"
#include "edlib.h"

/**
* @brief Like std::stoi, but demands that the entire value is consumed
Expand Down Expand Up @@ -93,6 +94,32 @@ bool ConfigFileParser::testKeyValidity(const std::string &key, const std::string
{
std::ostringstream oss;
oss << "Config key '" << key << "' is not valid (here).";

// Maybe the user made a typo
EdlibAlignConfig cmpcnf = edlibDefaultAlignConfig();
const std::string *alternative = nullptr;
int alternative_distance = INT_MAX;

for (const std::string &possible_key : validKeys)
{
EdlibAlignResult result = edlibAlign(key.c_str(), key.length(), possible_key.c_str(), key.length(), cmpcnf);
if (result.status == EDLIB_STATUS_OK)
{
// The mathemathical formula "x/y < 0.5"
// can be approximated with integers as "x*2/y < 1"
if ((result.editDistance * 2) / key.length() < 1 && result.editDistance < alternative_distance)
{
alternative = &possible_key;
alternative_distance = result.editDistance;
}
}
edlibFreeAlignResult(result);
}
if (alternative != nullptr)
{
oss << " Did you mean: " << alternative->c_str() << " ?";
}

throw ConfigFileException(oss.str());
}

Expand Down
20 changes: 20 additions & 0 deletions edlib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Martin Šošić

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions edlib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local copy of https://github.com/Martinsos/edlib
Loading

0 comments on commit 7598ee8

Please sign in to comment.