示例#1
0
        protected override Task WriteAsync(
            CodeWriter writer,
            InputModelSerializerDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetAccessModifier(AccessModifier.Public)
                                        .SetName(descriptor.Name)
                                        .AddImplements("global::StrawberryShake.IInputSerializer");

            AddFields(descriptor.ValueSerializers, classBuilder);
            AddProperties(descriptor, classBuilder);
            AddInitializeMethod(descriptor.ValueSerializers, classBuilder);
            AddSerializeMethod(descriptor, classBuilder);
            AddDeserializeMethod(classBuilder);
            AddTypeSerializerMethods(descriptor.TypeSerializerMethods, classBuilder);

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
示例#2
0
        protected override Task WriteAsync(
            CodeWriter writer,
            DependencyInjectionDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name)
                .AddField(FieldBuilder.New()
                          .SetConst()
                          .SetType("string")
                          .SetName("_clientName")
                          .SetValue($"\"{descriptor.ClientName}\""));

            AddAddClientMethod(classBuilder, descriptor, CodeWriter.Indent);

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddUsing("Microsoft.Extensions.DependencyInjection")
                   .AddUsing("Microsoft.Extensions.DependencyInjection.Extensions")
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
示例#3
0
        protected override Task WriteAsync(
            CodeWriter writer,
            ClientClassDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder builder = ClassBuilder.New()
                                   .SetAccessModifier(AccessModifier.Public)
                                   .SetName(descriptor.Name)
                                   .AddImplements(descriptor.InterfaceName);

            builder.AddField(
                FieldBuilder.New()
                .SetConst()
                .SetName("_clientName")
                .SetType("string")
                .SetValue($"\"{descriptor.Name}\""));

            ConstructorBuilder constructor = ConstructorBuilder.New()
                                             .AddParameter(
                ParameterBuilder.New()
                .SetName("executorPool")
                .SetType(descriptor.OperationExecutorPool));

            if (descriptor.OperationExecutor is { })
示例#4
0
        protected override void Generate(
            CodeWriter writer,
            InputObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                classBuilder
                .AddProperty(prop.Name)
                .SetPublic()
                .MakeSettable()
                .SetType(prop.Type.ToBuilder());
            }

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#5
0
        protected override void Generate(
            CodeWriter writer,
            NamedTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                var propBuilder = PropertyBuilder
                                  .New()
                                  .MakeSettable()
                                  .SetName(prop.Name)
                                  .SetType(propTypeBuilder)
                                  .SetAccessModifier(AccessModifier.Public);
                classBuilder.AddProperty(propBuilder);
            }

            foreach (var implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
        protected override Task WriteAsync(
            CodeWriter writer,
            InputModelDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder builder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            foreach (InputFieldDescriptor field in descriptor.Fields)
            {
                builder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type)
                    .MakeSettable());
            }

            return(builder.BuildAsync(writer));
        }
示例#7
0
        protected override void Generate(
            CodeWriter writer,
            EntityTypeDescriptor descriptor,
            out string fileName)
        {
            // Setup class
            fileName = EntityTypeNameFromGraphQLTypeName(descriptor.GraphQLTypeName);

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetName(fileName);

            // Add Properties to class
            foreach (KeyValuePair <string, PropertyDescriptor> item in descriptor.Properties)
            {
                PropertyBuilder builder = classBuilder
                                          .AddProperty(item.Value.Name)
                                          .SetName(item.Value.Name)
                                          .SetType(item.Value.Type.ToEntityIdBuilder())
                                          .MakeSettable()
                                          .SetAccessModifier(AccessModifier.Public);

                if (!item.Value.Type.IsNullableType())
                {
                    builder.SetValue("default!");
                }
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#8
0
        protected override void Generate(
            CodeWriter writer,
            EntityTypeDescriptor descriptor,
            out string fileName)
        {
            // Setup class
            fileName = descriptor.RuntimeType.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

            // Add Properties to class
            foreach (KeyValuePair <string, PropertyDescriptor> item in descriptor.Properties)
            {
                classBuilder
                .AddProperty(item.Value.Name)
                .SetType(item.Value.Type.ToEntityIdBuilder())
                .MakeSettable()
                .SetPublic()
                .SetValue(item.Value.Type.IsNullableType() ? null : "default!");
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#9
0
        protected override Task WriteAsync(
            CodeWriter writer,
            OutputModelDescriptor descriptor)
        {
            ClassBuilder builder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            foreach (string typeName in descriptor.Implements)
            {
                builder.AddImplements(typeName);
            }

            foreach (OutputFieldDescriptor field in descriptor.Fields)
            {
                builder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type));
            }

            return(builder.BuildAsync(writer));
        }
示例#10
0
        protected override void Generate(
            CodeWriter writer,
            OperationDescriptor descriptor,
            out string fileName)
        {
            var documentName = CreateDocumentTypeName(descriptor.Name);

            fileName = documentName;

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

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .AddImplements(TypeNames.IDocument)
                                        .SetName(fileName);

            classBuilder
            .AddConstructor()
            .SetPrivate();

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

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

            classBuilder
            .AddProperty("Body")
            .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte))
            .AsLambda(GetByteArray(descriptor.BodyString));

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

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#11
0
        protected override void Generate(
            CodeWriter writer,
            EntityIdFactoryDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.Name;

            var factory = ClassBuilder
                          .New()
                          .SetStatic()
                          .SetAccessModifier(AccessModifier.Public)
                          .SetName(fileName);

            var obj = ParameterBuilder
                      .New()
                      .SetName("obj")
                      .SetType(TypeNames.JsonElement);

            var type = ParameterBuilder
                       .New()
                       .SetName("type")
                       .SetType(TypeNames.String);

            var createEntityId = MethodBuilder
                                 .New()
                                 .SetStatic()
                                 .SetAccessModifier(AccessModifier.Public)
                                 .SetName("CreateEntityId")
                                 .SetReturnType(TypeNames.EntityId)
                                 .AddParameter(obj)
                                 .AddCode(CreateEntityIdBody(descriptor));

            factory.AddMethod(createEntityId);

            foreach (var entity in descriptor.Entities)
            {
                var createSpecificEntityId = MethodBuilder
                                             .New()
                                             .SetStatic()
                                             .SetAccessModifier(AccessModifier.Private)
                                             .SetName($"Create{entity.Name}EntityId")
                                             .SetReturnType(TypeNames.EntityId)
                                             .AddParameter(obj)
                                             .AddParameter(type)
                                             .AddCode(CreateSpecificEntityIdBody(entity));
                factory.AddMethod(createSpecificEntityId);
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(factory)
            .Build(writer);
        }
        protected override Task WriteAsync(
            CodeWriter writer,
            OperationModelDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name)
                .AddImplements($"global::StrawberryShake.IOperation<{descriptor.ResultType}>")
                .AddProperty(PropertyBuilder.New()
                             .SetAccessModifier(AccessModifier.Public)
                             .SetType("string")
                             .SetName("Name")
                             .SetGetter(CodeLineBuilder.New()
                                        .SetLine($"return \"{descriptor.GraphQLName}\";")))
                .AddProperty(PropertyBuilder.New()
                             .SetAccessModifier(AccessModifier.Public)
                             .SetType("global::StrawberryShake.IDocument")
                             .SetName("Document")
                             .SetGetter(CodeLineBuilder.New()
                                        .SetLine($"return {descriptor.DocumentType}.Default;")))
                .AddProperty(PropertyBuilder.New()
                             .SetAccessModifier(AccessModifier.Public)
                             .SetType("global::StrawberryShake.OperationKind")
                             .SetName("Kind")
                             .SetGetter(CodeLineBuilder.New()
                                        .SetLine($"return OperationKind.{descriptor.OperationKind};")))
                .AddProperty(PropertyBuilder.New()
                             .SetAccessModifier(AccessModifier.Public)
                             .SetType("global::System.Type")
                             .SetName("ResultType")
                             .SetGetter(CodeLineBuilder.New()
                                        .SetLine($"return typeof({descriptor.ResultType});")));

            AddConstructor(classBuilder, descriptor.Arguments);
            AddArgumentProperties(classBuilder, descriptor.Arguments);
            AddGetVariableValues(classBuilder, descriptor.Arguments, CodeWriter.Indent);

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
示例#13
0
        protected (ClassBuilder, ConstructorBuilder) CreateClassBuilder(
            bool addConstructorToClass = true)
        {
            var classBuilder       = ClassBuilder.New();
            var constructorBuilder = ConstructorBuilder.New();

            if (addConstructorToClass)
            {
                classBuilder.AddConstructor(constructorBuilder);
            }
            return(classBuilder, constructorBuilder);
        }
        protected override void Generate(
            CodeWriter writer,
            NamedTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = ConstructorBuilder.New()
                                                    .SetTypeName(fileName)
                                                    .SetAccessModifier(AccessModifier.Public);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                var propBuilder = PropertyBuilder
                                  .New()
                                  .SetName(prop.Name)
                                  .SetType(propTypeBuilder)
                                  .SetAccessModifier(AccessModifier.Public);

                if (prop.Type.IsNullableType())
                {
                    propBuilder.SetValue("default!");
                }

                classBuilder.AddProperty(propBuilder);

                // Add initialization of property to the constructor
                var paramName = prop.Name.WithLowerFirstChar();
                ParameterBuilder parameterBuilder = ParameterBuilder.New()
                                                    .SetName(paramName)
                                                    .SetType(propTypeBuilder);
                constructorBuilder.AddParameter(parameterBuilder);
                constructorBuilder.AddCode(prop.Name + " = " + paramName + ";");
            }

            foreach (var implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            classBuilder.AddConstructor(constructorBuilder);

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#15
0
        protected override void Generate(
            CodeWriter writer,
            InputObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            const string serializerResolver = nameof(serializerResolver);
            const string runtimeValue       = nameof(runtimeValue);
            const string value = nameof(value);

            fileName = CreateInputValueFormatter(namedTypeDescriptor);

            NameString typeName = namedTypeDescriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName)
                                        .AddImplements(TypeNames.IInputValueFormatter);

            var neededSerializers = namedTypeDescriptor
                                    .Properties
                                    .GroupBy(x => x.Type.Name)
                                    .ToDictionary(x => x, x => x.First());

            //  Initialize Method

            CodeBlockBuilder initialize = classBuilder
                                          .AddMethod("Initialize")
                                          .SetPublic()
                                          .AddParameter(serializerResolver, x => x.SetType(TypeNames.ISerializerResolver))
                                          .AddBody();

            foreach (var property in neededSerializers.Values)
            {
                if (property.Type.GetName().Value is { } name)
                {
                    var propertyName = GetFieldName(name) + "Formatter";

                    initialize
                    .AddAssigment(propertyName)
                    .AddMethodCall()
                    .SetMethodName(
                        serializerResolver,
                        nameof(ISerializerResolver.GetInputValueFormatter))
                    .AddArgument(name.AsStringToken());

                    classBuilder
                    .AddField(propertyName)
                    .SetAccessModifier(AccessModifier.Private)
                    .SetType(TypeNames.IInputValueFormatter)
                    .SetValue("default!");
                }
        protected override void Generate(
            CodeWriter writer,
            DependencyInjectionDescriptor descriptor,
            out string fileName)
        {
            fileName = ServiceCollectionExtensionsFromClientName(descriptor.Name);

            ClassBuilder factory = ClassBuilder
                                   .New(fileName)
                                   .SetStatic()
                                   .SetAccessModifier(AccessModifier.Public);

            factory
            .AddMethod($"Add{descriptor.Name}")
            .SetPublic()
            .SetStatic()
            .SetReturnType(TypeNames.IServiceCollection)
            .AddParameter(
                "services",
                x => x.SetThis().SetType(TypeNames.IServiceCollection))
            .AddParameter(
                "strategy",
                x => x.SetType(TypeNames.ExecutionStrategy)
                .SetDefault(
                    TypeNames.ExecutionStrategy + "." +
                    nameof(ExecutionStrategy.NetworkOnly)))
            .AddCode(GenerateMethodBody(descriptor));

            factory
            .AddMethod("ConfigureClient")
            .SetPrivate()
            .SetStatic()
            .SetReturnType(TypeNames.IServiceCollection)
            .AddParameter("services", x => x.SetType(TypeNames.IServiceCollection))
            .AddParameter("parentServices", x => x.SetType(TypeNames.IServiceProvider))
            .AddParameter(
                "strategy",
                x => x.SetType(TypeNames.ExecutionStrategy)
                .SetDefault(
                    TypeNames.ExecutionStrategy + "." +
                    nameof(ExecutionStrategy.NetworkOnly)))
            .AddCode(GenerateInternalMethodBody(descriptor));

            factory.AddClass(_clientServiceProvider);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(factory)
            .Build(writer);
        }
示例#17
0
        protected override Task WriteAsync(
            CodeWriter writer,
            DocumentDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name)
                .AddImplements("global::StrawberryShake.IDocument");

            AddArrayProperty(
                classBuilder,
                "HashName",
                "_hashName",
                descriptor.HashAlgorithm,
                CodeWriter.Indent);

            AddArrayProperty(
                classBuilder,
                "Hash",
                "_hash",
                descriptor.Hash,
                CodeWriter.Indent);

            AddArrayProperty(
                classBuilder,
                "Content",
                "_content",
                descriptor.Document,
                CodeWriter.Indent);

            AddToStringMethod(
                classBuilder,
                descriptor.OriginalDocument);

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
        protected override Task WriteAsync(
            CodeWriter writer,
            OutputModelDescriptor descriptor)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name);

            ConstructorBuilder constructorBuilder =
                ConstructorBuilder.New()
                .AddCode(CreateConstructorBody(descriptor));

            classBuilder.AddConstructor(constructorBuilder);

            foreach (var typeName in descriptor.Implements)
            {
                classBuilder.AddImplements(typeName);
            }

            foreach (OutputFieldDescriptor field in descriptor.Fields)
            {
                classBuilder.AddProperty(
                    PropertyBuilder.New()
                    .SetAccessModifier(AccessModifier.Public)
                    .SetName(field.Name)
                    .SetType(field.Type));

                constructorBuilder.AddParameter(
                    ParameterBuilder.New()
                    .SetName(field.ParameterName)
                    .SetType(field.Type));
            }

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
示例#19
0
        protected override void Generate(
            CodeWriter writer,
            EnumDescriptor descriptor,
            out string fileName)
        {
            fileName = NamingConventions.EnumParserNameFromEnumName(descriptor.Name);

            ClassBuilder classBuilder = ClassBuilder
                                        .New(fileName)
                                        .AddImplements(TypeNames.IInputValueFormatter)
                                        .AddImplements(
                TypeNames.ILeafValueParser.WithGeneric(TypeNames.String, descriptor.Name));

            const string serializedValueParamName = "serializedValue";

            classBuilder
            .AddMethod("Parse")
            .AddParameter(ParameterBuilder.New()
                          .SetName(serializedValueParamName)
                          .SetType(TypeNames.String))
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(descriptor.Name)
            .AddCode(CreateEnumParsingSwitch(serializedValueParamName, descriptor));

            const string runtimeValueParamName = "runtimeValue";

            classBuilder
            .AddMethod("Format")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType("object")
            .AddParameter(ParameterBuilder.New()
                          .SetType(TypeNames.Object.MakeNullable())
                          .SetName(runtimeValueParamName))
            .AddCode(CreateEnumFormatingSwitch(runtimeValueParamName, descriptor));

            classBuilder
            .AddProperty("TypeName")
            .AsLambda(descriptor.Name.AsStringToken())
            .SetPublic()
            .SetType(TypeNames.String);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#20
0
        protected override void Generate(
            CodeWriter writer,
            ObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = namedTypeDescriptor.RuntimeType.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName);

            ConstructorBuilder constructorBuilder = ConstructorBuilder.New()
                                                    .SetTypeName(fileName)
                                                    .SetAccessModifier(AccessModifier.Public);

            foreach (var prop in namedTypeDescriptor.Properties)
            {
                var propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                classBuilder.AddProperty(
                    prop.Name,
                    x => x
                    .SetName(prop.Name)
                    .SetType(propTypeBuilder)
                    .SetAccessModifier(AccessModifier.Public)
                    .SetValue(prop.Type.IsNullableType() ? "default!" : null));

                // Add initialization of property to the constructor
                var paramName = GetParameterName(prop.Name);
                constructorBuilder.AddParameter(paramName, x => x.SetType(propTypeBuilder));
                constructorBuilder.AddCode(prop.Name + " = " + paramName + ";");
            }

            foreach (NameString implement in namedTypeDescriptor.Implements)
            {
                classBuilder.AddImplements(implement);
            }

            classBuilder.AddConstructor(constructorBuilder);

            CodeFileBuilder
            .New()
            .SetNamespace(namedTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
        public async Task Class_With_One_Property()
        {
            // arrange
            var sb     = new StringBuilder();
            var writer = new CodeWriter(sb);

            // act
            await ClassBuilder.New()
            .SetName("MyClass")
            .AddProperty(
                PropertyBuilder.New()
                .SetName("Foo")
                .SetType("Bar"))
            .BuildAsync(writer);

            // assert
            sb.ToString().MatchSnapshot();
        }
示例#22
0
        protected override void Generate(
            CodeWriter writer,
            ObjectTypeDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.RuntimeType.Name;
            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

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

            foreach (var prop in descriptor.Properties)
            {
                TypeReferenceBuilder propTypeBuilder = prop.Type.ToBuilder();

                // Add Property to class
                classBuilder
                .AddProperty(prop.Name)
                .SetName(prop.Name)
                .SetType(propTypeBuilder)
                .SetPublic()
                .SetValue(prop.Type.IsNullableType() ? "default!" : null);

                // Add initialization of property to the constructor
                var paramName = GetParameterName(prop.Name);
                constructorBuilder
                .AddParameter(paramName, x => x.SetType(propTypeBuilder))
                .AddCode(AssignmentBuilder
                         .New()
                         .SetLefthandSide(prop.Name)
                         .SetRighthandSide(paramName));
            }

            classBuilder.AddImplementsRange(descriptor.Implements.Select(x => x.Value));

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#23
0
        protected override void Generate(
            CodeWriter writer,
            OperationDescriptor operationDescriptor,
            out string fileName)
        {
            fileName = operationDescriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

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

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

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

            AddInjectedSerializers(operationDescriptor, constructorBuilder, classBuilder);

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

            classBuilder.AddMethod(CreateWatchMethod(operationDescriptor, runtimeTypeName));
            classBuilder.AddMethod(CreateRequestMethod(operationDescriptor));

            AddFormatMethods(operationDescriptor, classBuilder);

            CodeFileBuilder
            .New()
            .SetNamespace(operationDescriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#24
0
        protected override void Generate(
            CodeWriter writer,
            EnumTypeDescriptor descriptor,
            out string fileName)
        {
            const string serializedValue = nameof(serializedValue);
            const string runtimeValue    = nameof(runtimeValue);

            fileName = CreateEnumParserName(descriptor.Name);

            ClassBuilder classBuilder = ClassBuilder
                                        .New(fileName)
                                        .AddImplements(IInputValueFormatter)
                                        .AddImplements(ILeafValueParser.WithGeneric(String, descriptor.Name));

            classBuilder
            .AddMethod("Parse")
            .AddParameter(serializedValue, x => x.SetType(String))
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(descriptor.Name)
            .AddCode(CreateEnumParsingSwitch(serializedValue, descriptor));

            classBuilder
            .AddMethod("Format")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(Object)
            .AddParameter(runtimeValue, x => x.SetType(Object.MakeNullable()))
            .AddCode(CreateEnumFormattingSwitch(runtimeValue, descriptor));

            classBuilder
            .AddProperty("TypeName")
            .AsLambda(descriptor.Name.AsStringToken())
            .SetPublic()
            .SetType(String);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
        protected override void Generate(
            CodeWriter writer,
            EntityIdFactoryDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                .New()
                .SetStatic()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(fileName);

            classBuilder
                .AddMethod("CreateEntityId")
                .SetStatic()
                .SetAccessModifier(AccessModifier.Public)
                .SetReturnType(TypeNames.EntityId)
                .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement))
                .AddCode(CreateEntityIdBody(descriptor));

            foreach (var entity in descriptor.Entities)
            {
                classBuilder
                    .AddMethod($"Create{entity.Name}EntityId")
                    .SetAccessModifier(AccessModifier.Private)
                    .SetStatic()
                    .SetReturnType(TypeNames.EntityId)
                    .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement))
                    .AddParameter(_type, x => x.SetType(TypeNames.String))
                    .AddCode(CreateSpecificEntityIdBody(entity));
            }

            CodeFileBuilder
                .New()
                .SetNamespace(descriptor.Namespace)
                .AddType(classBuilder)
                .Build(writer);
        }
        protected override Task WriteAsync(
            CodeWriter writer,
            ResultParserDescriptor descriptor)
        {
            ClassBuilder classBuilder =
                ClassBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .SetName(descriptor.Name)
                .AddImplements($"{Types.JsonResultParserBase}<{descriptor.ResultType}>");

            AddConstructor(
                classBuilder,
                descriptor.ValueSerializers,
                CodeWriter.Indent);

            foreach (ResultParserMethodDescriptor parserMethod in descriptor.ParseMethods)
            {
                if (parserMethod.IsRoot)
                {
                    AddParseDataMethod(classBuilder, parserMethod, CodeWriter.Indent);
                }
                else
                {
                    AddParseMethod(classBuilder, parserMethod, CodeWriter.Indent);
                }
            }

            foreach (ResultParserDeserializerMethodDescriptor deserializerMethod in
                     descriptor.DeserializerMethods)
            {
                AddDeserializeMethod(classBuilder, deserializerMethod, CodeWriter.Indent);
            }

            return(CodeFileBuilder.New()
                   .SetNamespace(descriptor.Namespace)
                   .AddType(classBuilder)
                   .BuildAsync(writer));
        }
示例#27
0
        protected override void Generate(
            CodeWriter writer,
            InputObjectTypeDescriptor namedTypeDescriptor,
            out string fileName)
        {
            fileName = CreateInputValueFormatter(namedTypeDescriptor);

            NameString   typeName     = namedTypeDescriptor.Name;
            ClassBuilder classBuilder = ClassBuilder.New()
                                        .SetName(fileName)
                                        .AddImplements(TypeNames.IInputValueFormatter);

            var neededSerializers = namedTypeDescriptor.Properties
                                    .ToLookup(x => x.Type.Name)
                                    .Select(x => x.First())
                                    .ToDictionary(x => x.Type.Name);

            //  Initialize Method

            CodeBlockBuilder initialize = classBuilder
                                          .AddMethod("Initialize")
                                          .SetPublic()
                                          .AddParameter("serializerResolver", x => x.SetType(TypeNames.ISerializerResolver))
                                          .AddBody();

            foreach (var property in neededSerializers.Values)
            {
                if (property.Type.GetName().Value is { } name)
                {
                    MethodCallBuilder call = MethodCallBuilder.New()
                                             .SetMethodName("serializerResolver." +
                                                            nameof(ISerializerResolver.GetInputValueFormatter))
                                             .AddArgument(name.AsStringToken() ?? "")
                                             .SetPrefix($"{GetFieldName(name)}Formatter = ");

                    initialize.AddCode(call);
                }
        public async Task Class_With_Field_Constructor_Property_Method()
        {
            // arrange
            var sb     = new StringBuilder();
            var writer = new CodeWriter(sb);

            // act
            await ClassBuilder.New()
            .SetName("MyClass")
            .AddImplements("SomeOtherType")
            .SetAbstract()
            .AddField(FieldBuilder.New()
                      .SetName("_foo")
                      .SetType("Bar")
                      .SetReadOnly())
            .AddProperty(
                PropertyBuilder.New()
                .SetName("Foo")
                .SetType("Bar")
                .SetBackingField("_foo"))
            .AddConstructor(ConstructorBuilder.New()
                            .SetAccessModifier(AccessModifier.Protected)
                            .AddParameter(ParameterBuilder.New()
                                          .SetName("foo")
                                          .SetType("Bar")
                                          .SetDefault())
                            .AddCode("_foo = foo;"))
            .AddMethod(MethodBuilder.New()
                       .SetName("GetFooAsync")
                       .SetReturnType("ValueTask<Bar>")
                       .AddCode("return new ValueTask<Bar>(_foo);"))
            .BuildAsync(writer);

            // assert
            sb.ToString().MatchSnapshot();
        }
示例#29
0
        protected override void Generate(
            CodeWriter writer,
            ClientDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.Name;

            ClassBuilder classBuilder = ClassBuilder
                                        .New()
                                        .SetName(fileName);

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

            foreach (OperationDescriptor operation in descriptor.Operations)
            {
                AddConstructorAssignedField(
                    operation.Name,
                    GetFieldName(operation.Name),
                    classBuilder,
                    constructorBuilder);

                classBuilder
                .AddProperty(operation.Name)
                .SetPublic()
                .SetType(operation.Name)
                .AsLambda(GetFieldName(operation.Name));
            }

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal)
            .AddType(classBuilder)
            .Build(writer);
        }
示例#30
0
        protected override void Generate(
            CodeWriter writer,
            DataTypeDescriptor descriptor,
            out string fileName)
        {
            fileName = descriptor.RuntimeType.Name;
            AbstractTypeBuilder typeBuilder;
            ConstructorBuilder? constructorBuilder = null;

            if (descriptor.IsInterface)
            {
                typeBuilder = InterfaceBuilder
                              .New()
                              .SetName(fileName);

                typeBuilder
                .AddProperty(__typename)
                .SetType(TypeNames.String);
            }
            else
            {
                ClassBuilder classBuilder = ClassBuilder
                                            .New()
                                            .SetName(fileName);

                typeBuilder = classBuilder;

                classBuilder
                .AddProperty(__typename)
                .SetPublic()
                .SetType(TypeNames.String);

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

                constructorBuilder
                .AddParameter(_typename)
                .SetType(TypeNames.String)
                .SetName(_typename);

                constructorBuilder
                .AddCode(
                    AssignmentBuilder
                    .New()
                    .SetLefthandSide(__typename)
                    .SetRighthandSide(_typename)
                    .AssertNonNull());
            }

            // Add Properties to class
            foreach (PropertyDescriptor property in descriptor.Properties)
            {
                TypeReferenceBuilder propertyType = property.Type.Kind switch
                {
                    TypeKind.LeafType => property.Type.ToBuilder(),
                    TypeKind.DataType => property.Type.ToBuilder(property.Type.Name),
                    TypeKind.EntityType => property.Type.ToEntityIdBuilder(),
                    _ => throw new ArgumentOutOfRangeException()
                };

                typeBuilder
                .AddProperty(property.Name)
                .SetType(propertyType)
                .SetPublic();

                var parameterName = GetParameterName(property.Name);

                constructorBuilder?
                .AddParameter(parameterName)
                .SetType(propertyType)
                .SetDefault("null");

                constructorBuilder?
                .AddCode(AssignmentBuilder
                         .New()
                         .SetLefthandSide(property.Name)
                         .SetRighthandSide(parameterName));
            }

            // implement interfaces
            typeBuilder.AddImplementsRange(descriptor.Implements.Select(CreateDataTypeName));

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