-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bdca9b
commit 54061a3
Showing
5 changed files
with
119 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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,34 @@ | ||
#ifndef RFL_SNAKECASETOHUNGARIANCASE_HPP_ | ||
#define RFL_SNAKECASETOHUNGARIANCASE_HPP_ | ||
|
||
#include "Field.hpp" | ||
#include "internal/transform_snake_case.hpp" | ||
|
||
namespace rfl { | ||
|
||
struct SnakeCaseToHungarianCase { | ||
public: | ||
/// Replaces all instances of snake_case field names with CamelCase. | ||
template <class StructType> | ||
static auto process(auto&& _named_tuple) { | ||
const auto handle_one = [](auto&& _f) { | ||
return handle_one_field(std::move(_f)); | ||
}; | ||
return _named_tuple.transform(handle_one); | ||
} | ||
|
||
private: | ||
/// Applies the logic to a single field. | ||
template <class FieldType> | ||
static auto handle_one_field(FieldType&& _f) { | ||
using NewFieldType = | ||
Field<internal::transform_snake_case<FieldType::name_, | ||
/*capitalize=*/false>(), | ||
typename FieldType::Type>; | ||
return NewFieldType(_f.value()); | ||
} | ||
}; | ||
|
||
} // namespace rfl | ||
|
||
#endif |
This file contains 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 @@ | ||
#ifndef RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_ | ||
#define RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_ | ||
|
||
#include "StringLiteral.hpp" | ||
|
||
namespace rfl::internal { | ||
|
||
/// Capitalizes a lower-case character. | ||
template <char c> | ||
consteval char to_upper() { | ||
if constexpr (c >= 'a' && c <= 'z') { | ||
return c + ('A' - 'a'); | ||
} else { | ||
return c; | ||
} | ||
} | ||
|
||
/// Transforms the field name from snake case to camel case. | ||
template <internal::StringLiteral _name, bool _capitalize, size_t _i = 0, | ||
char... chars> | ||
consteval auto transform_snake_case() { | ||
if constexpr (_i == _name.arr_.size()) { | ||
return StringLiteral<sizeof...(chars) + 1>(chars...); | ||
} else if constexpr (_name.arr_[_i] == '_') { | ||
return transform_snake_case<_name, true, _i + 1, chars...>(); | ||
} else if constexpr (_capitalize) { | ||
return transform_snake_case<_name, false, _i + 1, chars..., | ||
to_upper<_name.arr_[_i]>()>(); | ||
} else { | ||
return transform_snake_case<_name, false, _i + 1, chars..., | ||
_name.arr_[_i]>(); | ||
} | ||
} | ||
} // namespace rfl::internal | ||
|
||
#endif |
This file contains 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,40 @@ | ||
#include <iostream> | ||
#include <rfl.hpp> | ||
#include <rfl/json.hpp> | ||
#include <source_location> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "write_and_read.hpp" | ||
|
||
namespace test_snake_case_to_hungarian_case { | ||
|
||
struct Person { | ||
std::string first_name; | ||
std::string last_name; | ||
rfl::Timestamp<"%Y-%m-%d"> birthday; | ||
std::vector<Person> children; | ||
}; | ||
|
||
TEST(json, test_snake_case_to_hungarian_case) { | ||
const auto bart = Person{ | ||
.first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; | ||
|
||
const auto lisa = Person{ | ||
.first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; | ||
|
||
const auto maggie = Person{ | ||
.first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; | ||
|
||
const auto homer = | ||
Person{.first_name = "Homer", | ||
.last_name = "Simpson", | ||
.birthday = "1987-04-19", | ||
.children = std::vector<Person>({bart, lisa, maggie})}; | ||
|
||
const auto named_tuple = | ||
rfl::to_named_tuple<rfl::SnakeCaseToHungarianCase>(homer); | ||
|
||
std::cout << rfl::json::write(named_tuple, rfl::json::pretty) << std::endl; | ||
} | ||
} // namespace test_snake_case_to_hungarian_case |