See More

// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "GeneratorLoader.h" #include "graphqlservice/internal/Grammar.h" #include #include namespace graphql::generator { void TypeVisitor::visit(const peg::ast_node& typeName) { if (typeName.is_type<:nonnull_type>()) { visitNonNullType(typeName); } else if (typeName.is_type<:list_type>()) { visitListType(typeName); } else if (typeName.is_type<:named_type>()) { visitNamedType(typeName); } } void TypeVisitor::visitNamedType(const peg::ast_node& namedType) { if (!_nonNull) { _modifiers.push_back(service::TypeModifier::Nullable); } _type = namedType.string_view(); } void TypeVisitor::visitListType(const peg::ast_node& listType) { if (!_nonNull) { _modifiers.push_back(service::TypeModifier::Nullable); } _nonNull = false; _modifiers.push_back(service::TypeModifier::List); visit(*listType.children.front()); } void TypeVisitor::visitNonNullType(const peg::ast_node& nonNullType) { _nonNull = true; visit(*nonNullType.children.front()); } std::pair<:string_view typemodifierstack> TypeVisitor::getType() { return { std::move(_type), std::move(_modifiers) }; } void DefaultValueVisitor::visit(const peg::ast_node& value) { if (value.is_type<:integer_value>()) { visitIntValue(value); } else if (value.is_type<:float_value>()) { visitFloatValue(value); } else if (value.is_type<:string_value>()) { visitStringValue(value); } else if (value.is_type<:true_keyword>() || value.is_type<:false_keyword>()) { visitBooleanValue(value); } else if (value.is_type<:null_keyword>()) { visitNullValue(value); } else if (value.is_type<:enum_value>()) { visitEnumValue(value); } else if (value.is_type<:list_value>()) { visitListValue(value); } else if (value.is_type<:object_value>()) { visitObjectValue(value); } else if (value.is_type<:variable>()) { const auto position = value.begin(); const auto error = std::format("Unexpected variable in default value line: {} column: {}", position.line, position.column); throw std::runtime_error(error); } } void DefaultValueVisitor::visitIntValue(const peg::ast_node& intValue) { _value = response::Value(std::atoi(intValue.string().c_str())); } void DefaultValueVisitor::visitFloatValue(const peg::ast_node& floatValue) { _value = response::Value(std::atof(floatValue.string().c_str())); } void DefaultValueVisitor::visitStringValue(const peg::ast_node& stringValue) { _value = response::Value(std::string { stringValue.unescaped_view() }); } void DefaultValueVisitor::visitBooleanValue(const peg::ast_node& booleanValue) { _value = response::Value(booleanValue.is_type<:true_keyword>()); } void DefaultValueVisitor::visitNullValue(const peg::ast_node& /*nullValue*/) { _value = {}; } void DefaultValueVisitor::visitEnumValue(const peg::ast_node& enumValue) { _value = response::Value(response::Type::EnumValue); _value.set<:string>(enumValue.string()); } void DefaultValueVisitor::visitListValue(const peg::ast_node& listValue) { _value = response::Value(response::Type::List); _value.reserve(listValue.children.size()); for (const auto& child : listValue.children) { DefaultValueVisitor visitor; visitor.visit(*child); _value.emplace_back(visitor.getValue()); } } void DefaultValueVisitor::visitObjectValue(const peg::ast_node& objectValue) { _value = response::Value(response::Type::Map); _value.reserve(objectValue.children.size()); for (const auto& field : objectValue.children) { DefaultValueVisitor visitor; visitor.visit(*field->children.back()); _value.emplace_back(field->children.front()->string(), visitor.getValue()); } } response::Value DefaultValueVisitor::getValue() { return response::Value(std::move(_value)); } } // namespace graphql::generator