-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add ApplicationTags #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f6f748
feat: add ApplicationTags
cwaldren-ld 751876d
Add more tests
cwaldren-ld 26dfed2
Update config tests
cwaldren-ld cc0db62
fix some lints
cwaldren-ld 844a612
add .clang-tidy to short var names like 'c' for char
cwaldren-ld 9d4117b
Add more readability-identifier-length ignore patterns
cwaldren-ld 0edd108
public/private order cleanup
cwaldren-ld 4385344
Fix free function naming + add os to .clang-tidy param ignore
cwaldren-ld 1a724bd
add error handling to application info using tl::expected
cwaldren-ld a64a5a4
fix lints
cwaldren-ld 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| CheckOptions: | ||
| - { key: readability-identifier-length.IgnoredParameterNames, value: 'i|j|k|c|os|it' } | ||
| - { key: readability-identifier-length.IgnoredLoopCounterNames, value: 'i|j|k|c|os|it' } |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #pragma once | ||
|
|
||
| #include "logger.hpp" | ||
|
|
||
| #include <optional> | ||
| #include <string> | ||
| #include <tl/expected.hpp> | ||
| #include <vector> | ||
| #include "error.hpp" | ||
|
|
||
| namespace launchdarkly::config::detail { | ||
|
|
||
| class ApplicationInfo { | ||
| public: | ||
| ApplicationInfo() = default; | ||
| ApplicationInfo& app_identifier(std::string app_id); | ||
| ApplicationInfo& app_version(std::string version); | ||
| [[nodiscard]] std::optional<std::string> build(Logger& logger) const; | ||
|
|
||
| private: | ||
| struct Tag { | ||
| std::string key; | ||
| std::string value; | ||
| std::optional<Error> error; | ||
| Tag(std::string key, std::string value); | ||
| [[nodiscard]] tl::expected<std::string, Error> build() const; | ||
| }; | ||
| std::vector<Tag> tags_; | ||
| ApplicationInfo& add_tag(std::string key, std::string value); | ||
| }; | ||
|
|
||
| bool ValidChar(char c); | ||
| std::optional<Error> IsValidTag(std::string const& key, | ||
| std::string const& value); | ||
|
|
||
| } // namespace launchdarkly::config::detail | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #include "config/detail/application_info.hpp" | ||
|
|
||
| #include <boost/algorithm/string.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <cctype> | ||
|
|
||
| namespace launchdarkly::config::detail { | ||
|
|
||
| // Defines the maximum character length for an Application Tag value. | ||
| constexpr std::size_t kMaxTagValueLength = 64; | ||
|
|
||
| ApplicationInfo::Tag::Tag(std::string key, std::string value) | ||
| : key(std::move(key)), value(std::move(value)) {} | ||
|
|
||
| tl::expected<std::string, Error> ApplicationInfo::Tag::build() const { | ||
| if (auto err = IsValidTag(key, value)) { | ||
| return tl::unexpected(*err); | ||
| } | ||
| return key + '/' + value; | ||
| } | ||
|
|
||
| bool ValidChar(char c) { | ||
| return std::isalnum(c) != 0 || c == '-' || c == '.' || c == '_'; | ||
| } | ||
|
|
||
| std::optional<Error> IsValidTag(std::string const& key, | ||
| std::string const& value) { | ||
| if (value.empty() || key.empty()) { | ||
| return Error::kConfig_ApplicationInfo_EmptyKeyOrValue; | ||
| } | ||
| if (value.length() > kMaxTagValueLength) { | ||
| return Error::kConfig_ApplicationInfo_ValueTooLong; | ||
| } | ||
| if (!std::all_of(key.begin(), key.end(), ValidChar)) { | ||
| return Error::kConfig_ApplicationInfo_InvalidKeyCharacters; | ||
| } | ||
| if (!std::all_of(value.begin(), value.end(), ValidChar)) { | ||
| return Error::kConfig_ApplicationInfo_InvalidValueCharacters; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| ApplicationInfo& ApplicationInfo::add_tag(std::string key, std::string value) { | ||
| tags_.emplace_back(std::move(key), std::move(value)); | ||
| return *this; | ||
|
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. Will update once the |
||
| } | ||
| ApplicationInfo& ApplicationInfo::app_identifier(std::string app_id) { | ||
| return add_tag("application-id", std::move(app_id)); | ||
| } | ||
|
|
||
| ApplicationInfo& ApplicationInfo::app_version(std::string version) { | ||
| return add_tag("application-version", std::move(version)); | ||
| } | ||
|
|
||
| std::optional<std::string> ApplicationInfo::build(Logger& logger) const { | ||
| if (tags_.empty()) { | ||
| LD_LOG(logger, LogLevel::kDebug) << "no application tags configured"; | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| // Build all the tags, which results in pairs of (tag key, value | error). | ||
| std::vector<std::pair<std::string, tl::expected<std::string, Error>>> | ||
| unvalidated(tags_.size()); | ||
|
|
||
| std::transform( | ||
| tags_.cbegin(), tags_.cend(), unvalidated.begin(), | ||
| [](auto tag) { return std::make_pair(tag.key, tag.build()); }); | ||
|
|
||
| std::vector<std::string> validated; | ||
|
|
||
| for (auto const& tag : unvalidated) { | ||
| if (!tag.second) { | ||
| LD_LOG(logger, LogLevel::kWarn) | ||
| << tag.second.error() << " for tag '" << tag.first << "'"; | ||
| } else { | ||
| validated.push_back(tag.second.value()); | ||
| } | ||
| } | ||
|
|
||
| if (validated.empty()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| // Sort so the result is deterministic. | ||
| std::sort(validated.begin(), validated.end()); | ||
|
|
||
| // Remove any duplicate tags. | ||
| validated.erase(std::unique(validated.begin(), validated.end()), | ||
| validated.end()); | ||
|
|
||
| // Concatenate with space as the delimiter. | ||
| return boost::algorithm::join(validated, " "); | ||
| } | ||
|
|
||
| } // namespace launchdarkly::config::detail | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.