-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Began developing the JSON schema definition; #25
- Loading branch information
1 parent
5b7dab4
commit 4698ab9
Showing
4 changed files
with
155 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef RFL_INTERNAL_SCHEMA_DEFINITION_HPP_ | ||
#define RFL_INTERNAL_SCHEMA_DEFINITION_HPP_ | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
#include "Type.hpp" | ||
|
||
namespace rfl::internal::schema { | ||
|
||
struct Definition { | ||
/// Contains the root element of the schema definition. | ||
Type root_; | ||
|
||
/// Contains the definitions to be referenced by Type::Reference. | ||
std::map<std::string, Type> definitions_; | ||
}; | ||
|
||
} // namespace rfl::internal::schema | ||
|
||
#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,62 @@ | ||
#ifndef RFL_INTERNAL_SCHEMA_TYPE_HPP_ | ||
#define RFL_INTERNAL_SCHEMA_TYPE_HPP_ | ||
|
||
#include <map> | ||
#include <string> | ||
#include <variant> | ||
#include <vector> | ||
|
||
#include "../../Ref.hpp" | ||
|
||
namespace rfl::internal::schema { | ||
|
||
struct Type { | ||
struct Int32 {}; | ||
|
||
struct Int64 {}; | ||
|
||
struct UInt32 {}; | ||
|
||
struct UInt64 {}; | ||
|
||
struct Integer {}; | ||
|
||
struct Float {}; | ||
|
||
struct Double {}; | ||
|
||
struct String {}; | ||
|
||
struct Object { | ||
std::map<std::string, Type> types_; | ||
}; | ||
|
||
/// All values are assumed to be required unless explicitly stated otherwise | ||
/// using this wrapper. | ||
struct Optional { | ||
Ref<Type> type_; | ||
}; | ||
|
||
/// The is necessary to resolve circular definitions. Refers to something in | ||
/// Definitions. | ||
struct Reference { | ||
std::string name_; | ||
}; | ||
|
||
struct TypedArray { | ||
Ref<Type> type_; | ||
}; | ||
|
||
struct Variant { | ||
std::vector<Type> types_; | ||
}; | ||
|
||
/// A type can be determined to be any of the above. | ||
std::variant<Int32, Int64, UInt32, UInt64, Integer, Float, Double, String, | ||
Array, Object, Reference> | ||
variant_; | ||
}; | ||
|
||
} // namespace rfl::internal::schema | ||
|
||
#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,69 @@ | ||
#ifndef RFL_INTERNAL_SCHEMA_MAKE_HPP_ | ||
#define RFL_INTERNAL_SCHEMA_MAKE_HPP_ | ||
|
||
#include <bits/utility.h> | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <map> | ||
#include <string> | ||
#include <type_traits> | ||
|
||
#include "../../parsing/is_required.hpp" | ||
#include "../is_named_tuple.hpp" | ||
#include "Type.hpp" | ||
|
||
namespace rfl::internal::schema { | ||
|
||
template <class T> | ||
Type make_type(std::map<std::string, Type>* _definitions); | ||
|
||
template <class T, size_t _i = 0> | ||
Type make_named_tuple_type(std::map<std::string, Type>* _definitions, | ||
std::map<std::string, Type> _values = {}) { | ||
constexpr size_t size = T::size(); | ||
if constexpr (_i == size) { | ||
return Type{Object{_values}}; | ||
} else { | ||
using F = std::tuple_element_t<_i, typename T::Fields>; | ||
_values[F::name()] = make_type<typename F::Type>(_definitions); | ||
return make_named_tuple_type<T, _i + 1>(_definitions, _values); | ||
} | ||
}; | ||
|
||
template <class T> | ||
Type make_type(std::map<std::string, Type>* _definitions) { | ||
using U = std::remove_cvref_t<T>; | ||
if constexpr (std::is_same<U, std::int32_t>()) { | ||
return Definition{.root_ = Type{Int32{}}}; | ||
} else if constexpr (std::is_same<U, std::int64_t>()) { | ||
return Definition{.root_ = Type{Int64{}}}; | ||
} else if constexpr (std::is_same<U, std::uint32_t>()) { | ||
return Definition{.root_ = Type{UInt32{}}}; | ||
} else if constexpr (std::is_same<U, std::uint64_t>()) { | ||
return Definition{.root_ = Type{UInt64{}}}; | ||
} else if constexpr (std::is_integral<U>()) { | ||
return Definition{.root_ = Type{Integer{}}}; | ||
} else if constexpr (std::is_same<U, float>()) { | ||
return Definition{.root_ = Type{Float{}}}; | ||
} else if constexpr (std::is_same<U, double>()) { | ||
return Definition{.root_ = Type{Double{}}}; | ||
} else if constexpr (std::is_same<U, std::string>()) { | ||
return Definition{.root_ = Type{String{}}}; | ||
} else if constexpr (is_named_tuple_v<U>) { | ||
return make_named_tuple_definition<U>(); | ||
} else if constexpr (!is_required<T, false>()) { | ||
// TODO | ||
} | ||
} | ||
|
||
template <class T> | ||
Definition make() { | ||
std::map<std::string, Type> definitions; | ||
auto root = make_type<T>(&definitions); | ||
return Definition{.root_ = root, .definitions_ = definitions}; | ||
} | ||
|
||
} // namespace rfl::internal::schema | ||
|
||
#endif |