Skip to content

Commit c65d531

Browse files
committed
Fix #149
1 parent 1426562 commit c65d531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+257
-245
lines changed

include/Validation.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ struct ValidateArgument
2424
ValidateType type;
2525
};
2626

27-
using ValidateTypeFieldArguments = internal::sorted_map<std::string_view, ValidateArgument>;
27+
using ValidateTypeFieldArguments = internal::string_view_map<ValidateArgument>;
2828

2929
struct ValidateTypeField
3030
{
3131
ValidateType returnType;
3232
ValidateTypeFieldArguments arguments;
3333
};
3434

35-
using ValidateDirectiveArguments = internal::sorted_map<std::string_view, ValidateArgument>;
35+
using ValidateDirectiveArguments = internal::string_view_map<ValidateArgument>;
3636

3737
struct ValidateDirective
3838
{
@@ -75,7 +75,7 @@ struct ValidateArgumentMap
7575
{
7676
bool operator==(const ValidateArgumentMap& other) const;
7777

78-
internal::sorted_map<std::string_view, ValidateArgumentValuePtr> values;
78+
internal::string_view_map<ValidateArgumentValuePtr> values;
7979
};
8080

8181
using ValidateArgumentVariant = std::variant<ValidateArgumentVariable, response::IntType,
@@ -122,7 +122,7 @@ class ValidateArgumentValueVisitor
122122
std::list<schema_error>& _errors;
123123
};
124124

125-
using ValidateFieldArguments = internal::sorted_map<std::string_view, ValidateArgumentValuePtr>;
125+
using ValidateFieldArguments = internal::string_view_map<ValidateArgumentValuePtr>;
126126

127127
struct ValidateField
128128
{
@@ -137,7 +137,7 @@ struct ValidateField
137137
ValidateFieldArguments arguments;
138138
};
139139

140-
using ValidateTypes = internal::sorted_map<std::string_view, ValidateType>;
140+
using ValidateTypes = internal::string_view_map<ValidateType>;
141141

142142
// ValidateVariableTypeVisitor visits the AST and builds a ValidateType structure representing
143143
// a variable type in an operation definition as if it came from an Introspection query.
@@ -179,11 +179,11 @@ class ValidateExecutableVisitor
179179
static ValidateTypeFieldArguments getArguments(
180180
const std::vector<std::shared_ptr<const schema::InputValue>>& args);
181181

182-
using FieldTypes = internal::sorted_map<std::string_view, ValidateTypeField>;
183-
using TypeFields = internal::sorted_map<std::string_view, FieldTypes>;
182+
using FieldTypes = internal::string_view_map<ValidateTypeField>;
183+
using TypeFields = internal::string_view_map<FieldTypes>;
184184
using InputFieldTypes = ValidateTypeFieldArguments;
185-
using InputTypeFields = internal::sorted_map<std::string_view, InputFieldTypes>;
186-
using EnumValues = internal::sorted_map<std::string_view, internal::sorted_set<std::string_view>>;
185+
using InputTypeFields = internal::string_view_map<InputFieldTypes>;
186+
using EnumValues = internal::string_view_map<internal::string_view_set>;
187187

188188
constexpr bool isScalarType(introspection::TypeKind kind);
189189

@@ -218,15 +218,16 @@ class ValidateExecutableVisitor
218218
const std::shared_ptr<schema::Schema> _schema;
219219
std::list<schema_error> _errors;
220220

221-
using Directives = internal::sorted_map<std::string_view, ValidateDirective>;
222-
using ExecutableNodes = internal::sorted_map<std::string_view, std::reference_wrapper<const peg::ast_node>>;
223-
using FragmentSet = internal::sorted_set<std::string_view>;
224-
using MatchingTypes = internal::sorted_map<std::string_view, internal::sorted_set<std::string_view>>;
225-
using ScalarTypes = internal::sorted_set<std::string_view>;
226-
using VariableDefinitions = internal::sorted_map<std::string_view, std::reference_wrapper<const peg::ast_node>>;
227-
using VariableTypes = internal::sorted_map<std::string_view, ValidateArgument>;
221+
using Directives = internal::string_view_map<ValidateDirective>;
222+
using AstNodeRef = std::reference_wrapper<const peg::ast_node>;
223+
using ExecutableNodes = internal::string_view_map<AstNodeRef>;
224+
using FragmentSet = internal::string_view_set;
225+
using MatchingTypes = internal::string_view_map<internal::string_view_set>;
226+
using ScalarTypes = internal::string_view_set;
227+
using VariableDefinitions = internal::string_view_map<AstNodeRef>;
228+
using VariableTypes = internal::string_view_map<ValidateArgument>;
228229
using OperationVariables = std::optional<VariableTypes>;
229-
using VariableSet = internal::sorted_set<std::string_view>;
230+
using VariableSet = internal::string_view_set;
230231

231232
// These members store Introspection schema information which does not change between queries.
232233
ValidateTypes _operationTypes;
@@ -252,7 +253,7 @@ class ValidateExecutableVisitor
252253
TypeFields _typeFields;
253254
InputTypeFields _inputTypeFields;
254255
ValidateType _scopedType;
255-
internal::sorted_map<std::string_view, ValidateField> _selectionFields;
256+
internal::string_view_map<ValidateField> _selectionFields;
256257
};
257258

258259
} /* namespace graphql::service */

include/graphqlservice/GraphQLSchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Schema : public std::enable_shared_from_this<Schema>
6767
std::shared_ptr<const ObjectType> _query;
6868
std::shared_ptr<const ObjectType> _mutation;
6969
std::shared_ptr<const ObjectType> _subscription;
70-
internal::sorted_map<std::string_view, size_t> _typeMap;
70+
internal::string_view_map<size_t> _typeMap;
7171
std::vector<std::pair<std::string_view, std::shared_ptr<const BaseType>>> _types;
7272
std::vector<std::shared_ptr<const Directive>> _directives;
7373
internal::sorted_map<std::shared_ptr<const BaseType>, std::shared_ptr<const BaseType>>

include/graphqlservice/GraphQLService.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class Fragment
243243

244244
// Resolvers for complex types need to be able to find fragment definitions anywhere in
245245
// the request document by name.
246-
using FragmentMap = internal::sorted_map<std::string_view, Fragment>;
246+
using FragmentMap = internal::string_view_map<Fragment>;
247247

248248
// Resolver functors take a set of arguments encoded as members on a JSON object
249249
// with an optional selection set for complex types and return a JSON value for
@@ -279,7 +279,7 @@ struct ResolverResult
279279
};
280280

281281
using Resolver = std::function<std::future<ResolverResult>(ResolverParams&&)>;
282-
using ResolverMap = internal::sorted_map<std::string_view, Resolver>;
282+
using ResolverMap = internal::string_view_map<Resolver>;
283283

284284
// Binary data and opaque strings like IDs are encoded in Base64.
285285
class Base64
@@ -498,7 +498,7 @@ GRAPHQLSERVICE_EXPORT response::Value ModifiedArgument<response::Value>::convert
498498

499499
// Each type should handle fragments with type conditions matching its own
500500
// name and any inheritted interfaces.
501-
using TypeNames = internal::sorted_set<std::string_view>;
501+
using TypeNames = internal::string_view_set;
502502

503503
// Object parses argument values, performs variable lookups, expands fragments, evaluates @include
504504
// and @skip directives, and calls through to the resolver functor for each selected field with
@@ -851,7 +851,7 @@ GRAPHQLSERVICE_EXPORT std::future<ResolverResult> ModifiedResult<Object>::conver
851851
FieldResult<std::shared_ptr<Object>>&& result, ResolverParams&& params);
852852
#endif // GRAPHQL_DLLEXPORTS
853853

854-
using TypeMap = internal::sorted_map<std::string_view, std::shared_ptr<Object>>;
854+
using TypeMap = internal::string_view_map<std::shared_ptr<Object>>;
855855

856856
// You can still sub-class RequestState and use that in the state parameter to Request::subscribe
857857
// to add your own state to the service callbacks that you receive while executing the subscription
@@ -1002,7 +1002,7 @@ class Request : public std::enable_shared_from_this<Request>
10021002
const TypeMap _operations;
10031003
std::unique_ptr<ValidateExecutableVisitor> _validation;
10041004
internal::sorted_map<SubscriptionKey, std::shared_ptr<SubscriptionData>> _subscriptions;
1005-
internal::sorted_map<std::string_view, internal::sorted_set<SubscriptionKey>> _listeners;
1005+
internal::string_view_map<internal::sorted_set<SubscriptionKey>> _listeners;
10061006
SubscriptionKey _nextKey = 0;
10071007
};
10081008

include/graphqlservice/internal/SortedMap.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
#define SORTEDMAP_H
88

99
#include <algorithm>
10+
#include <functional>
1011
#include <initializer_list>
1112
#include <stdexcept>
1213
#include <tuple>
1314
#include <vector>
1415

1516
namespace graphql::internal {
1617

17-
template <class K, class V>
18+
template <class K, class V, class Compare = std::less<K>>
1819
class sorted_map
1920
{
2021
public:
@@ -29,7 +30,7 @@ class sorted_map
2930
: _data { init }
3031
{
3132
std::sort(_data.begin(), _data.end(), [](const auto& lhs, const auto& rhs) noexcept {
32-
return lhs.first < rhs.first;
33+
return Compare {}(lhs.first, rhs.first);
3334
});
3435
}
3536

@@ -89,7 +90,7 @@ class sorted_map
8990
end(),
9091
key,
9192
[](sorted_map_key lhs, sorted_map_key rhs) noexcept {
92-
return lhs.key < rhs.key;
93+
return Compare {}(lhs.key, rhs.key);
9394
});
9495

9596
return itr == itrEnd ? _data.end() : itr;
@@ -111,17 +112,15 @@ class sorted_map
111112
_data.end(),
112113
key,
113114
[](sorted_map_key lhs, sorted_map_key rhs) noexcept {
114-
return lhs.key < rhs.key;
115+
return Compare {}(lhs.key, rhs.key);
115116
});
116117

117118
if (itr != itrEnd)
118119
{
119120
return { itr, false };
120121
}
121122

122-
return { _data.emplace(itrEnd,
123-
std::move(key),
124-
V { std::forward<ValueArgs>(args)... }),
123+
return { _data.emplace(itrEnd, std::move(key), V { std::forward<ValueArgs>(args)... }),
125124
true };
126125
}
127126

@@ -131,7 +130,7 @@ class sorted_map
131130
_data.end(),
132131
key,
133132
[](sorted_map_key lhs, sorted_map_key rhs) noexcept {
134-
return lhs.key < rhs.key;
133+
return Compare {}(lhs.key, rhs.key);
135134
});
136135

137136
if (itr == itrEnd)
@@ -163,7 +162,7 @@ class sorted_map
163162
_data.end(),
164163
key,
165164
[](sorted_map_key lhs, sorted_map_key rhs) noexcept {
166-
return lhs.key < rhs.key;
165+
return Compare {}(lhs.key, rhs.key);
167166
});
168167

169168
if (itr != itrEnd)
@@ -182,7 +181,7 @@ class sorted_map
182181
_data.end(),
183182
key,
184183
[](sorted_map_key lhs, sorted_map_key rhs) noexcept {
185-
return lhs.key < rhs.key;
184+
return Compare {}(lhs.key, rhs.key);
186185
});
187186

188187
if (itr == itrEnd)
@@ -212,7 +211,7 @@ class sorted_map
212211
vector_type _data;
213212
};
214213

215-
template <class K>
214+
template <class K, class Compare = std::less<K>>
216215
class sorted_set
217216
{
218217
public:
@@ -225,8 +224,8 @@ class sorted_set
225224
sorted_set(std::initializer_list<K> init)
226225
: _data { init }
227226
{
228-
std::sort(_data.begin(), _data.end(), [](const auto& lhs, const auto& rhs) noexcept {
229-
return lhs < rhs;
227+
std::sort(_data.begin(), _data.end(), [](const K& lhs, const K& rhs) noexcept {
228+
return Compare {}(lhs, rhs);
230229
});
231230
}
232231

@@ -283,8 +282,8 @@ class sorted_set
283282
const_iterator find(const K& key) const noexcept
284283
{
285284
const auto [itr, itrEnd] =
286-
std::equal_range(begin(), end(), key, [](const auto& lhs, const auto& rhs) noexcept {
287-
return lhs < rhs;
285+
std::equal_range(begin(), end(), key, [](const K& lhs, const K& rhs) noexcept {
286+
return Compare {}(lhs, rhs);
288287
});
289288

290289
return itr == itrEnd ? _data.end() : itr;
@@ -305,7 +304,7 @@ class sorted_set
305304
_data.end(),
306305
key,
307306
[](const auto& lhs, const auto& rhs) noexcept {
308-
return lhs < rhs;
307+
return Compare {}(lhs, rhs);
309308
});
310309

311310
if (itr != itrEnd)
@@ -321,8 +320,8 @@ class sorted_set
321320
const auto [itr, itrEnd] = std::equal_range(_data.begin(),
322321
_data.end(),
323322
key,
324-
[](const auto& lhs, const auto& rhs) noexcept {
325-
return lhs < rhs;
323+
[](const K& lhs, const K& rhs) noexcept {
324+
return Compare {}(lhs, rhs);
326325
});
327326

328327
if (itr == itrEnd)
@@ -350,6 +349,18 @@ class sorted_set
350349
vector_type _data;
351350
};
352351

352+
struct shorter_or_less
353+
{
354+
constexpr bool operator()(const std::string_view& lhs, const std::string_view& rhs) const
355+
{
356+
return lhs.size() == rhs.size() ? lhs < rhs : lhs.size() < rhs.size();
357+
}
358+
};
359+
360+
template <class V>
361+
using string_view_map = sorted_map<std::string_view, V, shorter_or_less>;
362+
using string_view_set = sorted_set<std::string_view, shorter_or_less>;
363+
353364
} // namespace graphql::internal
354365

355366
#endif // SORTEDMAP_H

samples/separate/AppointmentConnectionObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ AppointmentConnection::AppointmentConnection()
2222
: service::Object({
2323
"AppointmentConnection"
2424
}, {
25-
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
2625
{ R"gql(edges)gql"sv, [this](service::ResolverParams&& params) { return resolveEdges(std::move(params)); } },
27-
{ R"gql(pageInfo)gql"sv, [this](service::ResolverParams&& params) { return resolvePageInfo(std::move(params)); } }
26+
{ R"gql(pageInfo)gql"sv, [this](service::ResolverParams&& params) { return resolvePageInfo(std::move(params)); } },
27+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } }
2828
})
2929
{
3030
}

samples/separate/AppointmentEdgeObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ AppointmentEdge::AppointmentEdge()
2222
: service::Object({
2323
"AppointmentEdge"
2424
}, {
25-
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
25+
{ R"gql(node)gql"sv, [this](service::ResolverParams&& params) { return resolveNode(std::move(params)); } },
2626
{ R"gql(cursor)gql"sv, [this](service::ResolverParams&& params) { return resolveCursor(std::move(params)); } },
27-
{ R"gql(node)gql"sv, [this](service::ResolverParams&& params) { return resolveNode(std::move(params)); } }
27+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } }
2828
})
2929
{
3030
}

samples/separate/AppointmentObject.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ Appointment::Appointment()
2424
"UnionType",
2525
"Appointment"
2626
}, {
27-
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
28-
{ R"gql(forceError)gql"sv, [this](service::ResolverParams&& params) { return resolveForceError(std::move(params)); } },
2927
{ R"gql(id)gql"sv, [this](service::ResolverParams&& params) { return resolveId(std::move(params)); } },
28+
{ R"gql(when)gql"sv, [this](service::ResolverParams&& params) { return resolveWhen(std::move(params)); } },
3029
{ R"gql(isNow)gql"sv, [this](service::ResolverParams&& params) { return resolveIsNow(std::move(params)); } },
3130
{ R"gql(subject)gql"sv, [this](service::ResolverParams&& params) { return resolveSubject(std::move(params)); } },
32-
{ R"gql(when)gql"sv, [this](service::ResolverParams&& params) { return resolveWhen(std::move(params)); } }
31+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
32+
{ R"gql(forceError)gql"sv, [this](service::ResolverParams&& params) { return resolveForceError(std::move(params)); } }
3333
})
3434
{
3535
}

samples/separate/CompleteTaskPayloadObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ CompleteTaskPayload::CompleteTaskPayload()
2222
: service::Object({
2323
"CompleteTaskPayload"
2424
}, {
25+
{ R"gql(task)gql"sv, [this](service::ResolverParams&& params) { return resolveTask(std::move(params)); } },
2526
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
26-
{ R"gql(clientMutationId)gql"sv, [this](service::ResolverParams&& params) { return resolveClientMutationId(std::move(params)); } },
27-
{ R"gql(task)gql"sv, [this](service::ResolverParams&& params) { return resolveTask(std::move(params)); } }
27+
{ R"gql(clientMutationId)gql"sv, [this](service::ResolverParams&& params) { return resolveClientMutationId(std::move(params)); } }
2828
})
2929
{
3030
}

samples/separate/ExpensiveObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Expensive::Expensive()
2222
: service::Object({
2323
"Expensive"
2424
}, {
25-
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
26-
{ R"gql(order)gql"sv, [this](service::ResolverParams&& params) { return resolveOrder(std::move(params)); } }
25+
{ R"gql(order)gql"sv, [this](service::ResolverParams&& params) { return resolveOrder(std::move(params)); } },
26+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } }
2727
})
2828
{
2929
}

samples/separate/FolderConnectionObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ FolderConnection::FolderConnection()
2222
: service::Object({
2323
"FolderConnection"
2424
}, {
25-
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } },
2625
{ R"gql(edges)gql"sv, [this](service::ResolverParams&& params) { return resolveEdges(std::move(params)); } },
27-
{ R"gql(pageInfo)gql"sv, [this](service::ResolverParams&& params) { return resolvePageInfo(std::move(params)); } }
26+
{ R"gql(pageInfo)gql"sv, [this](service::ResolverParams&& params) { return resolvePageInfo(std::move(params)); } },
27+
{ R"gql(__typename)gql"sv, [this](service::ResolverParams&& params) { return resolve_typename(std::move(params)); } }
2828
})
2929
{
3030
}

0 commit comments

Comments
 (0)