diff --git a/README.md b/README.md
index 9cfdfa223..627fc4c7c 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ reflect-cpp and sqlgen fill important gaps in C++ development. They reduce boile
- Simple [installation](https://munich-data-engineering.github.io/reflect-cpp/install)
- Simple extendability to [other serialization formats](https://munich-data-engineering.github.io/reflect-cpp/supported_formats/supporting_your_own_format)
- Simple extendability to [custom classes](https://munich-data-engineering.github.io/reflect-cpp/concepts/custom_classes)
+- Read-only field aliases via [`rfl::Alias`](https://munich-data-engineering.github.io/reflect-cpp/alias) for backwards-compatible schema and config migration
- Being one of the fastest serialization libraries in existence, as demonstrated by our [benchmarks](https://munich-data-engineering.github.io/reflect-cpp/benchmarks)
diff --git a/docs/content/_index.md b/docs/content/_index.md
index 056abfd96..89bff4427 100644
--- a/docs/content/_index.md
+++ b/docs/content/_index.md
@@ -16,6 +16,7 @@
{{< card link="rfl_ref" title="rfl::Box and rfl::Ref" icon="book-open" subtitle="For defining recursive structures.">}}
{{< card link="timestamps" title="rfl::Timestamp and std::chrono::duration" icon="book-open" subtitle="For serializing and deserializing timestamps and durations.">}}
{{< card link="rfl_skip" title="rfl::Skip" icon="book-open" subtitle="For skipping fields during serialization and/or deserialization.">}}
+ {{< card link="alias" title="rfl::Alias" icon="book-open" subtitle="For accepting read-only field aliases during deserialization.">}}
{{< card link="commented" title="rfl::Commented" icon="book-open" subtitle="For adding comments to your serialized format.">}}
{{< card link="result" title="rfl::Result" icon="book-open" subtitle="For error handling without exceptions.">}}
{{< card link="standard_containers" title="Standard containers" icon="book-open" subtitle="How reflect-cpp treats containers in the standard library.">}}
diff --git a/docs/content/alias.md b/docs/content/alias.md
new file mode 100644
index 000000000..1ab67bfca
--- /dev/null
+++ b/docs/content/alias.md
@@ -0,0 +1,79 @@
+# Read-only field aliases (`rfl::Alias`)
+
+The `rfl::Alias<"old-name", T>` wrapper declares a read-only alias for a struct field. During deserialization, `reflect-cpp` accepts **either** the canonical primary wire name **or** any declared alias. During serialization and JSON Schema generation, `reflect-cpp` **always** emits the canonical primary wire name.
+
+This is especially useful when evolving an API or configuration format (for example, migrating legacy `kebab-case` or `camelCase` RPC keys to `snake_case`, or supporting deprecated CLI flag names) without maintaining manual key-translation tables.
+
+## Basic usage
+
+```cpp
+#include
+#include
+
+struct Settings {
+ rfl::Alias<"download-dir", std::string> download_dir;
+ rfl::Alias<"speedLimitDown", int> speed_limit_down;
+};
+
+// Accepted on read (alias or canonical name):
+const auto s = rfl::json::read(
+ R"({"download-dir":"/downloads","speedLimitDown":250})").value();
+
+// Always serialized using the canonical primary names:
+const auto out = rfl::json::write(s);
+// {"download_dir":"/downloads","speed_limit_down":250}
+```
+
+## Multiple aliases
+
+Nest `rfl::Alias` wrappers to accept multiple legacy names for the same field:
+
+```cpp
+struct Settings {
+ rfl::Alias<"download-dir", rfl::Alias<"downloadDir", std::string>>
+ download_dir;
+};
+```
+
+Reading `{"download_dir": "/dl"}`, `{"download-dir": "/dl"}`, or `{"downloadDir": "/dl"}` all populate `download_dir`, and writing always emits `{"download_dir":"/dl"}`.
+
+## Semantics
+
+- **Primary wire name:** Unchanged. It comes from the C++ field name (transformed by naming processors such as `rfl::SnakeCaseToCamelCase` or `rfl::SnakeCaseToKebabCase`, or overridden by `rfl::Rename<"canonical", ...>`).
+- **Serialization (write):** Always writes the canonical primary wire name.
+- **JSON Schema (`to_schema`):** Emits only the canonical primary property name (preserving inner `rfl::Description`, `rfl::Deprecated`, `rfl::DefaultVal`, and `rfl::Validator` metadata).
+- **`rfl::NoExtraFields`:** Aliases are recognized as valid field names and are not rejected as unknown extra fields.
+- **Conflicting / duplicate input:** If an input object specifies both the primary name and an alias (or two different aliases for the same field), deserialization fails with an error instead of silently overwriting.
+- **Compile-time collision check:** If an alias on one field equals the primary wire name or an alias of a different field, compilation fails with a `static_assert`.
+
+## Composing with other wrappers and processors
+
+`rfl::Alias` is transparent and composes cleanly with `rfl::Rename`, `rfl::Description`, `rfl::Deprecated`, `rfl::DefaultVal`, `rfl::DefaultIfMissing`, `std::optional`, `rfl::Validator`, `rfl::Short`, `rfl::Positional`, and case-transforming processors:
+
+```cpp
+struct Config {
+ rfl::Rename<"download_dir", rfl::Alias<"download-dir", std::string>> dir;
+ rfl::Alias<"old-port", rfl::DefaultVal> port = 8080;
+ rfl::Description<"Timeout in seconds", rfl::Alias<"timeoutSec", std::optional>>
+ timeout_sec;
+};
+```
+
+## CLI support
+
+`rfl::cli::read` also recognizes `rfl::Alias` names as long options (including under `rfl::NoExtraFields`) and detects conflicts between aliases, canonical names, `rfl::Short`, and `rfl::Positional`:
+
+```cpp
+struct CliConfig {
+ rfl::Alias<"download_dir", rfl::Alias<"downloadDir", std::string>>
+ download_dir;
+ rfl::Alias<"debug", rfl::DefaultVal>> verbose = false;
+};
+
+// Accepts --download-dir=/dl, --download_dir=/dl, or --downloadDir=/dl
+// Accepts --verbose, -v, or --debug
+```
+
+## Wrapper ergonomics
+
+`rfl::Alias` provides `.value()`, `.get()`, `operator*`, `operator->`, `operator()`, implicit construction from convertible values, implicit conversion to `T&` / `const T&`, `==`, `<=>`, and `std::hash` specialization. Its default constructor is constrained on `std::is_default_constructible_v`.
diff --git a/docs/content/supported_formats/cli.md b/docs/content/supported_formats/cli.md
index 00b4002cc..cde2caedf 100644
--- a/docs/content/supported_formats/cli.md
+++ b/docs/content/supported_formats/cli.md
@@ -171,7 +171,7 @@ option in a token treats the next token as it would alone (`-qv false` sets
## Combining wrappers
`Short` and `Positional` can be nested with `DefaultVal`, `std::optional`,
-`Description`, `Rename` and `Deprecated` in any order:
+`Description`, `Rename`, `Alias` and `Deprecated` in any order:
`rfl::Description<"...", rfl::Short<"o", T>>` is `-o`, and
`rfl::DefaultVal>` is positional. Put `rfl::Rename`
outermost, as in `rfl::Rename<"out", rfl::Short<"o", T>>`; inside other
@@ -209,6 +209,7 @@ struct Config {
| Nested struct | `--parent.child=val` | Dot-separated path |
| `rfl::Flatten` | fields inlined | No prefix needed |
| `rfl::Rename<"x", T>` | `--x=val` | Bypasses kebab conversion |
+| `rfl::Alias<"old", T>` | `--old=val` | Read-only long alias (also works with `NoExtraFields`) |
| `rfl::Positional` | bare token | Matched in declaration order |
| `rfl::Short<"x", T>` | `-x value`, `-x=value` or `-xvalue` | Single-character alias, can be grouped (`-qv`) |
diff --git a/include/rfl.hpp b/include/rfl.hpp
index 432fff2f6..2162e0b65 100644
--- a/include/rfl.hpp
+++ b/include/rfl.hpp
@@ -9,6 +9,7 @@
#include "rfl/AddStructName.hpp"
#include "rfl/AddTagsToVariants.hpp"
+#include "rfl/Alias.hpp"
#include "rfl/AllOf.hpp"
#include "rfl/AllowRawPtrs.hpp"
#include "rfl/AnyOf.hpp"
diff --git a/include/rfl/Alias.hpp b/include/rfl/Alias.hpp
new file mode 100644
index 000000000..cf7144a29
--- /dev/null
+++ b/include/rfl/Alias.hpp
@@ -0,0 +1,245 @@
+#ifndef RFL_ALIAS_HPP_
+#define RFL_ALIAS_HPP_
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "default.hpp"
+#include "internal/StringLiteral.hpp"
+#include "internal/is_alias.hpp"
+
+namespace rfl {
+
+/// Declares a read-only field alias accepted during deserialization while
+/// always serializing (and generating schemas) under the canonical primary
+/// field name.
+///
+/// Multiple aliases can be declared by nesting `rfl::Alias`:
+/// `rfl::Alias<"download-dir", rfl::Alias<"downloadDir", std::string>>`
+///
+/// @tparam _name The compile-time string literal alias name accepted on read
+/// @tparam T The underlying value (or wrapped field) type
+template
+struct Alias {
+ /// The underlying type.
+ using Type = T;
+
+ /// The alias name as a compile-time string literal.
+ static constexpr internal::StringLiteral alias_name_ = _name;
+
+ /// Returns the alias name as a string_view.
+ static constexpr std::string_view alias_name() noexcept {
+ return alias_name_.string_view();
+ }
+
+ /// Default constructor - enabled only when Type is default-constructible.
+ template
+ requires std::is_default_constructible_v
+ constexpr Alias() noexcept(std::is_nothrow_default_constructible_v)
+ : value_() {}
+
+ /// Constructs from a const reference to the value.
+ constexpr Alias(const Type& _value) : value_(_value) {}
+
+ /// Constructs from an rvalue reference to the value.
+ constexpr Alias(Type&& _value) noexcept(
+ std::is_nothrow_move_constructible_v)
+ : value_(std::move(_value)) {}
+
+ /// Move constructor.
+ Alias(Alias&& _field) = default;
+
+ /// Copy constructor.
+ Alias(const Alias& _field) = default;
+
+ /// Copy constructor from Alias with the same alias name and a compatible
+ /// underlying type.
+ template
+ requires(!std::is_same_v && std::is_convertible_v)
+ constexpr Alias(const Alias<_name, U>& _field) : value_(_field.get()) {}
+
+ /// Move constructor from Alias with the same alias name and a compatible
+ /// underlying type.
+ template
+ requires(!std::is_same_v && std::is_convertible_v)
+ constexpr Alias(Alias<_name, U>&& _field) noexcept(
+ std::is_nothrow_constructible_v)
+ : value_(std::move(_field.value_)) {}
+
+ /// Constructs from any type convertible to Type.
+ template
+ requires(!std::is_same_v, Alias> &&
+ !std::is_same_v, Type> &&
+ !std::is_same_v, Default> &&
+ std::is_convertible_v)
+ constexpr Alias(U&& _value) noexcept(
+ std::is_nothrow_constructible_v)
+ : value_(std::forward(_value)) {}
+
+ /// Initializes the underlying object to its default value using the Default
+ /// sentinel.
+ template
+ requires std::is_default_constructible_v
+ constexpr Alias(const Default&) noexcept(
+ std::is_nothrow_default_constructible_v)
+ : value_() {}
+
+ /// Destructor.
+ ~Alias() = default;
+
+ /// Returns a const reference to the underlying value.
+ constexpr const Type& get() const noexcept { return value_; }
+
+ /// Returns a reference to the underlying value.
+ constexpr Type& get() noexcept { return value_; }
+
+ /// Dereference operator - returns the underlying value.
+ constexpr Type& operator*() noexcept { return value_; }
+
+ /// Dereference operator (const) - returns the underlying value.
+ constexpr const Type& operator*() const noexcept { return value_; }
+
+ /// Function call operator - returns the underlying value.
+ constexpr Type& operator()() noexcept { return value_; }
+
+ /// Function call operator (const) - returns the underlying value.
+ constexpr const Type& operator()() const noexcept { return value_; }
+
+ /// Pointer to the underlying value.
+ constexpr Type* operator->() noexcept { return &value_; }
+
+ /// Pointer to the underlying value (const).
+ constexpr const Type* operator->() const noexcept { return &value_; }
+
+ /// Implicit conversion to a mutable reference to the underlying value.
+ constexpr operator Type&() noexcept { return value_; }
+
+ /// Implicit conversion to a const reference to the underlying value.
+ constexpr operator const Type&() const noexcept { return value_; }
+
+ /// Assigns a new value (copy).
+ auto& operator=(const Type& _value) {
+ value_ = _value;
+ return *this;
+ }
+
+ /// Assigns a new value (move).
+ auto& operator=(Type&& _value) noexcept(
+ std::is_nothrow_move_assignable_v) {
+ value_ = std::move(_value);
+ return *this;
+ }
+
+ /// Assigns from any type convertible to the underlying type.
+ template
+ requires(!std::is_same_v, Alias> &&
+ !std::is_same_v, Type> &&
+ !std::is_same_v, Default> &&
+ std::is_convertible_v)
+ auto& operator=(U&& _value) {
+ value_ = std::forward(_value);
+ return *this;
+ }
+
+ /// Assigns the value to its default using the Default sentinel.
+ template
+ requires std::is_default_constructible_v
+ auto& operator=(const Default&) {
+ value_ = Type();
+ return *this;
+ }
+
+ /// Copy assignment operator.
+ Alias& operator=(const Alias& _field) = default;
+
+ /// Move assignment operator.
+ Alias& operator=(Alias&& _field) = default;
+
+ /// Assigns from another Alias with compatible underlying type (copy).
+ template
+ requires(!std::is_same_v && std::is_convertible_v)
+ auto& operator=(const Alias<_name, U>& _field) {
+ value_ = _field.get();
+ return *this;
+ }
+
+ /// Assigns from another Alias with compatible underlying type (move).
+ template
+ requires(!std::is_same_v && std::is_convertible_v)
+ auto& operator=(Alias<_name, U>&& _field) {
+ value_ = std::move(_field.value_);
+ return *this;
+ }
+
+ /// Sets the value (copy).
+ void set(const Type& _value) { value_ = _value; }
+
+ /// Sets the value (move).
+ void set(Type&& _value) noexcept(std::is_nothrow_move_assignable_v) {
+ value_ = std::move(_value);
+ }
+
+ /// Returns a reference to the underlying value.
+ constexpr Type& value() noexcept { return value_; }
+
+ /// Returns a const reference to the underlying value.
+ constexpr const Type& value() const noexcept { return value_; }
+
+ /// The underlying value.
+ Type value_;
+};
+
+/// Equality comparison between two Alias wrappers.
+template
+ requires requires(const T1& _a, const T2& _b) { _a == _b; }
+constexpr bool operator==(const Alias<_n1, T1>& _lhs,
+ const Alias<_n2, T2>& _rhs) {
+ return _lhs.value() == _rhs.value();
+}
+
+/// Three-way comparison between two Alias wrappers.
+template
+ requires requires(const T1& _a, const T2& _b) { _a <=> _b; }
+constexpr auto operator<=>(const Alias<_n1, T1>& _lhs,
+ const Alias<_n2, T2>& _rhs) {
+ return _lhs.value() <=> _rhs.value();
+}
+
+/// Equality comparison between an Alias wrapper and a non-Alias value.
+template
+ requires(!internal::is_alias>::value &&
+ requires(const T& _a, const U& _b) { _a == _b; })
+constexpr bool operator==(const Alias<_n, T>& _lhs, const U& _rhs) {
+ return _lhs.value() == _rhs;
+}
+
+/// Three-way comparison between an Alias wrapper and a non-Alias value.
+template
+ requires(!internal::is_alias>::value &&
+ requires(const T& _a, const U& _b) { _a <=> _b; })
+constexpr auto operator<=>(const Alias<_n, T>& _lhs, const U& _rhs) {
+ return _lhs.value() <=> _rhs;
+}
+
+} // namespace rfl
+
+namespace std {
+
+/// Specialization of std::hash for rfl::Alias.
+template
+ requires requires(const T& _v) { std::hash{}(_v); }
+struct hash> {
+ size_t operator()(const rfl::Alias<_name, T>& _a) const {
+ return hash{}(_a.value());
+ }
+};
+
+} // namespace std
+
+#endif
diff --git a/include/rfl/CamelCaseToSnakeCase.hpp b/include/rfl/CamelCaseToSnakeCase.hpp
index dcc15d9e8..7ec29c06f 100644
--- a/include/rfl/CamelCaseToSnakeCase.hpp
+++ b/include/rfl/CamelCaseToSnakeCase.hpp
@@ -2,6 +2,7 @@
#define RFL_CAMELCASETOSNAKECASE_HPP_
#include "Field.hpp"
+#include "internal/is_alias.hpp"
#include "internal/is_rename.hpp"
#include "internal/transform_case.hpp"
@@ -18,7 +19,8 @@ struct CamelCaseToSnakeCase {
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([](const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
- !internal::is_rename_v) {
+ !internal::is_rename_v<
+ internal::remove_alias_t>) {
return handle_one_field(_f);
} else {
return _f;
diff --git a/include/rfl/SnakeCaseToCamelCase.hpp b/include/rfl/SnakeCaseToCamelCase.hpp
index d073b0b4a..328a49129 100644
--- a/include/rfl/SnakeCaseToCamelCase.hpp
+++ b/include/rfl/SnakeCaseToCamelCase.hpp
@@ -2,6 +2,7 @@
#define RFL_SNAKECASETOCAMELCASE_HPP_
#include "Field.hpp"
+#include "internal/is_alias.hpp"
#include "internal/is_rename.hpp"
#include "internal/transform_case.hpp"
@@ -18,7 +19,8 @@ struct SnakeCaseToCamelCase {
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([](const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
- !internal::is_rename_v) {
+ !internal::is_rename_v<
+ internal::remove_alias_t>) {
return handle_one_field(_f);
} else {
return _f;
diff --git a/include/rfl/SnakeCaseToKebabCase.hpp b/include/rfl/SnakeCaseToKebabCase.hpp
index 4b9d09a6f..00a480dd3 100644
--- a/include/rfl/SnakeCaseToKebabCase.hpp
+++ b/include/rfl/SnakeCaseToKebabCase.hpp
@@ -2,6 +2,7 @@
#define RFL_SNAKECASETOKEBABCASE_HPP_
#include "Field.hpp"
+#include "internal/is_alias.hpp"
#include "internal/is_rename.hpp"
#include "internal/transform_case.hpp"
@@ -20,7 +21,8 @@ struct SnakeCaseToKebabCase {
// "xml_content" is a reserved name used by the XML Reader/Writer;
// all case-transform processors must leave it unchanged.
if constexpr (FieldType::name() != "xml_content" &&
- !internal::is_rename_v) {
+ !internal::is_rename_v<
+ internal::remove_alias_t>) {
return handle_one_field(_f);
} else {
return _f;
diff --git a/include/rfl/SnakeCaseToPascalCase.hpp b/include/rfl/SnakeCaseToPascalCase.hpp
index f10361da0..8194d8b9c 100644
--- a/include/rfl/SnakeCaseToPascalCase.hpp
+++ b/include/rfl/SnakeCaseToPascalCase.hpp
@@ -2,6 +2,7 @@
#define RFL_SNAKECASETOPASCALCASE_HPP_
#include "Field.hpp"
+#include "internal/is_alias.hpp"
#include "internal/is_rename.hpp"
#include "internal/transform_case.hpp"
@@ -18,7 +19,8 @@ struct SnakeCaseToPascalCase {
static auto process(const auto& _named_tuple) {
const auto handle_one = [](const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
- !internal::is_rename_v) {
+ !internal::is_rename_v<
+ internal::remove_alias_t>) {
return handle_one_field(_f);
} else {
return _f;
diff --git a/include/rfl/ToAllCaps.hpp b/include/rfl/ToAllCaps.hpp
index c86bad16b..2ebc60827 100644
--- a/include/rfl/ToAllCaps.hpp
+++ b/include/rfl/ToAllCaps.hpp
@@ -2,6 +2,7 @@
#define RFL_TOALLCAPS_HPP_
#include "Field.hpp"
+#include "internal/is_alias.hpp"
#include "internal/is_rename.hpp"
#include "internal/transform_case.hpp"
@@ -18,7 +19,8 @@ struct ToAllCaps {
static auto process(const auto& _named_tuple) {
return _named_tuple.transform([](const FieldType& _f) {
if constexpr (FieldType::name() != "xml_content" &&
- !internal::is_rename_v) {
+ !internal::is_rename_v<
+ internal::remove_alias_t>) {
return handle_one_field(_f);
} else {
return _f;
diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp
index bfa62c928..075ef9b26 100644
--- a/include/rfl/cli/options.hpp
+++ b/include/rfl/cli/options.hpp
@@ -10,6 +10,7 @@
#include
#include
+#include "../Alias.hpp"
#include "../DefaultVal.hpp"
#include "../Deprecated.hpp"
#include "../Description.hpp"
@@ -18,8 +19,10 @@
#include "../Short.hpp"
#include "../Tuple.hpp"
#include "../internal/StringLiteral.hpp"
+#include "../internal/field_aliases.hpp"
#include "../internal/has_reflection_type_v.hpp"
#include "../internal/has_reflector.hpp"
+#include "../internal/is_alias.hpp"
#include "../internal/is_extra_fields.hpp"
#include "../internal/is_named_tuple.hpp"
#include "../internal/is_positional.hpp"
@@ -58,9 +61,13 @@ struct Option {
/// aliases and positional arguments to fields. Names are the field names
/// after processors (kebab-case, rfl::Rename) are applied.
struct Options {
- /// The options of the top-level fields, by name.
+ /// The options of the top-level fields, by name (including rfl::Alias names).
std::map long_options;
+ /// Read-only long option aliases given by rfl::Alias (`old-name` for
+ /// `--old-name`), mapped to the canonical name of their field.
+ std::map long_aliases;
+
/// The short aliases given by rfl::Short (`p` for `-p`), mapped to the path
/// of their field.
std::map short_options;
@@ -140,6 +147,9 @@ struct cli_annotations>
template
struct cli_annotations> : cli_annotations_t {};
+template
+struct cli_annotations> : cli_annotations_t {};
+
template
struct cli_annotations> : cli_annotations_t {
static constexpr bool is_short = false;
@@ -214,6 +224,7 @@ auto unwrap_option_type() {
}
else if constexpr (rfl::internal::is_short_v
|| rfl::internal::is_positional_v
+ || rfl::internal::is_alias_v
|| parsing::is_rename_v
|| parsing::is_default_val_v
|| parsing::is_skip_v) {
@@ -287,7 +298,18 @@ template
void add_option_for_field(Options* _options) {
using T = std::remove_cvref_t;
if constexpr (!rfl::internal::is_extra_fields_v) {
- add_option(std::string(FieldType::name()), _options);
+ const auto canonical = std::string(FieldType::name());
+ add_option(canonical, _options);
+ using Aliases = rfl::internal::field_aliases_t;
+ if constexpr (Aliases::size > 0) {
+ for (const auto alias : Aliases::to_array()) {
+ const auto alias_str = std::string(alias);
+ add_option(alias_str, _options);
+ if (alias_str != canonical) {
+ _options->long_aliases.emplace(alias_str, canonical);
+ }
+ }
+ }
}
}
@@ -295,6 +317,9 @@ void add_option_for_field(Options* _options) {
template
void collect_options(Options* _options) {
using NT = option_fields_t;
+ static_assert(
+ rfl::internal::no_alias_collisions(),
+ "Field alias collides with another field's name or alias.");
[&](std::index_sequence<_is...>) {
(add_option_for_field,
ProcessorsType>(_options),
diff --git a/include/rfl/cli/resolve_args.hpp b/include/rfl/cli/resolve_args.hpp
index 1bd79d9eb..fd7433074 100644
--- a/include/rfl/cli/resolve_args.hpp
+++ b/include/rfl/cli/resolve_args.hpp
@@ -87,7 +87,28 @@ inline rfl::Unexpected too_many_positionals(const size_t _expected,
inline rfl::Result> resolve_args(
ParsedArgs _parsed, const Options& _options
) {
- auto& result = _parsed.named;
+ std::map result;
+ std::map seen_flag;
+ if (_options.long_aliases.empty()) {
+ result = std::move(_parsed.named);
+ } else {
+ for (auto& [key, val] : _parsed.named) {
+ const auto it = _options.long_aliases.find(key);
+ const auto& target =
+ it == _options.long_aliases.end() ? key : it->second;
+ if (!result.emplace(target, std::move(val)).second) {
+ const auto seen_it = seen_flag.find(target);
+ const auto& prev =
+ seen_it != seen_flag.end() ? seen_it->second : target;
+ return rfl::error(
+ "Conflict: '--" + key + "' and '--" + prev
+ + "' refer to the same field.");
+ }
+ if (key != target) {
+ seen_flag.emplace(target, key);
+ }
+ }
+ }
// Merge positional args.
const auto& positional_names = _options.positionals;
@@ -98,8 +119,11 @@ inline rfl::Result> resolve_args(
for (size_t i = 0; i < _parsed.positional.size(); ++i) {
const auto& long_name = positional_names[i];
if (result.count(long_name)) {
+ const auto flag_it = seen_flag.find(long_name);
+ const auto& flag_name =
+ flag_it != seen_flag.end() ? flag_it->second : long_name;
return rfl::error(
- "Conflict: positional argument and '--" + long_name
+ "Conflict: positional argument and '--" + flag_name
+ "' both provided.");
}
result.emplace(long_name, std::move(_parsed.positional[i]));
@@ -113,8 +137,11 @@ inline rfl::Result> resolve_args(
}
const auto& long_name = it->second;
if (result.count(long_name)) {
+ const auto flag_it = seen_flag.find(long_name);
+ const auto& flag_name =
+ flag_it != seen_flag.end() ? flag_it->second : long_name;
return rfl::error(
- "Conflict: '-" + short_name + "' and '--" + long_name
+ "Conflict: '-" + short_name + "' and '--" + flag_name
+ "' both provided.");
}
result.emplace(long_name, std::move(value));
diff --git a/include/rfl/env/Reader.hpp b/include/rfl/env/Reader.hpp
index 68545f790..c98e00516 100644
--- a/include/rfl/env/Reader.hpp
+++ b/include/rfl/env/Reader.hpp
@@ -11,8 +11,11 @@
#include
#include
#include
+#include
#include "../Result.hpp"
+#include "../Tuple.hpp"
+#include "../internal/field_aliases.hpp"
#if defined(__APPLE__)
#include
@@ -230,15 +233,51 @@ struct Reader {
if constexpr (has_view_type) {
using ViewType = typename std::remove_cvref_t::ViewType;
- using NamesType = typename ViewType::Names;
- const auto names = NamesType::names();
- for (const auto& name : names) {
- const auto child_path = _obj.prefix.empty() ? name : _obj.prefix + name;
- const auto var = InputVarType{.path = child_path};
- if (!is_empty(var)) {
- _object_reader.read(std::string_view(name), var);
- }
- }
+ using FieldsType = typename ViewType::Fields;
+ [&](std::index_sequence) {
+ (
+ [&]() {
+ using FieldType =
+ std::remove_cvref_t>;
+ const std::string primary(FieldType::name());
+ const auto child_path =
+ _obj.prefix.empty() ? primary : _obj.prefix + primary;
+ const auto var = InputVarType{.path = child_path};
+ if (!is_empty(var)) {
+ _object_reader.read(std::string_view(primary), var);
+ }
+ using Aliases =
+ rfl::internal::field_aliases_t;
+ if constexpr (Aliases::size > 0) {
+ constexpr auto aliases = Aliases::to_array();
+ for (size_t idx = 0; idx < aliases.size(); ++idx) {
+ const auto alias = aliases[idx];
+ if (alias == FieldType::name()) {
+ continue;
+ }
+ bool duplicate = false;
+ for (size_t prev = 0; prev < idx; ++prev) {
+ if (aliases[prev] == alias) {
+ duplicate = true;
+ break;
+ }
+ }
+ if (duplicate) {
+ continue;
+ }
+ const std::string alias_str(alias);
+ const auto alias_path = _obj.prefix.empty()
+ ? alias_str
+ : _obj.prefix + alias_str;
+ const auto alias_var = InputVarType{.path = alias_path};
+ if (!is_empty(alias_var)) {
+ _object_reader.read(alias, alias_var);
+ }
+ }
+ }
+ }.template operator()(),
+ ...);
+ }(std::make_index_sequence>());
} else {
for (char** env = get_environ(); *env != nullptr; ++env) {
diff --git a/include/rfl/internal/cpp20/get_field_names.hpp b/include/rfl/internal/cpp20/get_field_names.hpp
index 707a0590a..2b3c85cf9 100644
--- a/include/rfl/internal/cpp20/get_field_names.hpp
+++ b/include/rfl/internal/cpp20/get_field_names.hpp
@@ -12,6 +12,7 @@
#include "../../Literal.hpp"
#include "../concat_literals.hpp"
+#include "../is_alias.hpp"
#include "../is_flatten_field.hpp"
#include "../is_rename.hpp"
#include "get_ith_field_from_fake_object.hpp"
@@ -88,8 +89,9 @@ consteval auto get_field_name() {
#else
using Type = std::remove_cvref_t>;
#endif
- if constexpr (is_rename_v) {
- using Name = typename Type::Name;
+ using UnwrappedType = remove_alias_t;
+ if constexpr (is_rename_v) {
+ using Name = typename UnwrappedType::Name;
return Name();
} else if constexpr (is_flatten_field_v) {
return get_field_names>();
diff --git a/include/rfl/internal/cpp20/get_member_ptr_name.hpp b/include/rfl/internal/cpp20/get_member_ptr_name.hpp
index 37e3a81da..d8c1b5ac9 100644
--- a/include/rfl/internal/cpp20/get_member_ptr_name.hpp
+++ b/include/rfl/internal/cpp20/get_member_ptr_name.hpp
@@ -5,6 +5,7 @@
#include
#include "../StringLiteral.hpp"
+#include "../is_alias.hpp"
#include "../is_field.hpp"
#include "../is_rename.hpp"
#include "../lit_name.hpp"
@@ -66,8 +67,9 @@ using ptr_type_t = member_type_helper::ptr_type;
template
consteval auto get_member_ptr_name() {
using Type = ptr_type_t;
- if constexpr (is_rename_v || is_field_v) {
- using Name = typename Type::Name;
+ using UnwrappedType = remove_alias_t;
+ if constexpr (is_rename_v || is_field_v) {
+ using Name = typename UnwrappedType::Name;
return lit_name_v;
} else {
using StructType = struct_type_t;
diff --git a/include/rfl/internal/cpp26/get_field_names.hpp b/include/rfl/internal/cpp26/get_field_names.hpp
index 07d872f84..e82993589 100644
--- a/include/rfl/internal/cpp26/get_field_names.hpp
+++ b/include/rfl/internal/cpp26/get_field_names.hpp
@@ -13,6 +13,7 @@
#include "../../Tuple.hpp"
#include "../StringLiteral.hpp"
#include "../concat_literals.hpp"
+#include "../is_alias.hpp"
#include "../is_flatten_field.hpp"
#include "../is_rename.hpp"
#include "bind_to_tuple.hpp"
@@ -26,8 +27,9 @@ consteval auto get_field_names();
template
consteval auto get_field_names_impl() {
using Type = std::remove_cvref_t>;
- if constexpr (is_rename_v) {
- using Name = typename Type::Name;
+ using UnwrappedType = remove_alias_t;
+ if constexpr (is_rename_v) {
+ using Name = typename UnwrappedType::Name;
return Name();
} else if constexpr (is_flatten_field_v) {
return get_field_names>();
diff --git a/include/rfl/internal/field_aliases.hpp b/include/rfl/internal/field_aliases.hpp
new file mode 100644
index 000000000..a3037234f
--- /dev/null
+++ b/include/rfl/internal/field_aliases.hpp
@@ -0,0 +1,223 @@
+#ifndef RFL_INTERNAL_FIELD_ALIASES_HPP_
+#define RFL_INTERNAL_FIELD_ALIASES_HPP_
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "../Alias.hpp"
+#include "../Attribute.hpp"
+#include "../Box.hpp"
+#include "../Commented.hpp"
+#include "../DefaultVal.hpp"
+#include "../Deprecated.hpp"
+#include "../Description.hpp"
+#include "../Positional.hpp"
+#include "../Ref.hpp"
+#include "../Rename.hpp"
+#include "../Short.hpp"
+#include "../Skip.hpp"
+#include "../Tuple.hpp"
+#include "../Validator.hpp"
+#include "StringLiteral.hpp"
+
+namespace rfl::internal {
+
+/// Compile-time list of read-only alias names declared on a field.
+template
+struct AliasList {
+ static constexpr size_t size = sizeof...(_names);
+
+ static constexpr std::array
+ to_array() noexcept {
+ return {_names.string_view()...};
+ }
+
+ static constexpr bool contains(std::string_view _name) noexcept {
+ return ((_name == _names.string_view()) || ...);
+ }
+};
+
+template
+struct prepend_alias;
+
+template
+struct prepend_alias<_head, AliasList<_tail...>> {
+ using type = AliasList<_head, _tail...>;
+};
+
+/// Collects all rfl::Alias names declared on a field type, unwrapping through
+/// transparent field wrappers (Alias, Rename, Description, Deprecated,
+/// DefaultVal, Short, Positional, std::optional, Skip, Attribute, Commented,
+/// Validator, Box, Ref, std::unique_ptr, std::shared_ptr, and raw pointers).
+template
+struct field_aliases_impl {
+ using type = AliasList<>;
+};
+
+template
+using field_aliases_t = typename field_aliases_impl<
+ std::remove_cvref_t>>::type;
+
+template
+struct field_aliases_impl {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = typename prepend_alias<_name, field_aliases_t>::type;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl[> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+template
+struct field_aliases_impl> {
+ using type = field_aliases_t;
+};
+
+/// Returns true if any field in Fields declares at least one rfl::Alias.
+template
+consteval bool has_any_aliases() {
+ constexpr size_t N = rfl::tuple_size_v;
+ return [](std::index_sequence) {
+ return (false || ... ||
+ (field_aliases_t>::Type>::size > 0));
+ }(std::make_index_sequence());
+}
+
+/// Checks at compile time that no alias on field A equals the primary wire name
+/// or any alias on a different field B.
+template
+consteval bool no_alias_collisions() {
+ constexpr size_t N = rfl::tuple_size_v;
+ if constexpr (N <= 1 || !has_any_aliases()) {
+ return true;
+ } else {
+ constexpr auto primary_names =
+ [](std::index_sequence) {
+ return std::array{
+ std::remove_cvref_t>::name()...};
+ }(std::make_index_sequence());
+
+ constexpr size_t total_aliases =
+ [](std::index_sequence) {
+ return (size_t{0} + ... +
+ field_aliases_t>::Type>::size);
+ }(std::make_index_sequence());
+
+ struct AliasEntry {
+ size_t field_idx;
+ std::string_view name;
+ };
+
+ std::array all_aliases{};
+ size_t pos = 0;
+ [&](std::index_sequence) {
+ (
+ [&]() {
+ constexpr auto arr =
+ field_aliases_t>::Type>::to_array();
+ for (const auto sv : arr) {
+ all_aliases[pos++] = AliasEntry{I, sv};
+ }
+ }.template operator()(),
+ ...);
+ }(std::make_index_sequence());
+
+ for (size_t a = 0; a < total_aliases; ++a) {
+ for (size_t f = 0; f < N; ++f) {
+ if (f != all_aliases[a].field_idx &&
+ all_aliases[a].name == primary_names[f]) {
+ return false;
+ }
+ }
+ for (size_t b = a + 1; b < total_aliases; ++b) {
+ if (all_aliases[a].field_idx != all_aliases[b].field_idx &&
+ all_aliases[a].name == all_aliases[b].name) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+}
+
+} // namespace rfl::internal
+
+#endif
diff --git a/include/rfl/internal/is_alias.hpp b/include/rfl/internal/is_alias.hpp
new file mode 100644
index 000000000..61283b886
--- /dev/null
+++ b/include/rfl/internal/is_alias.hpp
@@ -0,0 +1,42 @@
+#ifndef RFL_INTERNAL_ISALIAS_HPP_
+#define RFL_INTERNAL_ISALIAS_HPP_
+
+#include
+
+#include "StringLiteral.hpp"
+
+namespace rfl {
+
+template
+struct Alias;
+
+namespace internal {
+
+template
+class is_alias : public std::false_type {};
+
+template
+class is_alias> : public std::true_type {};
+
+template
+constexpr bool is_alias_v =
+ is_alias>>::value;
+
+template
+struct remove_alias {
+ using Type = T;
+};
+
+template
+struct remove_alias]