Skip to content

Commit

Permalink
Began developing the JSON schema definition; #25
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Feb 4, 2024
1 parent 5b7dab4 commit 4698ab9
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/rfl/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ struct Field {
/// Returns the underlying object.
const Type& get() const { return value_; }

/// Returns the name of the fiels as a string.
static std::string name() const { return name_::str(); }

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

static member function ‘static std::string rfl::Field<_name, T>::name()’ cannot have cv-qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

‘name_’ is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

static member function ‘static std::string rfl::Field<_name, T>::name()’ cannot have cv-qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

‘name_’ is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

static member function ‘static std::string rfl::Field<_name, T>::name()’ cannot have cv-qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

‘name_’ is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

static member function ‘static std::string rfl::Field<_name, T>::name()’ cannot have cv-qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

‘name_’ is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

static member function ‘static std::string rfl::Field<_name, T>::name()’ cannot have cv-qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

‘name_’ is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

static member function cannot have 'const' qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

'name_' is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

static member function cannot have 'const' qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

static member function cannot have 'const' qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

'name_' is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

'name_' is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

static member function cannot have 'const' qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

'name_' is not a class, namespace, or enumeration

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

static member function cannot have 'const' qualifier

Check failure on line 69 in include/rfl/Field.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

'name_' is not a class, namespace, or enumeration

/// Returns the underlying object.
Type& operator()() { return value_; }

Expand Down
21 changes: 21 additions & 0 deletions include/rfl/internal/schema/Definition.hpp
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
62 changes: 62 additions & 0 deletions include/rfl/internal/schema/Type.hpp
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
69 changes: 69 additions & 0 deletions include/rfl/internal/schema/make.hpp
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

0 comments on commit 4698ab9

Please sign in to comment.