Exemplo n.º 1
0
        protected override void Generate(
            CodeWriter writer,
            OperationDescriptor descriptor,
            out string fileName,
            out string?path)
        {
            fileName = descriptor.InterfaceType.Name;
            path     = null;

            InterfaceBuilder interfaceBuilder = InterfaceBuilder
                                                .New()
                                                .SetComment(
                XmlCommentBuilder
                .New()
                .SetSummary(
                    string.Format(
                        CodeGenerationResources.OperationServiceDescriptor_Description,
                        descriptor.Name))
                .AddCode(descriptor.BodyString))
                                                .AddImplements(TypeNames.IOperationRequestFactory)
                                                .SetName(fileName);

            var runtimeTypeName =
                descriptor.ResultTypeReference.GetRuntimeType().Name;

            if (descriptor is not SubscriptionOperationDescriptor)
            {
                interfaceBuilder.AddMethod(CreateExecuteMethod(descriptor, runtimeTypeName));
            }

            interfaceBuilder.AddMethod(CreateWatchMethod(descriptor, runtimeTypeName));

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(interfaceBuilder)
            .Build(writer);
        }
        protected override void Generate(OperationDescriptor descriptor,
                                         CSharpSyntaxGeneratorSettings settings,
                                         CodeWriter writer,
                                         out string fileName,
                                         out string?path,
                                         out string ns)
        {
            var documentName = CreateDocumentTypeName(descriptor.RuntimeType.Name);

            fileName = documentName;
            path     = null;
            ns       = descriptor.RuntimeType.NamespaceWithoutGlobal;

            string operationKind = descriptor switch
            {
                MutationOperationDescriptor => "Mutation",
                QueryOperationDescriptor => "Query",
                SubscriptionOperationDescriptor => "Subscription",
                _ => throw new ArgumentOutOfRangeException(nameof(descriptor))
            };

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName)
                                        .AddImplements(TypeNames.IDocument)
                                        .SetComment(
                XmlCommentBuilder
                .New()
                .SetSummary(
                    string.Format(
                        CodeGenerationResources.OperationServiceDescriptor_Description,
                        descriptor.Name))
                .AddCode(descriptor.BodyString));

            classBuilder
            .AddConstructor()
            .SetPrivate();

            classBuilder
            .AddProperty("Instance")
            .SetStatic()
            .SetType(documentName)
            .SetValue($"new {documentName}()");

            classBuilder
            .AddProperty("Kind")
            .SetType(TypeNames.OperationKind)
            .AsLambda($"{TypeNames.OperationKind}.{operationKind}");

            if (descriptor.Strategy == RequestStrategy.PersistedQuery)
            {
                classBuilder
                .AddProperty("Body")
                .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte))
                .AsLambda($"new {TypeNames.Byte}[0]");
            }
            else
            {
                classBuilder
                .AddProperty("Body")
                .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte))
                .AsLambda(GetByteArray(descriptor.Body));
            }

            classBuilder
            .AddProperty("Hash")
            .SetType(TypeNames.DocumentHash)
            .SetValue(
                $@"new {TypeNames.DocumentHash}(" +
                $@"""{descriptor.HashAlgorithm}"", " +
                $@"""{descriptor.HashValue}"")");

            classBuilder
            .AddMethod("ToString")
            .SetPublic()
            .SetOverride()
            .SetReturnType(TypeNames.String)
            .AddCode("#if NETSTANDARD2_0")
            .AddCode(MethodCallBuilder
                     .New()
                     .SetReturn()
                     .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString))
                     .AddArgument("Body.ToArray()"))
            .AddCode("#else")
            .AddCode(MethodCallBuilder
                     .New()
                     .SetReturn()
                     .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString))
                     .AddArgument("Body"))
            .AddCode("#endif");

            classBuilder.Build(writer);
        }
Exemplo n.º 3
0
        protected override void Generate(
            CodeWriter writer,
            OperationDescriptor descriptor,
            out string fileName,
            out string?path)
        {
            fileName = descriptor.RuntimeType.Name;
            path     = null;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetComment(
                XmlCommentBuilder
                .New()
                .SetSummary(
                    string.Format(
                        CodeGenerationResources.OperationServiceDescriptor_Description,
                        descriptor.Name))
                .AddCode(descriptor.BodyString))
                                        .AddImplements(descriptor.InterfaceType.ToString())
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = classBuilder
                                                    .AddConstructor()
                                                    .SetTypeName(fileName);

            var runtimeTypeName =
                descriptor.ResultTypeReference.GetRuntimeType().Name;

            AddConstructorAssignedField(
                TypeNames.IOperationExecutor.WithGeneric(runtimeTypeName),
                _operationExecutor,
                classBuilder,
                constructorBuilder);

            AddInjectedSerializers(descriptor, constructorBuilder, classBuilder);

            if (descriptor is not SubscriptionOperationDescriptor)
            {
                classBuilder.AddMethod(CreateExecuteMethod(descriptor, runtimeTypeName));
            }

            classBuilder.AddMethod(CreateWatchMethod(descriptor, runtimeTypeName));
            classBuilder.AddMethod(CreateRequestMethod(descriptor));
            classBuilder.AddMethod(CreateRequestVariablesMethod(descriptor));

            AddFormatMethods(descriptor, classBuilder);

            classBuilder
            .AddProperty("ResultType")
            .SetType(TypeNames.Type)
            .AsLambda($"typeof({runtimeTypeName})")
            .SetInterface(TypeNames.IOperationRequestFactory);

            MethodCallBuilder createRequestCall = MethodCallBuilder
                                                  .New()
                                                  .SetReturn()
                                                  .SetMethodName(_createRequest);

            if (descriptor.Arguments.Count > 0)
            {
                createRequestCall.AddArgument($"{_variables}!");
            }

            classBuilder
            .AddMethod("Create")
            .SetReturnType(TypeNames.OperationRequest)
            .SetInterface(TypeNames.IOperationRequestFactory)
            .AddParameter(
                _variables,
                x => x.SetType(
                    TypeNames.IReadOnlyDictionary
                    .WithGeneric(TypeNames.String, TypeNames.Object.MakeNullable())
                    .MakeNullable()))
            .AddCode(createRequestCall);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }