Skip to content

Commit b07860d

Browse files
committed
Port sorted_map_lookup for enums to clientgen
1 parent 160f40a commit b07860d

File tree

7 files changed

+116
-43
lines changed

7 files changed

+116
-43
lines changed

samples/client/benchmark/BenchmarkClient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "BenchmarkClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>

samples/client/multiple/MultipleQueriesClient.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "MultipleQueriesClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>
@@ -120,10 +122,17 @@ const peg::ast& GetRequestObject() noexcept
120122
}
121123

122124
static const std::array<std::string_view, 4> s_namesTaskState = {
123-
"New"sv,
124-
"Started"sv,
125-
"Complete"sv,
126-
"Unassigned"sv,
125+
R"gql(New)gql"sv,
126+
R"gql(Started)gql"sv,
127+
R"gql(Complete)gql"sv,
128+
R"gql(Unassigned)gql"sv
129+
};
130+
131+
static const std::array<std::pair<std::string_view, TaskState>, 4> s_valuesTaskState = {
132+
std::make_pair(R"gql(New)gql"sv, TaskState::New),
133+
std::make_pair(R"gql(Started)gql"sv, TaskState::Started),
134+
std::make_pair(R"gql(Complete)gql"sv, TaskState::Complete),
135+
std::make_pair(R"gql(Unassigned)gql"sv, TaskState::Unassigned)
127136
};
128137

129138
CompleteTaskInput::CompleteTaskInput(
@@ -526,17 +535,19 @@ TaskState ModifiedResponse<TaskState>::parse(response::Value&& value)
526535
{
527536
if (!value.maybe_enum())
528537
{
529-
throw std::logic_error { "not a valid TaskState value" };
538+
throw std::logic_error { R"ex(not a valid TaskState value)ex" };
530539
}
531540

532-
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.release<std::string>());
541+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
542+
s_valuesTaskState,
543+
std::string_view { value.get<std::string>() });
533544

534-
if (itr == s_namesTaskState.cend())
545+
if (!result)
535546
{
536-
throw std::logic_error { "not a valid TaskState value" };
547+
throw std::logic_error { { R"ex(not a valid TaskState value)ex" } };
537548
}
538549

539-
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
550+
return *result;
540551
}
541552

542553
template <>

samples/client/mutate/MutateClient.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "MutateClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>
@@ -53,10 +55,17 @@ const peg::ast& GetRequestObject() noexcept
5355
}
5456

5557
static const std::array<std::string_view, 4> s_namesTaskState = {
56-
"New"sv,
57-
"Started"sv,
58-
"Complete"sv,
59-
"Unassigned"sv,
58+
R"gql(New)gql"sv,
59+
R"gql(Started)gql"sv,
60+
R"gql(Complete)gql"sv,
61+
R"gql(Unassigned)gql"sv
62+
};
63+
64+
static const std::array<std::pair<std::string_view, TaskState>, 4> s_valuesTaskState = {
65+
std::make_pair(R"gql(New)gql"sv, TaskState::New),
66+
std::make_pair(R"gql(Started)gql"sv, TaskState::Started),
67+
std::make_pair(R"gql(Complete)gql"sv, TaskState::Complete),
68+
std::make_pair(R"gql(Unassigned)gql"sv, TaskState::Unassigned)
6069
};
6170

6271
CompleteTaskInput::CompleteTaskInput(
@@ -138,17 +147,19 @@ TaskState ModifiedResponse<TaskState>::parse(response::Value&& value)
138147
{
139148
if (!value.maybe_enum())
140149
{
141-
throw std::logic_error { "not a valid TaskState value" };
150+
throw std::logic_error { R"ex(not a valid TaskState value)ex" };
142151
}
143152

144-
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.release<std::string>());
153+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
154+
s_valuesTaskState,
155+
std::string_view { value.get<std::string>() });
145156

146-
if (itr == s_namesTaskState.cend())
157+
if (!result)
147158
{
148-
throw std::logic_error { "not a valid TaskState value" };
159+
throw std::logic_error { { R"ex(not a valid TaskState value)ex" } };
149160
}
150161

151-
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
162+
return *result;
152163
}
153164

154165
template <>

samples/client/nestedinput/NestedInputClient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "NestedInputClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>

samples/client/query/QueryClient.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "QueryClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>
@@ -101,10 +103,17 @@ const peg::ast& GetRequestObject() noexcept
101103
}
102104

103105
static const std::array<std::string_view, 4> s_namesTaskState = {
104-
"New"sv,
105-
"Started"sv,
106-
"Complete"sv,
107-
"Unassigned"sv,
106+
R"gql(New)gql"sv,
107+
R"gql(Started)gql"sv,
108+
R"gql(Complete)gql"sv,
109+
R"gql(Unassigned)gql"sv
110+
};
111+
112+
static const std::array<std::pair<std::string_view, TaskState>, 4> s_valuesTaskState = {
113+
std::make_pair(R"gql(New)gql"sv, TaskState::New),
114+
std::make_pair(R"gql(Started)gql"sv, TaskState::Started),
115+
std::make_pair(R"gql(Complete)gql"sv, TaskState::Complete),
116+
std::make_pair(R"gql(Unassigned)gql"sv, TaskState::Unassigned)
108117
};
109118

110119
} // namespace query
@@ -116,17 +125,19 @@ TaskState ModifiedResponse<TaskState>::parse(response::Value&& value)
116125
{
117126
if (!value.maybe_enum())
118127
{
119-
throw std::logic_error { "not a valid TaskState value" };
128+
throw std::logic_error { R"ex(not a valid TaskState value)ex" };
120129
}
121130

122-
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.release<std::string>());
131+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
132+
s_valuesTaskState,
133+
std::string_view { value.get<std::string>() });
123134

124-
if (itr == s_namesTaskState.cend())
135+
if (!result)
125136
{
126-
throw std::logic_error { "not a valid TaskState value" };
137+
throw std::logic_error { { R"ex(not a valid TaskState value)ex" } };
127138
}
128139

129-
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
140+
return *result;
130141
}
131142

132143
template <>

samples/client/subscribe/SubscribeClient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "SubscribeClient.h"
77

8+
#include "graphqlservice/internal/SortedMap.h"
9+
810
#include <algorithm>
911
#include <array>
1012
#include <sstream>

src/ClientGenerator.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ bool Generator::outputSource() const noexcept
581581
#include ")cpp" << _schemaLoader.getFilenamePrefix()
582582
<< R"cpp(Client.h"
583583
584+
#include "graphqlservice/internal/SortedMap.h"
585+
584586
#include <algorithm>
585587
#include <array>
586588
#include <sstream>
@@ -618,16 +620,49 @@ using namespace std::literals;
618620
const auto& enumValues = enumType->enumValues();
619621

620622
sourceFile << R"cpp(static const std::array<std::string_view, )cpp" << enumValues.size()
621-
<< R"cpp(> s_names)cpp" << cppType << R"cpp( = {
622-
)cpp";
623+
<< R"cpp(> s_names)cpp" << cppType << R"cpp( = {)cpp";
624+
625+
bool firstValue = true;
623626

624627
for (const auto& enumValue : enumValues)
625628
{
626-
sourceFile << R"cpp( ")cpp" << SchemaLoader::getSafeCppName(enumValue->name())
627-
<< R"cpp("sv,
628-
)cpp";
629+
if (!firstValue)
630+
{
631+
sourceFile << R"cpp(,)cpp";
632+
}
633+
634+
firstValue = false;
635+
sourceFile << R"cpp(
636+
R"gql()cpp" << enumValue->name()
637+
<< R"cpp()gql"sv)cpp";
638+
pendingSeparator.add();
639+
}
640+
641+
pendingSeparator.reset();
642+
sourceFile << R"cpp(};
643+
644+
static const std::array<std::pair<std::string_view, )cpp"
645+
<< cppType << R"cpp(>, )cpp" << enumValues.size() << R"cpp(> s_values)cpp"
646+
<< cppType << R"cpp( = {)cpp";
647+
648+
firstValue = true;
649+
650+
for (const auto& enumValue : enumValues)
651+
{
652+
if (!firstValue)
653+
{
654+
sourceFile << R"cpp(,)cpp";
655+
}
656+
657+
firstValue = false;
658+
sourceFile << R"cpp(
659+
std::make_pair(R"gql()cpp"
660+
<< enumValue->name() << R"cpp()gql"sv, )cpp" << cppType << R"cpp(::)cpp"
661+
<< SchemaLoader::getSafeCppName(enumValue->name()) << R"cpp())cpp";
662+
pendingSeparator.add();
629663
}
630664

665+
pendingSeparator.reset();
631666
sourceFile << R"cpp(};
632667
)cpp";
633668

@@ -873,23 +908,22 @@ response::Value ModifiedVariable<)cpp"
873908
{
874909
if (!value.maybe_enum())
875910
{
876-
throw std::logic_error { "not a valid )cpp"
877-
<< cppType << R"cpp( value" };
911+
throw std::logic_error { R"ex(not a valid )cpp"
912+
<< enumType->name() << R"cpp( value)ex" };
878913
}
879914
880-
const auto itr = std::find(s_names)cpp"
881-
<< cppType << R"cpp(.cbegin(), s_names)cpp" << cppType
882-
<< R"cpp(.cend(), value.release<std::string>());
915+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
916+
s_values)cpp" << cppType
917+
<< R"cpp(,
918+
std::string_view { value.get<std::string>() });
883919
884-
if (itr == s_names)cpp"
885-
<< cppType << R"cpp(.cend())
920+
if (!result)
886921
{
887-
throw std::logic_error { "not a valid )cpp"
888-
<< cppType << R"cpp( value" };
922+
throw std::logic_error { { R"ex(not a valid )cpp"
923+
<< enumType->name() << R"cpp( value)ex" } };
889924
}
890925
891-
return static_cast<)cpp"
892-
<< cppType << R"cpp(>(itr - s_names)cpp" << cppType << R"cpp(.cbegin());
926+
return *result;
893927
}
894928
)cpp";
895929

0 commit comments

Comments
 (0)