Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
sabudilovskiy committed Sep 9, 2023
1 parent 0c959ef commit 2a5e20f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .gen/unittest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ utests/openapi/json/serialize/basic_array.cpp
utests/openapi/json/parse/basic_string.cpp
utests/openapi/json/parse/basic_object.cpp
utests/openapi/json/parse/basic_array.cpp
utests/openapi/doc/serialize/basic_array.cpp
utests/openapi/doc/serialize/basic.cpp
utests/hello_test.cpp
utests/dropper_additional.cpp
utests/dropper.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/openapi/base/object_property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ template <typename T>
concept IsReflective = requires
{
requires std::is_class_v<T>;
requires std::is_base_of_v<Reflective, T>;
typename T::Reflective;
requires std::is_same_v<typename T::Reflective, Yes>;
};
} // namespace checks

Expand Down
5 changes: 4 additions & 1 deletion src/openapi/base/property_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ struct EmptyTraits
{
};

struct Reflective
struct Yes
{
};

#define REFLECTIVE_BASE(type) \
using Reflective = timetable_vsu_backend::openapi::Yes

template <typename T, typename U>
concept HasSpaceShip = requires(T t, U u)
{
Expand Down
22 changes: 20 additions & 2 deletions src/openapi/doc/serialize/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ std::string GetOpenApiTypeName()
return raw_name;
}

template <typename T>
void PlaceRefToType(userver::formats::yaml::ValueBuilder& place)
{
if (!place.IsObject())
{
place = userver::formats::common::Type::kObject;
}
auto ref_str =
std::string(R"(#/components/schemas/)").append(GetOpenApiTypeName<T>());
std::clog << "log this moment: 104\n";
place["$ref"] = std::move(ref_str);
}

template <typename T, typename... Other>
consteval bool IsAnyOf()
{
Expand Down Expand Up @@ -201,6 +214,12 @@ void AppendField(DocHelper doc_helper, std::type_identity<T> type = {})
Append(DocHelper{doc_helper.root, field_node}, std::type_identity<T>{});
}

// noop, пропускаем
template <>
inline void AppendField(DocHelper, std::type_identity<AdditionalProperties>)
{
}

template <typename... Field>
void AppendFields(DocHelper doc_helper,
std::type_identity<std::tuple<Field...>>)
Expand Down Expand Up @@ -237,8 +256,7 @@ void Append(DocHelper doc_helper, std::type_identity<T>,
std::string name_type = GetOpenApiTypeName<T>();
if (append_cur_place)
{
doc_helper.cur_place =
std::string("$ref/components/schemas/").append(name_type);
PlaceRefToType<T>(doc_helper.cur_place);
}
auto type_node = doc_helper.root["components"]["schemas"][name_type];
if (!type_node.IsObject())
Expand Down
5 changes: 4 additions & 1 deletion src/openapi/json/parse/reflective.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#pragma once

#include <fmt/core.h>

#include <array>
#include <boost/pfr.hpp>
#include <boost/pfr/core.hpp>
#include <iostream>
#include <stdexcept>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -105,7 +108,7 @@ T Parse(const json::Value& item, To<T>)
};
boost::pfr::for_each_field(result, matcher_common_type);
}
return {std::move(result)};
return result;
}

} // namespace userver::formats::parse
3 changes: 1 addition & 2 deletions src/openapi/json/serialize/reflective.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ inline void serialize_without_additional(
auto matcher_common = [&result]<typename F>(const F& field) {
constexpr auto name = traits::GetName<typename F::traits>();
static_assert(!name.empty(), "Common field must have name");
auto name_str = std::string{name.AsStringView()};
result[name_str] = field();
result[name.AsString()] = field();
};
// noop
auto matcher_additional_properties = [](const AdditionalProperties&) {};
Expand Down
5 changes: 5 additions & 0 deletions src/utils/constexpr_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ struct ConstexprString
return {contents.begin(), contents.begin() + Size - 1};
}

std::string AsString() const
{
return std::string{AsStringView()};
}

constexpr const char* data() const
{
return contents.begin();
Expand Down
99 changes: 0 additions & 99 deletions utests/openapi/doc/serialize/basic_array.cpp

This file was deleted.

20 changes: 11 additions & 9 deletions utests/openapi/json/parse/basic_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ using namespace timetable_vsu_backend::openapi;
using namespace timetable_vsu_backend::openapi::types;
using namespace timetable_vsu_backend::openapi::preferences;

struct First : Reflective
struct First
{
REFLECTIVE_BASE(First);
String<Name<"field">> field;
Array<int, Name<"field2">> field2;
};
Expand All @@ -52,8 +53,9 @@ UTEST(Openapi_Json_Parse, BasicObject)
EXPECT_EQ(got_object().field2(), expected_object().field2());
}

struct Second : Reflective
struct Second
{
REFLECTIVE_BASE(Second);
String<Name<"field">> field;
AdditionalProperties other;
};
Expand All @@ -69,14 +71,14 @@ UTEST(Openapi_Json_Parse, BasicObjectAdditional)
)";
auto json = userver::formats::json::FromString(jsonString);
auto got_object = json.As<Type>();
auto expected_object = Type();
expected_object().field() = "test";
EXPECT_TRUE(expected_object().other().IsArray());
EXPECT_EQ(expected_object().other().GetSize(), 3);
auto first_elem = expected_object().other()[0].As<int>();
EXPECT_TRUE(got_object().other().IsObject());
auto field2 = got_object().other()["field2"];
EXPECT_TRUE(field2.IsArray());
EXPECT_EQ(field2.GetSize(), 3);
auto first_elem = field2[0].As<int>();
EXPECT_EQ(first_elem, 1);
auto second_elem = expected_object().other()[1].As<int>();
auto second_elem = field2[1].As<int>();
EXPECT_EQ(second_elem, 3);
auto thirst_elem = expected_object().other()[2].As<int>();
auto thirst_elem = field2[2].As<int>();
EXPECT_EQ(thirst_elem, 5);
}
6 changes: 4 additions & 2 deletions utests/openapi/json/serialize/basic_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ using namespace timetable_vsu_backend::openapi;
using namespace timetable_vsu_backend::openapi::types;
using namespace timetable_vsu_backend::openapi::preferences;

struct First : Reflective
struct First
{
REFLECTIVE_BASE(First);
String<Name<"field">> field;
Array<int, Name<"field2">> field2;
};
Expand All @@ -49,8 +50,9 @@ UTEST(Openapi_Json_Serialize, BasicObject)
EXPECT_EQ(text_json, R"({"field":"test_string","field2":[1,2,3]})");
}

struct Second : Reflective
struct Second
{
REFLECTIVE_BASE(Second);
String<Name<"field">> field;
AdditionalProperties other_fields;
};
Expand Down

0 comments on commit 2a5e20f

Please sign in to comment.