Exemple #1
0
        private CodeBlockBuilder CreateSerializeBody(
            InputModelSerializerDescriptor descriptor,
            string indent)
        {
            var body = new StringBuilder();

            AppendInitializationCheck(body, indent);

            body.AppendLine("if (value is null)");
            body.AppendLine("{");
            body.AppendLine($"{indent}return null;");
            body.AppendLine("}");
            body.AppendLine();

            body.AppendLine($"var input = ({descriptor.InputTypeName})value;");
            body.Append(NullableRefTypes
                ? "var map = new global::System.Collections.Generic.Dictionary<string, object?>();"
                : "var map = new global::System.Collections.Generic.Dictionary<string, object>();");

            foreach (InputFieldSerializerDescriptor field in descriptor.FieldSerializers)
            {
                body.AppendLine();
                body.AppendLine();
                AppendSerializeField(field, body, indent);
            }

            body.AppendLine();
            body.AppendLine();
            body.AppendLine("return map;");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #2
0
        private CodeBlockBuilder CreateSerializerMethodBody(
            EnumValueSerializerDescriptor descriptor,
            string indent)
        {
            var code = new StringBuilder();

            code.AppendLine("if (value is null)");
            code.AppendLine("{");
            code.AppendLine($"{indent}return null;");
            code.AppendLine("}");
            code.AppendLine();

            code.AppendLine($"var enumValue = ({descriptor.EnumTypeName})value");
            code.AppendLine();

            code.AppendLine("switch(enumValue)");
            code.AppendLine("{");

            foreach (EnumElementDescriptor element in descriptor.Elements)
            {
                code.AppendLine($"{indent}case {descriptor.EnumTypeName}.{element.Name}:");
                code.AppendLine($"{indent}{indent}return \"{element.SerializedName}\";");
            }

            code.AppendLine($"{indent}default:");
            code.AppendLine($"{indent}{indent}throw new NotSupportedException();");

            code.AppendLine("}");

            return(CodeBlockBuilder.FromStringBuilder(code));
        }
        private CodeBlockBuilder CreateConstructorBody(
            IReadOnlyList <ValueSerializerDescriptor> serializers,
            string indent)
        {
            var body = new StringBuilder();

            body.AppendLine("if (serializerResolver is null)");
            body.AppendLine("{");
            body.AppendLine(
                $"{indent}throw new {Types.ArgumentNullException}" +
                "(nameof(serializerResolver));");
            body.AppendLine("}");
            body.AppendLine();

            for (int i = 0; i < serializers.Count; i++)
            {
                if (i > 0)
                {
                    body.AppendLine();
                }
                body.Append(
                    $"{serializers[i].FieldName} = serializerResolver." +
                    $"Get(\"{serializers[i].Name}\");");
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #4
0
        private CodeBlockBuilder CreateDeserializerMethodBody(
            EnumValueSerializerDescriptor descriptor,
            string indent)
        {
            var code = new StringBuilder();

            code.AppendLine("if (serialized is null)");
            code.AppendLine("{");
            code.AppendLine($"{indent}return null;");
            code.AppendLine("}");
            code.AppendLine();

            code.AppendLine($"var stringValue = (string)serialized");
            code.AppendLine();

            code.AppendLine("switch(serialized)");
            code.AppendLine("{");

            foreach (EnumElementDescriptor element in descriptor.Elements)
            {
                code.AppendLine(
                    $"{indent}case \"{element.SerializedName}\":");
                code.AppendLine(
                    $"{indent}{indent}return {descriptor.EnumTypeName}.{element.Name};");
            }

            code.AppendLine($"{indent}default:");
            code.AppendLine($"{indent}{indent}throw new {Types.NotSupportedException}();");

            code.AppendLine("}");

            return(CodeBlockBuilder.FromStringBuilder(code));
        }
Exemple #5
0
        private static CodeBlockBuilder CreateDeserializeBody(string indent)
        {
            var body = new StringBuilder();

            body.AppendLine("throw new NotSupportedException(");
            body.Append($"{indent}\"Deserializing input values is not supported.\");");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private CodeBlockBuilder CreateConstructorBody(
            OutputModelDescriptor descriptor)
        {
            var body = new StringBuilder();

            foreach (OutputFieldDescriptor field in descriptor.Fields)
            {
                body.AppendLine($"{field.Name} = {field.ParameterName};");
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private static CodeBlockBuilder CreateGetVariableValuesBody(
            IReadOnlyList <OperationArgumentDescriptor> arguments,
            string indent)
        {
            if (arguments.Count == 0)
            {
                return(CodeBlockBuilder.New()
                       .AddCode(
                           "return global::System.Array.Empty<" +
                           "global::StrawberryShake.VariableValue>();"));
            }

            var body = new StringBuilder();

            body.AppendLine("var variables = new List<VariableValue>();");
            body.AppendLine();

            foreach (OperationArgumentDescriptor argument in arguments)
            {
                if (argument.IsOptional)
                {
                    body.AppendLine("if (Episode.HasValue)");
                    body.AppendLine("{");
                    body.AppendLine(
                        $"{indent}variables.Add(new VariableValue(" +
                        $"\"{argument.GraphQLName}\", " +
                        $"\"{argument.GraphQLType}\", " +
                        $"{argument.Name}.Value));");
                    body.AppendLine("}");
                }
                else
                {
                    body.AppendLine(
                        $"variables.Add(new VariableValue(" +
                        $"\"{argument.GraphQLName}\", " +
                        $"\"{argument.GraphQLType}\", " +
                        $"{argument.Name}));");
                }
                body.AppendLine();
            }

            body.Append("return variables;");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #8
0
        private static CodeBlockBuilder CreateToStringBody(
            string originalDocument)
        {
            var body = new StringBuilder();

            body.Append("return @\"");
#pragma warning disable CA1307
            body.Append(originalDocument
                        .Replace("\"", "\"\"")
                        .Replace("\r\n", "\n")
                        .Replace("\n\r", "\n")
                        .Replace("\r", "\n")
                        .Replace("\n", "\n"));
#pragma warning restore CA1307
            body.Append("\";");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private static CodeBlockBuilder CreateConstructorBody(
            IReadOnlyList <OperationArgumentDescriptor> arguments)
        {
            var body = new StringBuilder();

            bool first = true;

            foreach (OperationArgumentDescriptor argument in arguments.OrderBy(t => t.IsOptional))
            {
                if (first)
                {
                    body.AppendLine();
                    first = false;
                }

                body.Append($"{arguments[0].Name} = {arguments[0].ParameterName};");
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private static CodeBlockBuilder CreateParseDataMethodBody(
            ResultParserMethodDescriptor methodDescriptor,
            string indent)
        {
            var body = new StringBuilder();

            body.Append($"return ");

            AppendNewObject(
                body,
                methodDescriptor.PossibleTypes[0].Name,
                "data",
                methodDescriptor.PossibleTypes[0].Fields,
                indent,
                string.Empty);

            body.Append(";");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private CodeBlockBuilder CreateParseMethodBody(
            ResultParserMethodDescriptor methodDescriptor,
            string indent)
        {
            var body = new StringBuilder();

            if (methodDescriptor.ResultType.Components[0].IsList &&
                methodDescriptor.ResultType.Components[1].IsList)
            {
                AppendNestedList(body, methodDescriptor, indent);
            }
            else if (methodDescriptor.ResultType.Components[0].IsList)
            {
                AppendList(body, methodDescriptor, indent);
            }
            else
            {
                AppendObject(body, methodDescriptor, indent);
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #12
0
        private static CodeBlockBuilder CreateInitializeBody(
            IReadOnlyList <ValueSerializerDescriptor> serializerDescriptors,
            string indent)
        {
            var body = new StringBuilder();

            body.AppendLine("if (serializerResolver is null)");
            body.AppendLine("{");
            body.AppendLine(
                $"{indent}throw new global::System.ArgumentNullException(nameof(serializerResolver));");
            body.AppendLine("}");
            body.AppendLine();

            foreach (ValueSerializerDescriptor serializer in serializerDescriptors)
            {
                body.Append($"{serializer.FieldName} = ");
                body.AppendLine($"serializerResolver.Get(\"{serializer.Name}\")");
            }

            body.Append("_needsInitialization = false;");

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #13
0
        private CodeBlockBuilder CreateTypeSerializeBody(
            InputTypeSerializerMethodDescriptor descriptor,
            string indent)
        {
            var body = new StringBuilder();

            if (descriptor.IsNullableType)
            {
                body.AppendLine("if (value is null)");
                body.AppendLine("{");
                body.AppendLine($"{indent}return null;");
                body.AppendLine("}");
                body.AppendLine();
            }

            if (descriptor.IsListSerializer)
            {
                body.AppendLine("var source = (global::System.Collections.IList)value;");
                body.AppendLine(NullableRefTypes
                    ? "object?[] serialized = new object?[source.Count];"
                    : "object[] serialized = new object[source.Count];");
                body.AppendLine("for(int i = 0; i < source.Count; i++)");
                body.AppendLine("{");
                body.AppendLine(
                    $"{indent}serialized[i] = {descriptor.SerializerMethodName}(source[i]);");
                body.AppendLine("}");
                body.AppendLine();
                body.AppendLine("return result;");
            }
            else
            {
                body.Append($"{descriptor.ValueSerializerFieldName}!.Serialize(value);");
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
        private CodeBlockBuilder CreateDeserializeMethodBody(
            ResultParserDeserializerMethodDescriptor methodDescriptor,
            ImmutableQueue <ResultTypeComponentDescriptor> runtimeTypeComponents,
            string indent)
        {
            var body = new StringBuilder();

            ImmutableQueue <ResultTypeComponentDescriptor> next =
                runtimeTypeComponents.Dequeue(out ResultTypeComponentDescriptor type);

            if (type.IsList && next.Peek().IsList)
            {
            }
            else if (type.IsList)
            {
                AppendDeserializeLeafList(body, methodDescriptor, type, next, indent);
            }
            else
            {
                AppendDeserializeLeaf(body, methodDescriptor, type, indent);
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }
Exemple #15
0
        private static CodeBlockBuilder CreateAddClientBody(
            DependencyInjectionDescriptor descriptor,
            string indent)
        {
            var body = new StringBuilder();

            body.AppendLine("if (serviceCollection is null)");
            body.AppendLine("{");
            body.AppendLine(
                $"{indent}throw new global::System.ArgumentNullException(" +
                "nameof(serviceCollection));");
            body.AppendLine("}");
            body.AppendLine();

            body.AppendLine(
                "serviceCollection.AddSingleton" +
                $"<{descriptor.ClientTypeName}, {descriptor.ClientInterfaceTypeName}>();");
            body.AppendLine();

            body.AppendLine(
                "serviceCollection.AddSingleton<global::StrawberryShake." +
                "IOperationExecutorFactory>(sp =>");
            body.AppendLine(
                $"{indent}new global::StrawberryShake.Http.HttpOperationExecutorFactory(");
            body.AppendLine($"{indent}{indent}_clientName,");
            body.AppendLine(
                $"{indent}{indent}sp.GetRequiredService<global::System.Net.Http." +
                "IHttpClientFactory>().CreateClient,");
            body.AppendLine(
                $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                "Configuration.IClientOptions>()" +
                ".GetOperationPipeline<global::StrawberryShake.Http." +
                "IHttpOperationContext>(_clientName),");
            body.AppendLine(
                $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                "Configuration.IClientOptions>()." +
                "GetOperationFormatter(_clientName),");
            body.AppendLine(
                $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                "Configuration.IClientOptions>()." +
                "GetResultParsers(_clientName)));");
            body.AppendLine();

            if (descriptor.EnableSubscriptions)
            {
                body.AppendLine(
                    "serviceCollection.AddSingleton<global::StrawberryShake." +
                    "IOperationStreamExecutorFactory>(sp =>");
                body.AppendLine(
                    $"{indent}new global::StrawberryShake.Http.Subscriptions." +
                    "SocketOperationStreamExecutorFactory(");
                body.AppendLine($"{indent}{indent}_clientName,");
                body.AppendLine(
                    $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                    "Transport.ISocketConnectionPool>().RentAsync,");
                body.AppendLine(
                    $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake.Http." +
                    "Subscriptions.ISubscriptionManager>(),");
                body.AppendLine(
                    $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                    "Configuration.IClientOptions>().GetOperationFormatter(_clientName),");
                body.AppendLine(
                    $"{indent}{indent}sp.GetRequiredService<global::StrawberryShake." +
                    "Configuration.IClientOptions>().GetResultParsers(_clientName)));");
                body.AppendLine();
            }

            body.AppendLine(
                "global::StrawberryShake.Configuration.IOperationClientBuilder builder = " +
                "serviceCollection.AddOperationClientOptions(_clientName)");
            AppendSerializerRegistrations(descriptor.ValueSerializers, body, indent);
            AppendParserRegistrations(descriptor.ResultParsers, body, indent);
            body.AppendLine(
                ".AddOperationFormatter(serializers => new global::StrawberryShake." +
                "Http.JsonOperationFormatter(serializers))");
            body.AppendLine(
                ".AddHttpOperationPipeline(builder => builder.UseHttpDefaultPipeline());");

            if (descriptor.EnableSubscriptions)
            {
                body.AppendLine(
                    "serviceCollection.TryAddSingleton<global::StrawberryShake.Http." +
                    "Subscriptions.ISubscriptionManager, global::StrawberryShake.Http." +
                    "Subscriptions.SubscriptionManager>();");
            }

            body.Append(
                "serviceCollection.TryAddSingleton<global::StrawberryShake." +
                "IOperationExecutorPool, global::StrawberryShake.OperationExecutorPool>();");

            if (descriptor.EnableSubscriptions)
            {
                body.AppendLine();
                body.AppendLine(
                    "serviceCollection.TryAddEnumerable(new global::Microsoft.Extensions." +
                    "DependencyInjection.ServiceDescriptor(");
                body.AppendLine(
                    $"{indent}typeof(global::StrawberryShake.Transport." +
                    "ISocketConnectionInterceptor),");
                body.AppendLine(
                    $"{indent}typeof(global::StrawberryShake.Http.Subscriptions." +
                    "MessagePipelineHandler),");
                body.AppendLine(
                    $"{indent}global::Microsoft.Extensions.DependencyInjection." +
                    "ServiceLifetime.Singleton));");
                body.AppendLine();
                body.Append("return builder;");
            }

            return(CodeBlockBuilder.FromStringBuilder(body));
        }