From 0b47cf154130abb74bbe85b9c1c909b5fa60b222 Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Thu, 16 Dec 2021 10:24:25 -0800 Subject: [PATCH 1/2] Default initialize input type struct members --- samples/learn/schema/StarWarsSchema.h | 4 ++-- samples/today/nointrospection/TodaySchema.h | 22 ++++++++++---------- samples/today/schema/TodaySchema.h | 22 ++++++++++---------- samples/validation/schema/ValidationSchema.h | 4 ++-- src/SchemaGenerator.cpp | 2 +- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/samples/learn/schema/StarWarsSchema.h b/samples/learn/schema/StarWarsSchema.h index 652d290f..59ac67bf 100644 --- a/samples/learn/schema/StarWarsSchema.h +++ b/samples/learn/schema/StarWarsSchema.h @@ -30,8 +30,8 @@ enum class Episode struct ReviewInput { - int stars; - std::optional commentary; + int stars {}; + std::optional commentary {}; }; namespace object { diff --git a/samples/today/nointrospection/TodaySchema.h b/samples/today/nointrospection/TodaySchema.h index 053a5157..bb0834b2 100644 --- a/samples/today/nointrospection/TodaySchema.h +++ b/samples/today/nointrospection/TodaySchema.h @@ -31,33 +31,33 @@ enum class TaskState struct CompleteTaskInput { - response::IdType id; - std::optional testTaskState; - std::optional isComplete; - std::optional clientMutationId; + response::IdType id {}; + std::optional testTaskState {}; + std::optional isComplete {}; + std::optional clientMutationId {}; }; struct ThirdNestedInput { - response::IdType id; + response::IdType id {}; }; struct FourthNestedInput { - response::IdType id; + response::IdType id {}; }; struct SecondNestedInput { - response::IdType id; - ThirdNestedInput third; + response::IdType id {}; + ThirdNestedInput third {}; }; struct FirstNestedInput { - response::IdType id; - SecondNestedInput second; - ThirdNestedInput third; + response::IdType id {}; + SecondNestedInput second {}; + ThirdNestedInput third {}; }; namespace object { diff --git a/samples/today/schema/TodaySchema.h b/samples/today/schema/TodaySchema.h index 053a5157..bb0834b2 100644 --- a/samples/today/schema/TodaySchema.h +++ b/samples/today/schema/TodaySchema.h @@ -31,33 +31,33 @@ enum class TaskState struct CompleteTaskInput { - response::IdType id; - std::optional testTaskState; - std::optional isComplete; - std::optional clientMutationId; + response::IdType id {}; + std::optional testTaskState {}; + std::optional isComplete {}; + std::optional clientMutationId {}; }; struct ThirdNestedInput { - response::IdType id; + response::IdType id {}; }; struct FourthNestedInput { - response::IdType id; + response::IdType id {}; }; struct SecondNestedInput { - response::IdType id; - ThirdNestedInput third; + response::IdType id {}; + ThirdNestedInput third {}; }; struct FirstNestedInput { - response::IdType id; - SecondNestedInput second; - ThirdNestedInput third; + response::IdType id {}; + SecondNestedInput second {}; + ThirdNestedInput third {}; }; namespace object { diff --git a/samples/validation/schema/ValidationSchema.h b/samples/validation/schema/ValidationSchema.h index 98d554ce..129ac91e 100644 --- a/samples/validation/schema/ValidationSchema.h +++ b/samples/validation/schema/ValidationSchema.h @@ -35,8 +35,8 @@ enum class CatCommand struct ComplexInput { - std::optional name; - std::optional owner; + std::optional name {}; + std::optional owner {}; }; namespace object { diff --git a/src/SchemaGenerator.cpp b/src/SchemaGenerator.cpp index 3ff453f0..8a0ca86e 100644 --- a/src/SchemaGenerator.cpp +++ b/src/SchemaGenerator.cpp @@ -208,7 +208,7 @@ static_assert(graphql::internal::MinorVersion == )cpp" )cpp"; for (const auto& inputField : inputType.fields) { - headerFile << getFieldDeclaration(inputField) << R"cpp(; + headerFile << getFieldDeclaration(inputField) << R"cpp( {}; )cpp"; } headerFile << R"cpp(}; From 70e804fd34a0f0196c91db3a0c35924a677fded8 Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Thu, 16 Dec 2021 10:51:19 -0800 Subject: [PATCH 2/2] Fix scope of headerPath and sourcePath variables --- src/SchemaGenerator.cpp | 331 +++++++++++++++++++--------------------- 1 file changed, 153 insertions(+), 178 deletions(-) diff --git a/src/SchemaGenerator.cpp b/src/SchemaGenerator.cpp index 8a0ca86e..4c0c72c0 100644 --- a/src/SchemaGenerator.cpp +++ b/src/SchemaGenerator.cpp @@ -2654,93 +2654,87 @@ std::vector Generator::outputSeparateFiles() const noexcept } } + std::ostringstream ossNamespace; + + ossNamespace << R"cpp(graphql::)cpp" << _loader.getSchemaNamespace(); + + const auto schemaNamespace = ossNamespace.str(); + std::ostringstream ossInterfaceNamespace; + + ossInterfaceNamespace << schemaNamespace << R"cpp(::object)cpp"; + + const auto objectNamespace = ossInterfaceNamespace.str(); + for (const auto& interfaceType : _loader.getInterfaceTypes()) { const auto headerFilename = std::string(interfaceType.cppType) + "Object.h"; auto headerPath = (headerDir / headerFilename).string(); - std::ofstream headerFile(headerPath, std::ios_base::trunc); - IncludeGuardScope includeGuard { headerFile, headerFilename }; - - headerFile << R"cpp(#include ")cpp" - << std::filesystem::path(_headerPath).filename().string() << R"cpp(" - -)cpp"; - - std::ostringstream ossNamespace; - ossNamespace << R"cpp(graphql::)cpp" << _loader.getSchemaNamespace(); + { + std::ofstream headerFile(headerPath, std::ios_base::trunc); + IncludeGuardScope includeGuard { headerFile, headerFilename }; - const auto schemaNamespace = ossNamespace.str(); - std::ostringstream ossInterfaceNamespace; + headerFile << R"cpp(#include ")cpp" + << std::filesystem::path(_headerPath).filename().string() << R"cpp(" - ossInterfaceNamespace << schemaNamespace << R"cpp(::object)cpp"; +)cpp"; - const auto objectNamespace = ossInterfaceNamespace.str(); - NamespaceScope headerNamespace { headerFile, objectNamespace }; + NamespaceScope headerNamespace { headerFile, objectNamespace }; - // Output the full declaration - headerFile << std::endl; - outputInterfaceDeclaration(headerFile, interfaceType.cppType); - headerFile << std::endl; + // Output the full declaration + headerFile << std::endl; + outputInterfaceDeclaration(headerFile, interfaceType.cppType); + headerFile << std::endl; - if (_options.verbose) - { - files.push_back(std::move(headerPath)); + if (_options.verbose) + { + files.push_back(std::move(headerPath)); + } } const auto sourceFilename = std::string(interfaceType.cppType) + "Object.cpp"; auto sourcePath = (sourceDir / sourceFilename).string(); - std::ofstream sourceFile(sourcePath, std::ios_base::trunc); - sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. + { + std::ofstream sourceFile(sourcePath, std::ios_base::trunc); + + sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // WARNING! Do not edit this file manually, your changes will be overwritten. #include ")cpp" << headerFilename - << R"cpp(" -)cpp"; + << R"cpp(" - if (_loader.isIntrospection()) - { - sourceFile << R"cpp( -#include "graphqlservice/internal/Introspection.h" -)cpp"; - } - else - { - sourceFile << R"cpp( #include "graphqlservice/internal/Schema.h" #include "graphqlservice/introspection/IntrospectionSchema.h" -)cpp"; - } - sourceFile << R"cpp( using namespace std::literals; )cpp"; - NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; - NamespaceScope sourceInterfaceNamespace { sourceFile, "object" }; + NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; + NamespaceScope sourceInterfaceNamespace { sourceFile, "object" }; - sourceFile << std::endl; - outputInterfaceImplementation(sourceFile, interfaceType.cppType); - sourceFile << std::endl; + sourceFile << std::endl; + outputInterfaceImplementation(sourceFile, interfaceType.cppType); + sourceFile << std::endl; - sourceInterfaceNamespace.exit(); - sourceFile << std::endl; + sourceInterfaceNamespace.exit(); + sourceFile << std::endl; - sourceFile << R"cpp(void Add)cpp" << interfaceType.cppType - << R"cpp(Details(const std::shared_ptr& type)cpp" - << interfaceType.cppType - << R"cpp(, const std::shared_ptr& schema) + sourceFile << R"cpp(void Add)cpp" << interfaceType.cppType + << R"cpp(Details(const std::shared_ptr& type)cpp" + << interfaceType.cppType + << R"cpp(, const std::shared_ptr& schema) { )cpp"; - outputInterfaceIntrospection(sourceFile, interfaceType); - sourceFile << R"cpp(} + outputInterfaceIntrospection(sourceFile, interfaceType); + sourceFile << R"cpp(} )cpp"; + } files.push_back(std::move(sourcePath)); } @@ -2749,30 +2743,23 @@ using namespace std::literals; { const auto headerFilename = std::string(unionType.cppType) + "Object.h"; auto headerPath = (headerDir / headerFilename).string(); - std::ofstream headerFile(headerPath, std::ios_base::trunc); - IncludeGuardScope includeGuard { headerFile, headerFilename }; - - headerFile << R"cpp(#include ")cpp" - << std::filesystem::path(_headerPath).filename().string() << R"cpp(" -)cpp"; - - std::ostringstream ossNamespace; - - ossNamespace << R"cpp(graphql::)cpp" << _loader.getSchemaNamespace(); + { + std::ofstream headerFile(headerPath, std::ios_base::trunc); + IncludeGuardScope includeGuard { headerFile, headerFilename }; - const auto schemaNamespace = ossNamespace.str(); - std::ostringstream ossUnionNamespace; + headerFile << R"cpp(#include ")cpp" + << std::filesystem::path(_headerPath).filename().string() << R"cpp(" - ossUnionNamespace << schemaNamespace << R"cpp(::object)cpp"; +)cpp"; - const auto objectNamespace = ossUnionNamespace.str(); - NamespaceScope headerNamespace { headerFile, objectNamespace }; + NamespaceScope headerNamespace { headerFile, objectNamespace }; - // Output the full declaration - headerFile << std::endl; - outputInterfaceDeclaration(headerFile, unionType.cppType); - headerFile << std::endl; + // Output the full declaration + headerFile << std::endl; + outputInterfaceDeclaration(headerFile, unionType.cppType); + headerFile << std::endl; + } if (_options.verbose) { @@ -2781,56 +2768,47 @@ using namespace std::literals; const auto sourceFilename = std::string(unionType.cppType) + "Object.cpp"; auto sourcePath = (sourceDir / sourceFilename).string(); - std::ofstream sourceFile(sourcePath, std::ios_base::trunc); - sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. + { + std::ofstream sourceFile(sourcePath, std::ios_base::trunc); + + sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // WARNING! Do not edit this file manually, your changes will be overwritten. #include ")cpp" << headerFilename - << R"cpp(" -)cpp"; + << R"cpp(" - if (_loader.isIntrospection()) - { - sourceFile << R"cpp( -#include "graphqlservice/internal/Introspection.h" -)cpp"; - } - else - { - sourceFile << R"cpp( #include "graphqlservice/internal/Schema.h" #include "graphqlservice/introspection/IntrospectionSchema.h" -)cpp"; - } - sourceFile << R"cpp( using namespace std::literals; )cpp"; - NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; - NamespaceScope sourceUnionNamespace { sourceFile, "object" }; + NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; + NamespaceScope sourceUnionNamespace { sourceFile, "object" }; - sourceFile << std::endl; - outputInterfaceImplementation(sourceFile, unionType.cppType); - sourceFile << std::endl; + sourceFile << std::endl; + outputInterfaceImplementation(sourceFile, unionType.cppType); + sourceFile << std::endl; - sourceUnionNamespace.exit(); - sourceFile << std::endl; + sourceUnionNamespace.exit(); + sourceFile << std::endl; - sourceFile << R"cpp(void Add)cpp" << unionType.cppType - << R"cpp(Details(const std::shared_ptr& type)cpp" - << unionType.cppType << R"cpp(, const std::shared_ptr& schema) + sourceFile << R"cpp(void Add)cpp" << unionType.cppType + << R"cpp(Details(const std::shared_ptr& type)cpp" + << unionType.cppType + << R"cpp(, const std::shared_ptr& schema) { )cpp"; - outputUnionIntrospection(sourceFile, unionType); - sourceFile << R"cpp(} + outputUnionIntrospection(sourceFile, unionType); + sourceFile << R"cpp(} )cpp"; + } files.push_back(std::move(sourcePath)); } @@ -2840,55 +2818,48 @@ using namespace std::literals; const bool isQueryType = objectType.type == queryType; const auto headerFilename = std::string(objectType.cppType) + "Object.h"; auto headerPath = (headerDir / headerFilename).string(); - std::ofstream headerFile(headerPath, std::ios_base::trunc); - IncludeGuardScope includeGuard { headerFile, headerFilename }; - headerFile << R"cpp(#include ")cpp" - << std::filesystem::path(_headerPath).filename().string() << R"cpp(" + { + std::ofstream headerFile(headerPath, std::ios_base::trunc); + IncludeGuardScope includeGuard { headerFile, headerFilename }; + + headerFile << R"cpp(#include ")cpp" + << std::filesystem::path(_headerPath).filename().string() << R"cpp(" )cpp"; - std::ostringstream ossNamespace; + NamespaceScope headerNamespace { headerFile, objectNamespace }; - ossNamespace << R"cpp(graphql::)cpp" << _loader.getSchemaNamespace(); + if (!_loader.isIntrospection()) + { + if (!objectType.interfaces.empty() || !objectType.unions.empty()) + { + NamespaceScope implementsNamespace { headerFile, R"cpp(implements)cpp" }; - const auto schemaNamespace = ossNamespace.str(); - std::ostringstream ossObjectNamespace; + headerFile << std::endl; + outputObjectImplements(headerFile, objectType); - ossObjectNamespace << schemaNamespace << R"cpp(::object)cpp"; + implementsNamespace.exit(); + headerFile << std::endl; + } - const auto objectNamespace = ossObjectNamespace.str(); - NamespaceScope headerNamespace { headerFile, objectNamespace }; + // Output the stub concepts + std::ostringstream ossConceptNamespace; - if (!_loader.isIntrospection()) - { - if (!objectType.interfaces.empty() || !objectType.unions.empty()) - { - NamespaceScope implementsNamespace { headerFile, R"cpp(implements)cpp" }; + ossConceptNamespace << R"cpp(methods::)cpp" << objectType.cppType << R"cpp(Has)cpp"; - headerFile << std::endl; - outputObjectImplements(headerFile, objectType); + const auto conceptNamespace = ossConceptNamespace.str(); + NamespaceScope stubNamespace { headerFile, conceptNamespace }; - implementsNamespace.exit(); - headerFile << std::endl; + outputObjectStubs(headerFile, objectType); } - // Output the stub concepts - std::ostringstream ossConceptNamespace; - - ossConceptNamespace << R"cpp(methods::)cpp" << objectType.cppType << R"cpp(Has)cpp"; - - const auto conceptNamespace = ossConceptNamespace.str(); - NamespaceScope stubNamespace { headerFile, conceptNamespace }; - - outputObjectStubs(headerFile, objectType); + // Output the full declaration + headerFile << std::endl; + outputObjectDeclaration(headerFile, objectType, isQueryType); + headerFile << std::endl; } - // Output the full declaration - headerFile << std::endl; - outputObjectDeclaration(headerFile, objectType, isQueryType); - headerFile << std::endl; - if (_options.verbose) { files.push_back(std::move(headerPath)); @@ -2896,63 +2867,65 @@ using namespace std::literals; const auto sourceFilename = std::string(objectType.cppType) + "Object.cpp"; auto sourcePath = (sourceDir / sourceFilename).string(); - std::ofstream sourceFile(sourcePath, std::ios_base::trunc); - sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. + { + std::ofstream sourceFile(sourcePath, std::ios_base::trunc); + + sourceFile << R"cpp(// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // WARNING! Do not edit this file manually, your changes will be overwritten. #include ")cpp" << headerFilename - << R"cpp(" + << R"cpp(" )cpp"; - std::unordered_set includedObjects; + std::unordered_set includedObjects; - for (const auto& field : objectType.fields) - { - switch (field.fieldType) + for (const auto& field : objectType.fields) { - case OutputFieldType::Interface: - case OutputFieldType::Union: - case OutputFieldType::Object: - if (includedObjects.insert(field.type).second) - { - sourceFile << R"cpp(#include ")cpp" << _loader.getSafeCppName(field.type) - << R"cpp(Object.h" -)cpp"; - } - break; - - default: - break; + switch (field.fieldType) + { + case OutputFieldType::Interface: + case OutputFieldType::Union: + case OutputFieldType::Object: + if (includedObjects.insert(field.type).second) + { + sourceFile << R"cpp(#include ")cpp" + << _loader.getSafeCppName(field.type) << R"cpp(Object.h" +)cpp"; + } + break; + + default: + break; + } } - } - if (_loader.isIntrospection() || (isQueryType && !_options.noIntrospection)) - { - sourceFile << R"cpp( + if (_loader.isIntrospection() || (isQueryType && !_options.noIntrospection)) + { + sourceFile << R"cpp( #include "graphqlservice/internal/Introspection.h" )cpp"; - } - else - { - sourceFile << R"cpp( + } + else + { + sourceFile << R"cpp( #include "graphqlservice/internal/Schema.h" #include "graphqlservice/introspection/IntrospectionSchema.h" )cpp"; - } + } - if (isQueryType && !_options.noIntrospection) - { - sourceFile << R"cpp( + if (isQueryType && !_options.noIntrospection) + { + sourceFile << R"cpp( #include "graphqlservice/introspection/SchemaObject.h" #include "graphqlservice/introspection/TypeObject.h" )cpp"; - } + } - sourceFile << R"cpp( + sourceFile << R"cpp( #include #include #include @@ -2963,25 +2936,27 @@ using namespace std::literals; )cpp"; - NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; - NamespaceScope sourceObjectNamespace { sourceFile, "object" }; + NamespaceScope sourceSchemaNamespace { sourceFile, schemaNamespace }; + NamespaceScope sourceObjectNamespace { sourceFile, "object" }; - sourceFile << std::endl; - outputObjectImplementation(sourceFile, objectType, isQueryType); - sourceFile << std::endl; + sourceFile << std::endl; + outputObjectImplementation(sourceFile, objectType, isQueryType); + sourceFile << std::endl; - sourceObjectNamespace.exit(); - sourceFile << std::endl; + sourceObjectNamespace.exit(); + sourceFile << std::endl; - sourceFile << R"cpp(void Add)cpp" << objectType.cppType - << R"cpp(Details(const std::shared_ptr& type)cpp" - << objectType.cppType << R"cpp(, const std::shared_ptr& schema) + sourceFile << R"cpp(void Add)cpp" << objectType.cppType + << R"cpp(Details(const std::shared_ptr& type)cpp" + << objectType.cppType + << R"cpp(, const std::shared_ptr& schema) { )cpp"; - outputObjectIntrospection(sourceFile, objectType); - sourceFile << R"cpp(} + outputObjectIntrospection(sourceFile, objectType); + sourceFile << R"cpp(} )cpp"; + } files.push_back(std::move(sourcePath)); }