Skip to content

Commit a98690b

Browse files
committed
Refactor common logic into GeneratorUtil.*
1 parent 2f3cf47 commit a98690b

File tree

7 files changed

+347
-465
lines changed

7 files changed

+347
-465
lines changed

include/ClientGenerator.h

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#pragma once
55

6-
#ifndef SCHEMAGENERATOR_H
7-
#define SCHEMAGENERATOR_H
6+
#ifndef CLIENTGENERATOR_H
7+
#define CLIENTGENERATOR_H
88

99
#include "graphqlservice/GraphQLGrammar.h"
1010
#include "graphqlservice/GraphQLParse.h"
@@ -15,7 +15,7 @@
1515
#include <unordered_map>
1616
#include <unordered_set>
1717

18-
namespace graphql::schema {
18+
namespace graphql::client {
1919

2020
// These are the set of built-in types in GraphQL.
2121
enum class BuiltinType
@@ -210,7 +210,7 @@ struct ObjectType
210210

211211
using ObjectTypeList = std::vector<ObjectType>;
212212

213-
// The schema maps operation types to named types.
213+
// The client maps operation types to named types.
214214
struct OperationType
215215
{
216216
std::string_view type;
@@ -220,11 +220,11 @@ struct OperationType
220220

221221
using OperationTypeList = std::vector<OperationType>;
222222

223-
struct GeneratorSchema
223+
struct GeneratorClient
224224
{
225-
const std::string schemaFilename;
225+
const std::string clientFilename;
226226
const std::string filenamePrefix;
227-
const std::string schemaNamespace;
227+
const std::string clientNamespace;
228228
};
229229

230230
struct GeneratorPaths
@@ -235,63 +235,18 @@ struct GeneratorPaths
235235

236236
struct GeneratorOptions
237237
{
238-
const std::optional<GeneratorSchema> customSchema;
238+
const std::optional<GeneratorClient> customClient;
239239
const std::optional<GeneratorPaths> paths;
240240
const bool verbose = false;
241241
const bool separateFiles = false;
242242
const bool noStubs = false;
243243
const bool noIntrospection = false;
244244
};
245245

246-
// RAII object to help with emitting matching include guard begin and end statements
247-
class IncludeGuardScope
248-
{
249-
public:
250-
explicit IncludeGuardScope(std::ostream& outputFile, std::string_view headerFileName) noexcept;
251-
~IncludeGuardScope() noexcept;
252-
253-
private:
254-
std::ostream& _outputFile;
255-
std::string _includeGuardName;
256-
};
257-
258-
// RAII object to help with emitting matching namespace begin and end statements
259-
class NamespaceScope
260-
{
261-
public:
262-
explicit NamespaceScope(
263-
std::ostream& outputFile, std::string_view cppNamespace, bool deferred = false) noexcept;
264-
NamespaceScope(NamespaceScope&& other) noexcept;
265-
~NamespaceScope() noexcept;
266-
267-
bool enter() noexcept;
268-
bool exit() noexcept;
269-
270-
private:
271-
bool _inside = false;
272-
std::ostream& _outputFile;
273-
std::string_view _cppNamespace;
274-
};
275-
276-
// Keep track of whether we want to add a blank separator line once some additional content is about
277-
// to be output.
278-
class PendingBlankLine
279-
{
280-
public:
281-
explicit PendingBlankLine(std::ostream& outputFile) noexcept;
282-
283-
void add() noexcept;
284-
bool reset() noexcept;
285-
286-
private:
287-
bool _pending = true;
288-
std::ostream& _outputFile;
289-
};
290-
291246
class Generator
292247
{
293248
public:
294-
// Initialize the generator with the introspection schema or a custom GraphQL schema.
249+
// Initialize the generator with the introspection client or a custom GraphQL client.
295250
explicit Generator(GeneratorOptions&& options);
296251

297252
// Run the generator and return a list of filenames that were output.
@@ -306,8 +261,8 @@ class Generator
306261

307262
void visitDefinition(const peg::ast_node& definition);
308263

309-
void visitSchemaDefinition(const peg::ast_node& schemaDefinition);
310-
void visitSchemaExtension(const peg::ast_node& schemaExtension);
264+
void visitClientDefinition(const peg::ast_node& clientDefinition);
265+
void visitClientExtension(const peg::ast_node& clientExtension);
311266
void visitScalarTypeDefinition(const peg::ast_node& scalarTypeDefinition);
312267
void visitEnumTypeDefinition(const peg::ast_node& enumTypeDefinition);
313268
void visitEnumTypeExtension(const peg::ast_node& enumTypeExtension);
@@ -366,7 +321,7 @@ class Generator
366321
response::Value _value;
367322
};
368323

369-
void validateSchema();
324+
void validateClient();
370325
void fixupOutputFieldList(OutputFieldList& fields,
371326
const std::optional<std::unordered_set<std::string_view>>& interfaceFields,
372327
const std::optional<std::string_view>& accessor);
@@ -407,15 +362,15 @@ class Generator
407362

408363
const GeneratorOptions _options;
409364
const bool _isIntrospection;
410-
std::string_view _schemaNamespace;
365+
std::string_view _clientNamespace;
411366
const std::string _headerDir;
412367
const std::string _sourceDir;
413368
const std::string _headerPath;
414369
const std::string _objectHeaderPath;
415370
const std::string _sourcePath;
416371
peg::ast _ast;
417372

418-
SchemaTypeMap _schemaTypes;
373+
SchemaTypeMap _clientTypes;
419374
PositionMap _typePositions;
420375
TypeNameMap _scalarNames;
421376
ScalarTypeList _scalarTypes;
@@ -434,6 +389,6 @@ class Generator
434389
OperationTypeList _operationTypes;
435390
};
436391

437-
} /* namespace graphql::schema */
392+
} /* namespace graphql::client */
438393

439-
#endif // SCHEMAGENERATOR_H
394+
#endif // CLIENTGENERATOR_H

include/GeneratorUtil.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#ifndef GENERATORUTIL_H
7+
#define GENERATORUTIL_H
8+
9+
#include <iostream>
10+
#include <string>
11+
#include <string_view>
12+
13+
namespace graphql::generator {
14+
15+
// RAII object to help with emitting matching include guard begin and end statements
16+
class IncludeGuardScope
17+
{
18+
public:
19+
explicit IncludeGuardScope(std::ostream& outputFile, std::string_view headerFileName) noexcept;
20+
~IncludeGuardScope() noexcept;
21+
22+
private:
23+
std::ostream& _outputFile;
24+
std::string _includeGuardName;
25+
};
26+
27+
// RAII object to help with emitting matching namespace begin and end statements
28+
class NamespaceScope
29+
{
30+
public:
31+
explicit NamespaceScope(
32+
std::ostream& outputFile, std::string_view cppNamespace, bool deferred = false) noexcept;
33+
NamespaceScope(NamespaceScope&& other) noexcept;
34+
~NamespaceScope() noexcept;
35+
36+
bool enter() noexcept;
37+
bool exit() noexcept;
38+
39+
private:
40+
bool _inside = false;
41+
std::ostream& _outputFile;
42+
std::string_view _cppNamespace;
43+
};
44+
45+
// Keep track of whether we want to add a blank separator line once some additional content is about
46+
// to be output.
47+
class PendingBlankLine
48+
{
49+
public:
50+
explicit PendingBlankLine(std::ostream& outputFile) noexcept;
51+
52+
void add() noexcept;
53+
bool reset() noexcept;
54+
55+
private:
56+
bool _pending = true;
57+
std::ostream& _outputFile;
58+
};
59+
60+
} /* namespace graphql::generator */
61+
62+
#endif // GENERATORUTIL_H

include/SchemaGenerator.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -243,51 +243,6 @@ struct GeneratorOptions
243243
const bool noIntrospection = false;
244244
};
245245

246-
// RAII object to help with emitting matching include guard begin and end statements
247-
class IncludeGuardScope
248-
{
249-
public:
250-
explicit IncludeGuardScope(std::ostream& outputFile, std::string_view headerFileName) noexcept;
251-
~IncludeGuardScope() noexcept;
252-
253-
private:
254-
std::ostream& _outputFile;
255-
std::string _includeGuardName;
256-
};
257-
258-
// RAII object to help with emitting matching namespace begin and end statements
259-
class NamespaceScope
260-
{
261-
public:
262-
explicit NamespaceScope(
263-
std::ostream& outputFile, std::string_view cppNamespace, bool deferred = false) noexcept;
264-
NamespaceScope(NamespaceScope&& other) noexcept;
265-
~NamespaceScope() noexcept;
266-
267-
bool enter() noexcept;
268-
bool exit() noexcept;
269-
270-
private:
271-
bool _inside = false;
272-
std::ostream& _outputFile;
273-
std::string_view _cppNamespace;
274-
};
275-
276-
// Keep track of whether we want to add a blank separator line once some additional content is about
277-
// to be output.
278-
class PendingBlankLine
279-
{
280-
public:
281-
explicit PendingBlankLine(std::ostream& outputFile) noexcept;
282-
283-
void add() noexcept;
284-
bool reset() noexcept;
285-
286-
private:
287-
bool _pending = true;
288-
std::ostream& _outputFile;
289-
};
290-
291246
class Generator
292247
{
293248
public:

src/CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ if(WIN32 AND BUILD_SHARED_LIBS)
9999
add_version_rc(graphqlresponse)
100100
endif()
101101

102+
103+
# Common schemagen and clientgen dependencies
104+
if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN)
105+
add_library(generator_util STATIC GeneratorUtil.cpp)
106+
target_link_libraries(generator_util PUBLIC
107+
graphqlpeg
108+
graphqlresponse)
109+
endif()
110+
102111
# schemagen
103112
if(GRAPHQL_BUILD_SCHEMAGEN)
104113
add_executable(schemagen SchemaGenerator.cpp)
105114
add_executable(cppgraphqlgen::schemagen ALIAS schemagen)
106-
target_link_libraries(schemagen PRIVATE
107-
graphqlpeg
108-
graphqlresponse)
115+
target_link_libraries(schemagen PRIVATE generator_util)
109116
add_bigobj_flag(schemagen)
110117

111118
# SchemaGen.rc
@@ -142,9 +149,7 @@ endif()
142149
if(GRAPHQL_BUILD_CLIENTGEN)
143150
add_executable(clientgen ClientGenerator.cpp)
144151
add_executable(cppgraphqlgen::clientgen ALIAS clientgen)
145-
target_link_libraries(clientgen PRIVATE
146-
graphqlpeg
147-
graphqlresponse)
152+
target_link_libraries(clientgen PRIVATE generator_util)
148153
add_bigobj_flag(clientgen)
149154

150155
# ClientGen.rc

0 commit comments

Comments
 (0)