Skip to content
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

feat: support tags for options #4507

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vowpalwabbit/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(vw_config_sources
src/options_cli.cc
src/options_name_extractor.cc
src/options.cc
src/option.cc
)

vw_add_library(
Expand Down
6 changes: 6 additions & 0 deletions vowpalwabbit/config/include/vw/config/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class base_option
virtual void accept(typed_option_visitor& handler) = 0;

virtual ~base_option() = default;

VW_ATTR(nodiscard) const std::vector<std::string>& get_tags() const { return _tags; }
lalo marked this conversation as resolved.
Show resolved Hide resolved
void set_tags(std::vector<std::string> tags);

private:
std::vector<std::string> _tags;
};

template <typename T>
Expand Down
7 changes: 7 additions & 0 deletions vowpalwabbit/config/include/vw/config/option_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "option.h"

#include <initializer_list>
#include <memory>
#include <set>
#include <string>
Expand Down Expand Up @@ -125,6 +126,12 @@ class option_builder
return std::make_shared<T>(std::move(option._option_obj));
}

option_builder& tags(std::initializer_list<std::string> input_tags)
{
_option_obj.set_tags(input_tags);
return *this;
}

private:
T _option_obj;
};
Expand Down
32 changes: 32 additions & 0 deletions vowpalwabbit/config/src/option.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD (revised)
// license as described in the file LICENSE.

#include "vw/config/option.h"

#include <regex>

void VW::config::base_option::set_tags(std::vector<std::string> tags)
{
std::sort(tags.begin(), tags.end());
auto last = std::unique(tags.begin(), tags.end());
if (last != tags.end())
{
std::stringstream ss;
ss << "Duplicate tags found in option: " << m_name << ". Tags: ";
for (auto it = tags.begin(); it != last; ++it) { ss << *it << ", "; }
ss << *last;
THROW(ss.str());
}
for (const auto& tag : tags)
{
if (!std::regex_match(tag, std::regex("^[a-z_]+$")))
{
std::stringstream ss;
ss << "Invalid tag found in option: " << m_name << ". Tag: " << tag
<< ". Tags must be lowercase and contain only letters and underscores.";
THROW(ss.str());
}
}
_tags = std::move(tags);
}
12 changes: 12 additions & 0 deletions vowpalwabbit/config/tests/options_cli_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "vw/config/options_cli.h"

#include "vw/common/vw_exception.h"
#include "vw/config/cli_options_serializer.h"
#include "vw/test_common/test_common.h"

Expand Down Expand Up @@ -536,3 +537,14 @@ TEST(OptionsCli, CheckWasSuppliedCommonPrefixBefore)
EXPECT_TRUE(!options->was_supplied("int_opt"));
EXPECT_TRUE(options->was_supplied("int_opt_two"));
}

TEST(OptionsCli, MakeOptionTags)
{
int int_opt{};
option_group_definition arg_group("group1");
arg_group.add(make_option("int_opt", int_opt).tags({"taga"}));
ASSERT_THAT(arg_group.m_options.back()->get_tags(), ::testing::ElementsAre("taga"));

int int_opt2{};
EXPECT_THROW(arg_group.add(make_option("int_opt2", int_opt2).tags({"taga", "taga"})), VW::vw_exception);
}
23 changes: 23 additions & 0 deletions vowpalwabbit/config/tests/options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "vw/config/options.h"

#include "vw/config/option.h"
#include "vw/config/options_name_extractor.h"

#include <gmock/gmock.h>
Expand Down Expand Up @@ -189,3 +190,25 @@ TEST(Options, NameExtractionRecycle)
EXPECT_EQ(name_extractor.generated_name, "im_necessary_v2_opt2");
EXPECT_EQ(result, false);
}

TEST(Options, SetTags)
{
typed_option<bool> opt("my_opt");
opt.set_tags(std::vector<std::string>{"tagb", "taga", "tagc"});
ASSERT_THAT(opt.get_tags(), ::testing::ElementsAre("taga", "tagb", "tagc"));
}

TEST(Options, SetTagsDuplicate)
{
typed_option<bool> opt("my_opt");
EXPECT_THROW(opt.set_tags(std::vector<std::string>{"taga", "tagb", "tagb"}), VW::vw_exception);
}

TEST(Options, SetTagsInvalidName)
{
typed_option<bool> opt("my_opt");
EXPECT_THROW(opt.set_tags(std::vector<std::string>{"tag1"}), VW::vw_exception);
EXPECT_THROW(opt.set_tags(std::vector<std::string>{"Tag"}), VW::vw_exception);
EXPECT_THROW(opt.set_tags(std::vector<std::string>{"a b"}), VW::vw_exception);
EXPECT_THROW(opt.set_tags(std::vector<std::string>{"t-a-g"}), VW::vw_exception);
}