From f6c2137f3127c6fba0b2dd7a7cf24e52a40cd81c Mon Sep 17 00:00:00 2001 From: mjurado Date: Tue, 12 Sep 2023 16:09:47 +0200 Subject: [PATCH 1/3] Check program input --- headers/Configuration.h | 6 ++++++ src/Configuration.cpp | 28 ++++++++++++++++++++++++++-- src/RecognitionClient.cpp | 1 + src/main.cpp | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/headers/Configuration.h b/headers/Configuration.h index 9a0f2b6..e2ff49b 100644 --- a/headers/Configuration.h +++ b/headers/Configuration.h @@ -45,6 +45,8 @@ class Configuration { std::string getClientSecret() const; + void validate_configuration_values(); + private: std::string language; std::string topic; @@ -60,6 +62,10 @@ class Configuration { std::string label; std::string clientId; std::string clientSecret; + std::vector allowedTopicValues = {"GENERIC", "BANKING", "TELCO", "INSURANCE"}; + std::vector allowedLanguageValues = {"en-US", "en-GB", "pt-BR", "es", "es-419", "tr", "ja", "fr", "fr-CA", "de", "it"}; + std::vector allowedAsrVersionValues = {"V1", "V2"}; + }; diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 7de6e8e..022c9c8 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -1,11 +1,11 @@ #include "Configuration.h" - #include "gRpcExceptions.h" +#include "logger.h" #include -Configuration::Configuration() : host("csr.api.speechcenter.verbio.com"), topic("generic"), language("en-US"), +Configuration::Configuration() : host("us.speechcenter.verbio.com"), topic("generic"), language("en-US"), sampleRate(8000) {} Configuration::Configuration(int argc, char **argv) : Configuration() { @@ -51,6 +51,7 @@ void Configuration::parse(int argc, char **argv) { if ((parsedOptions.count("t") == 0) == (parsedOptions.count("g") == 0)) throw GrpcException("Topic and grammar options are mutually exclusive and at least one is needed."); + validate_configuration_values(); } std::string Configuration::getAudioPath() const { @@ -107,4 +108,27 @@ std::string Configuration::getClientId() const { std::string Configuration::getClientSecret() const { return clientSecret; +} + +void Configuration::validate_configuration_values() { + + if(sampleRate != 8000 and sampleRate != 16000) { + throw std::runtime_error("Unsupported sample rate value. Allowed values: 8000 or 1600"); + } + + if (std::find(allowedTopicValues.begin(), allowedTopicValues.end(), topic) == allowedTopicValues.end()) + { + throw std::runtime_error("Unsupported topic value. Allowed values: GENERIC, BANKING, TELCO, INSURANCE"); + } + + if (std::find(allowedLanguageValues.begin(), allowedLanguageValues.end(), language) == allowedLanguageValues.end()) + { + throw std::runtime_error("Unsupported language value. Allowed values: en-US, en-GB, pt-BR, es, es-419, tr, ja, fr, fr-CA, de, it"); + } + + if (std::find(allowedAsrVersionValues.begin(), allowedAsrVersionValues.end(), asrVersion) == allowedAsrVersionValues.end()) + { + throw std::runtime_error("Unsupported asr version value. Allowed values: V1, V2"); + } + } \ No newline at end of file diff --git a/src/RecognitionClient.cpp b/src/RecognitionClient.cpp index ffb8991..9e81521 100644 --- a/src/RecognitionClient.cpp +++ b/src/RecognitionClient.cpp @@ -257,6 +257,7 @@ RecognitionClient::convertTopic(const std::string &topicName) { auto topicIter = validTopics.find(topicUpper); if (topicIter == validTopics.end()) { + ERROR("Unsupported topic: {}", topicName); throw UnknownTopicModel(topicUpper); } diff --git a/src/main.cpp b/src/main.cpp index e0aff42..2148064 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) { Configuration configuration(argc, argv); RecognitionClient client(configuration); client.performStreamingRecognition(); - } catch (GrpcException &e) { + } catch (std::exception &e) { ERROR(e.what()); return -1; } From 29a82d6f591b95c122458433a93a84c9609f0003 Mon Sep 17 00:00:00 2001 From: mjurado Date: Tue, 12 Sep 2023 16:28:37 +0200 Subject: [PATCH 2/3] Try to build missing dependencies --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18d4191..1e8d391 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ jobs: run: conan install cmake/3.16.4@ -g=virtualrunenv working-directory: ./build - name: Build - run: conan install -s compiler.libcxx=libstdc++ .. && cmake .. && cmake --build ./ --target all --parallel + run: conan install -s compiler.libcxx=libstdc++ --build=missing .. && cmake .. && cmake --build ./ --target all --parallel working-directory: ./build - name: Test run: ctest --parallel From 720553eea8c8e1fe7fb81df3bac91ab5ab6812a6 Mon Sep 17 00:00:00 2001 From: mjurado Date: Wed, 13 Sep 2023 10:52:27 +0200 Subject: [PATCH 3/3] Move string parameter value check to a new method --- headers/Configuration.h | 3 +++ src/Configuration.cpp | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/headers/Configuration.h b/headers/Configuration.h index e2ff49b..75deb78 100644 --- a/headers/Configuration.h +++ b/headers/Configuration.h @@ -48,6 +48,9 @@ class Configuration { void validate_configuration_values(); private: + + void validate_string_value(const char *name, const std::string &value, const std::vector &allowedValues); + std::string language; std::string topic; std::string grammarPath; diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 022c9c8..cedafc0 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -113,22 +113,24 @@ std::string Configuration::getClientSecret() const { void Configuration::validate_configuration_values() { if(sampleRate != 8000 and sampleRate != 16000) { - throw std::runtime_error("Unsupported sample rate value. Allowed values: 8000 or 1600"); + throw std::runtime_error("Unsupported parameter value. Allowed values sample rate: 8000 1600"); } - if (std::find(allowedTopicValues.begin(), allowedTopicValues.end(), topic) == allowedTopicValues.end()) - { - throw std::runtime_error("Unsupported topic value. Allowed values: GENERIC, BANKING, TELCO, INSURANCE"); - } - - if (std::find(allowedLanguageValues.begin(), allowedLanguageValues.end(), language) == allowedLanguageValues.end()) - { - throw std::runtime_error("Unsupported language value. Allowed values: en-US, en-GB, pt-BR, es, es-419, tr, ja, fr, fr-CA, de, it"); - } + validate_string_value("topic", topic, allowedTopicValues); + validate_string_value("language", language, allowedLanguageValues); + validate_string_value("asr version", asrVersion, allowedAsrVersionValues); +} + - if (std::find(allowedAsrVersionValues.begin(), allowedAsrVersionValues.end(), asrVersion) == allowedAsrVersionValues.end()) +void Configuration::validate_string_value(const char *name, const std::string &value, const std::vector &allowedValues) { + if (std::find(allowedValues.begin(), allowedValues.end(), value) == allowedValues.end()) { - throw std::runtime_error("Unsupported asr version value. Allowed values: V1, V2"); + std::stringstream ss; + std::copy(allowedValues.begin(), allowedValues.end(), std::ostream_iterator(ss, " ")); + std::string allowedValuesList = ss.str(); + std::string nameString(name); + std::string errorMessage("Unsupported parameter value. Allowed values for "); + std::string exceptionMessage = errorMessage + nameString.append(": ") + allowedValuesList; + throw std::runtime_error(exceptionMessage); } - } \ No newline at end of file