Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
0
        public static ClassBuilder AddEquality(
            this ClassBuilder builder,
            string typeName,
            IReadOnlyList <PropertyDescriptor> properties)
        {
            builder.AddImplements(TypeNames.IEquatable.WithGeneric(typeName));
            builder.AddMethod(BuildEqualsMethod(typeName, properties));
            builder.AddMethod(BuildObjectEqualsMethod(typeName));
            builder.AddMethod(BuildGetHashCodeMethod(properties));

            return(builder);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public static ClassBuilder AddEquality(
            this ClassBuilder builder,
            string typeName,
            IReadOnlyList <PropertyDescriptor> properties)
        {
            const string obj   = nameof(obj);
            const string other = nameof(other);

            builder.AddImplements(TypeNames.IEquatable.WithGeneric(typeName));

            builder
            .AddMethod(nameof(IEquatable <object> .Equals))
            .SetPublic()
            .SetOverride()
            .SetReturnType(TypeNames.Boolean)
            .AddParameter(obj, x => x.SetType(TypeNames.Object.MakeNullable()))
            .AddCode(CodeBlockBuilder
                     .New()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition(MethodCallBuilder
                                            .Inline()
                                            .SetMethodName(nameof(ReferenceEquals))
                                            .AddArgument("null")
                                            .AddArgument(obj))
                              .AddCode("return false;"))
                     .AddEmptyLine()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition(MethodCallBuilder
                                            .Inline()
                                            .SetMethodName(nameof(ReferenceEquals))
                                            .AddArgument("this")
                                            .AddArgument(obj))
                              .AddCode("return true;"))
                     .AddEmptyLine()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition($"{obj}.GetType() != GetType()")
                              .AddCode("return false;"))
                     .AddEmptyLine()
                     .AddLine($"return Equals(({typeName}){obj});"));

            ConditionBuilder equalCondition =
                ConditionBuilder
                .New()
                .SetReturn()
                .SetDetermineStatement();

            if (properties.Count == 0)
            {
                equalCondition.And("true");
            }
            else
            {
                foreach (PropertyDescriptor property in properties)
                {
                    equalCondition.And(ConditionBuilder
                                       .New()
                                       .Set(BuildPropertyComparison(property.Type, property.Name)));
                }
            }

            builder
            .AddMethod(nameof(IEquatable <object> .Equals))
            .SetPublic()
            .SetReturnType(TypeNames.Boolean)
            .AddParameter(other, x => x.SetType(typeName.MakeNullable()))
            .AddCode(CodeBlockBuilder
                     .New()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition(MethodCallBuilder
                                            .Inline()
                                            .SetMethodName(nameof(ReferenceEquals))
                                            .AddArgument("null")
                                            .AddArgument(other))
                              .AddCode("return false;"))
                     .AddEmptyLine()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition(MethodCallBuilder
                                            .Inline()
                                            .SetMethodName(nameof(ReferenceEquals))
                                            .AddArgument("this")
                                            .AddArgument(other))
                              .AddCode("return true;"))
                     .AddEmptyLine()
                     .AddCode(IfBuilder
                              .New()
                              .SetCondition($"{other}.GetType() != GetType()")
                              .AddCode("return false;"))
                     .AddEmptyLine()
                     .AddCode(equalCondition));

            builder
            .AddMethod(nameof(GetHashCode))
            .SetPublic()
            .SetOverride()
            .SetReturnType(TypeNames.Int32)
            .AddCode(HashCodeBuilder
                     .New()
                     .AddProperties(properties));

            return(builder);
        }
        protected override void Generate(
            CodeWriter writer,
            ResultBuilderDescriptor resultBuilderDescriptor,
            out string fileName)
        {
            InterfaceTypeDescriptor resultTypeDescriptor =
                resultBuilderDescriptor.ResultNamedType as InterfaceTypeDescriptor
                ?? throw new InvalidOperationException(
                          "A result type can only be generated for complex types");

            fileName = resultBuilderDescriptor.RuntimeType.Name;

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

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

            classBuilder
            .AddImplements(
                TypeNames.IOperationResultBuilder.WithGeneric(
                    TypeNames.JsonDocument,
                    resultTypeDescriptor.RuntimeType.Name));

            AddConstructorAssignedField(
                TypeNames.IEntityStore,
                _entityStore,
                classBuilder,
                constructorBuilder);

            AddConstructorAssignedField(
                TypeNames.Func.WithGeneric(TypeNames.JsonElement, TypeNames.EntityId),
                _extractId,
                classBuilder,
                constructorBuilder);

            AddConstructorAssignedField(
                TypeNames.IOperationResultDataFactory
                .WithGeneric(resultTypeDescriptor.RuntimeType.Name),
                _resultDataFactory,
                classBuilder,
                constructorBuilder);

            constructorBuilder
            .AddParameter(_serializerResolver)
            .SetType(TypeNames.ISerializerResolver);

            IEnumerable <ValueParserDescriptor> valueParsers = resultBuilderDescriptor
                                                               .ValueParsers
                                                               .GroupBy(t => t.Name)
                                                               .Select(t => t.First());

            foreach (ValueParserDescriptor valueParser in valueParsers)
            {
                var parserFieldName = $"{GetFieldName(valueParser.Name)}Parser";

                classBuilder
                .AddField(parserFieldName)
                .SetReadOnly()
                .SetType(
                    TypeNames.ILeafValueParser
                    .WithGeneric(valueParser.SerializedType, valueParser.RuntimeType));

                MethodCallBuilder getLeaveValueParser = MethodCallBuilder
                                                        .Inline()
                                                        .SetMethodName(
                    _serializerResolver,
                    nameof(ISerializerResolver.GetLeafValueParser))
                                                        .AddGeneric(valueParser.SerializedType.ToString())
                                                        .AddGeneric(valueParser.RuntimeType.ToString())
                                                        .AddArgument(valueParser.Name.AsStringToken());

                constructorBuilder.AddCode(
                    AssignmentBuilder
                    .New()
                    .SetAssertNonNull()
                    .SetAssertException(
                        ExceptionBuilder
                        .Inline(TypeNames.ArgumentException)
                        .AddArgument(
                            $"\"No serializer for type `{valueParser.Name}` found.\""))
                    .SetLefthandSide(parserFieldName)
                    .SetRighthandSide(getLeaveValueParser));
            }

            AddBuildMethod(resultTypeDescriptor, classBuilder);

            AddBuildDataMethod(resultTypeDescriptor, classBuilder);

            var processed = new HashSet <string>();

            AddRequiredDeserializeMethods(resultTypeDescriptor, classBuilder, processed);

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