7
7
#define SCHEMAGENERATOR_H
8
8
9
9
#include " graphqlservice/GraphQLGrammar.h"
10
+ #include " graphqlservice/GraphQLParse.h"
10
11
#include " graphqlservice/GraphQLService.h"
11
12
12
13
#include < array>
13
14
#include < cstdio>
15
+ #include < unordered_map>
16
+ #include < unordered_set>
14
17
15
18
namespace graphql ::schema {
16
19
@@ -24,10 +27,10 @@ enum class BuiltinType
24
27
ID,
25
28
};
26
29
27
- using BuiltinTypeMap = std::map<std::string , BuiltinType>;
30
+ using BuiltinTypeMap = std::map<std::string_view , BuiltinType>;
28
31
29
32
// These are the C++ types we'll use for them.
30
- using CppTypeMap = std::array<std::string , static_cast <size_t >(BuiltinType::ID) + 1 >;
33
+ using CppTypeMap = std::array<std::string_view , static_cast <size_t >(BuiltinType::ID) + 1 >;
31
34
32
35
// Types that we understand and use to generate the skeleton of a service.
33
36
enum class SchemaType
@@ -41,14 +44,14 @@ enum class SchemaType
41
44
Operation,
42
45
};
43
46
44
- using SchemaTypeMap = std::map<std::string , SchemaType>;
47
+ using SchemaTypeMap = std::map<std::string_view , SchemaType>;
45
48
46
49
// Keep track of the positions of each type declaration in the file.
47
- using PositionMap = std::unordered_map<std::string , tao::graphqlpeg::position>;
50
+ using PositionMap = std::unordered_map<std::string_view , tao::graphqlpeg::position>;
48
51
49
52
// For all of the named types we track, we want to keep them in order in a vector but
50
53
// be able to lookup their offset quickly by name.
51
- using TypeNameMap = std::unordered_map<std::string , size_t >;
54
+ using TypeNameMap = std::unordered_map<std::string_view , size_t >;
52
55
53
56
// Any type can also have a list and/or non-nullable wrapper, and those can be nested.
54
57
// Since it's easier to express nullability than non-nullability in C++, we'll invert
@@ -60,28 +63,28 @@ using TypeModifierStack = std::vector<service::TypeModifier>;
60
63
// scalar type names have been declared so we recognize the references.
61
64
struct ScalarType
62
65
{
63
- std::string type;
64
- std::string description;
66
+ std::string_view type;
67
+ std::string_view description;
65
68
};
66
69
67
70
using ScalarTypeList = std::vector<ScalarType>;
68
71
69
72
// Enum types map a type name to a collection of valid string values.
70
73
struct EnumValueType
71
74
{
72
- std::string value;
73
- std::string cppValue;
74
- std::string description;
75
- std::optional<std::string > deprecationReason;
75
+ std::string_view value;
76
+ std::string_view cppValue;
77
+ std::string_view description;
78
+ std::optional<std::string_view > deprecationReason;
76
79
std::optional<tao::graphqlpeg::position> position;
77
80
};
78
81
79
82
struct EnumType
80
83
{
81
- std::string type;
82
- std::string cppType;
84
+ std::string_view type;
85
+ std::string_view cppType;
83
86
std::vector<EnumValueType> values;
84
- std::string description;
87
+ std::string_view description;
85
88
};
86
89
87
90
using EnumTypeList = std::vector<EnumType>;
@@ -99,47 +102,47 @@ enum class InputFieldType
99
102
100
103
struct InputField
101
104
{
102
- std::string type;
103
- std::string name;
104
- std::string cppName;
105
- std::string defaultValueString;
105
+ std::string_view type;
106
+ std::string_view name;
107
+ std::string_view cppName;
108
+ std::string_view defaultValueString;
106
109
response::Value defaultValue;
107
110
InputFieldType fieldType = InputFieldType::Builtin;
108
111
TypeModifierStack modifiers;
109
- std::string description;
112
+ std::string_view description;
110
113
std::optional<tao::graphqlpeg::position> position;
111
114
};
112
115
113
116
using InputFieldList = std::vector<InputField>;
114
117
115
118
struct InputType
116
119
{
117
- std::string type;
118
- std::string cppType;
120
+ std::string_view type;
121
+ std::string_view cppType;
119
122
InputFieldList fields;
120
- std::string description;
123
+ std::string_view description;
121
124
};
122
125
123
126
using InputTypeList = std::vector<InputType>;
124
127
125
128
// Directives are defined with arguments and a list of valid locations.
126
129
struct Directive
127
130
{
128
- std::string name;
129
- std::vector<std::string > locations;
131
+ std::string_view name;
132
+ std::vector<std::string_view > locations;
130
133
InputFieldList arguments;
131
- std::string description;
134
+ std::string_view description;
132
135
};
133
136
134
137
using DirectiveList = std::vector<Directive>;
135
138
136
139
// Union types map a type name to a set of potential concrete type names.
137
140
struct UnionType
138
141
{
139
- std::string type;
140
- std::string cppType;
141
- std::vector<std::string > options;
142
- std::string description;
142
+ std::string_view type;
143
+ std::string_view cppType;
144
+ std::vector<std::string_view > options;
145
+ std::string_view description;
143
146
};
144
147
145
148
using UnionTypeList = std::vector<UnionType>;
@@ -163,14 +166,14 @@ constexpr std::string_view strApply = "apply";
163
166
164
167
struct OutputField
165
168
{
166
- std::string type;
167
- std::string name;
168
- std::string cppName;
169
+ std::string_view type;
170
+ std::string_view name;
171
+ std::string_view cppName;
169
172
InputFieldList arguments;
170
173
OutputFieldType fieldType = OutputFieldType::Builtin;
171
174
TypeModifierStack modifiers;
172
- std::string description;
173
- std::optional<std::string > deprecationReason;
175
+ std::string_view description;
176
+ std::optional<std::string_view > deprecationReason;
174
177
std::optional<tao::graphqlpeg::position> position;
175
178
bool interfaceField = false ;
176
179
bool inheritedField = false ;
@@ -185,10 +188,10 @@ using OutputFieldList = std::vector<OutputField>;
185
188
// conditions. The fields can include any output type.
186
189
struct InterfaceType
187
190
{
188
- std::string type;
189
- std::string cppType;
191
+ std::string_view type;
192
+ std::string_view cppType;
190
193
OutputFieldList fields;
191
- std::string description;
194
+ std::string_view description;
192
195
};
193
196
194
197
using InterfaceTypeList = std::vector<InterfaceType>;
@@ -197,22 +200,22 @@ using InterfaceTypeList = std::vector<InterfaceType>;
197
200
// may inherit multiple interfaces.
198
201
struct ObjectType
199
202
{
200
- std::string type;
201
- std::string cppType;
202
- std::vector<std::string > interfaces;
203
- std::vector<std::string > unions;
203
+ std::string_view type;
204
+ std::string_view cppType;
205
+ std::vector<std::string_view > interfaces;
206
+ std::vector<std::string_view > unions;
204
207
OutputFieldList fields;
205
- std::string description;
208
+ std::string_view description;
206
209
};
207
210
208
211
using ObjectTypeList = std::vector<ObjectType>;
209
212
210
213
// The schema maps operation types to named types.
211
214
struct OperationType
212
215
{
213
- std::string type;
214
- std::string cppType;
215
- std::string operation;
216
+ std::string_view type;
217
+ std::string_view cppType;
218
+ std::string_view operation;
216
219
};
217
220
218
221
using OperationTypeList = std::vector<OperationType>;
@@ -318,17 +321,16 @@ class Generator
318
321
void visitObjectTypeExtension (const peg::ast_node& objectTypeExtension);
319
322
void visitDirectiveDefinition (const peg::ast_node& directiveDefinition);
320
323
321
- static const std::string& getSafeCppName (const std::string& type) noexcept ;
322
- static OutputFieldList getOutputFields (
323
- const std::vector<std::unique_ptr<peg::ast_node>>& fields);
324
- static InputFieldList getInputFields (const std::vector<std::unique_ptr<peg::ast_node>>& fields);
324
+ static std::string_view getSafeCppName (std::string_view type) noexcept ;
325
+ static OutputFieldList getOutputFields (const peg::ast_node::children_t & fields);
326
+ static InputFieldList getInputFields (const peg::ast_node::children_t & fields);
325
327
326
328
// Recursively visit a Type node until we reach a NamedType and we've
327
329
// taken stock of all of the modifier wrappers.
328
330
class TypeVisitor
329
331
{
330
332
public:
331
- std::pair<std::string , TypeModifierStack> getType ();
333
+ std::pair<std::string_view , TypeModifierStack> getType ();
332
334
333
335
void visit (const peg::ast_node& typeName);
334
336
@@ -337,7 +339,7 @@ class Generator
337
339
void visitListType (const peg::ast_node& listType);
338
340
void visitNonNullType (const peg::ast_node& nonNullType);
339
341
340
- std::string _type;
342
+ std::string_view _type;
341
343
TypeModifierStack _modifiers;
342
344
bool _nonNull = false ;
343
345
};
@@ -366,11 +368,11 @@ class Generator
366
368
367
369
void validateSchema ();
368
370
void fixupOutputFieldList (OutputFieldList& fields,
369
- const std::optional<std::unordered_set<std::string >>& interfaceFields,
371
+ const std::optional<std::unordered_set<std::string_view >>& interfaceFields,
370
372
const std::optional<std::string_view>& accessor);
371
373
void fixupInputFieldList (InputFieldList& fields);
372
374
373
- const std::string& getCppType (const std::string& type) const noexcept ;
375
+ std::string_view getCppType (std::string_view type) const noexcept ;
374
376
std::string getInputCppType (const InputField& field) const noexcept ;
375
377
std::string getOutputCppType (const OutputField& field) const noexcept ;
376
378
@@ -393,14 +395,14 @@ class Generator
393
395
std::string getResultAccessType (const OutputField& result) const noexcept ;
394
396
std::string getTypeModifiers (const TypeModifierStack& modifiers) const noexcept ;
395
397
std::string getIntrospectionType (
396
- const std::string& type, const TypeModifierStack& modifiers) const noexcept ;
398
+ std::string_view type, const TypeModifierStack& modifiers) const noexcept ;
397
399
398
400
std::vector<std::string> outputSeparateFiles () const noexcept ;
399
401
400
- static const std::string s_introspectionNamespace;
402
+ static const std::string_view s_introspectionNamespace;
401
403
static const BuiltinTypeMap s_builtinTypes;
402
404
static const CppTypeMap s_builtinCppTypes;
403
- static const std::string s_scalarCppType;
405
+ static const std::string_view s_scalarCppType;
404
406
static const std::string s_currentDirectory;
405
407
406
408
const GeneratorOptions _options;
@@ -411,6 +413,7 @@ class Generator
411
413
const std::string _headerPath;
412
414
const std::string _objectHeaderPath;
413
415
const std::string _sourcePath;
416
+ peg::ast _ast;
414
417
415
418
SchemaTypeMap _schemaTypes;
416
419
PositionMap _typePositions;
0 commit comments