示例#1
0
        protected void AddConstructorAssignedField(
            TypeReferenceBuilder type,
            string fieldName,
            ClassBuilder classBuilder,
            ConstructorBuilder constructorBuilder,
            bool skipNullCheck = false)
        {
            var paramName = fieldName.TrimStart('_');

            classBuilder.AddField(
                FieldBuilder
                .New()
                .SetReadOnly()
                .SetName(fieldName)
                .SetType(type));

            var assignment = AssignmentBuilder
                             .New()
                             .SetLefthandSide(fieldName)
                             .SetRighthandSide(paramName);

            if (!skipNullCheck)
            {
                assignment.AssertNonNull();
            }

            constructorBuilder
            .AddCode(assignment)
            .AddParameter(paramName, b => b.SetType(type));
        }
示例#2
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 { })
示例#3
0
        public static FieldBuilder AddField(this ClassBuilder builder, string name)
        {
            FieldBuilder fieldBuilder = FieldBuilder.New().SetName(name);

            builder.AddField(fieldBuilder);
            return(fieldBuilder);
        }
示例#4
0
        private void AddFields(
            IReadOnlyList <ValueSerializerDescriptor> serializerDescriptors,
            ClassBuilder builder)
        {
            builder
            .AddField(FieldBuilder.New()
                      .SetName("_needsInitialization")
                      .SetType("bool")
                      .SetValue("true"));

            foreach (ValueSerializerDescriptor serializer in serializerDescriptors)
            {
                builder
                .AddField(FieldBuilder.New()
                          .SetName(serializer.FieldName)
                          .SetType($"{Types.IValueSerializer}?", NullableRefTypes)
                          .SetType(Types.IValueSerializer, !NullableRefTypes));
            }
        }
示例#5
0
        public static FieldBuilder AddField(this ClassBuilder builder, string?name = null)
        {
            FieldBuilder fieldBuilder = FieldBuilder.New();

            if (name is not null)
            {
                fieldBuilder.SetName(name);
            }

            builder.AddField(fieldBuilder);
            return(fieldBuilder);
        }
示例#6
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!");
                }
示例#7
0
 private static void AddDefaultProperty(
     ClassBuilder classBuilder,
     string name)
 {
     classBuilder
     .AddField(FieldBuilder.New()
               .SetStatic()
               .SetType(name)
               .SetReadOnly()
               .SetName("_default")
               .SetValue($"new {name}();"))
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType(name)
                  .SetBackingField("_default")
                  .SetName("Default"));
 }
示例#8
0
        private static void AddInjectedSerializers(
            OperationDescriptor operationDescriptor,
            ConstructorBuilder constructorBuilder,
            ClassBuilder classBuilder)
        {
            var neededSerializers = operationDescriptor
                                    .Arguments
                                    .GroupBy(x => x.Type.Name)
                                    .ToDictionary(x => x.Key, x => x.First());

            if (!neededSerializers.Any())
            {
                return;
            }

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

            foreach (var property in neededSerializers.Values)
            {
                if (property.Type.GetName().Value is { } name)
                {
                    var fieldName = $"{GetFieldName(name)}Formatter";
                    constructorBuilder
                    .AddCode(
                        AssignmentBuilder
                        .New()
                        .SetLefthandSide(fieldName)
                        .SetRighthandSide(
                            MethodCallBuilder
                            .Inline()
                            .SetMethodName(
                                _serializerResolver,
                                nameof(ISerializerResolver.GetInputValueFormatter))
                            .AddArgument(name.AsStringToken())));

                    classBuilder
                    .AddField()
                    .SetName(fieldName)
                    .SetAccessModifier(AccessModifier.Private)
                    .SetType(TypeNames.IInputValueFormatter)
                    .SetReadOnly();
                }
示例#9
0
 private static void AddArrayProperty(
     ClassBuilder classBuilder,
     string name,
     string fieldName,
     ReadOnlySpan <byte> bytes,
     string indent)
 {
     classBuilder
     .AddField(FieldBuilder.New()
               .SetType("byte[]")
               .SetReadOnly()
               .SetName(fieldName)
               .SetValue(CreateNewByteArray(bytes, indent)))
     .AddProperty(PropertyBuilder.New()
                  .SetAccessModifier(AccessModifier.Public)
                  .SetType("global::System.ReadOnlySpan<byte>")
                  .SetBackingField(fieldName)
                  .SetName(name));
 }
        private void AddConstructor(
            ClassBuilder classBuilder,
            IReadOnlyList <ValueSerializerDescriptor> serializers,
            string indent)
        {
            foreach (ValueSerializerDescriptor serializer in serializers)
            {
                classBuilder.AddField(
                    FieldBuilder.New()
                    .SetType(Types.IValueSerializer)
                    .SetName(serializer.FieldName));
            }

            classBuilder.AddConstructor(
                ConstructorBuilder.New()
                .SetAccessModifier(AccessModifier.Public)
                .AddParameter(ParameterBuilder.New()
                              .SetType(Types.IValueSerializerCollection)
                              .SetName("serializerResolver"))
                .AddCode(CreateConstructorBody(serializers, indent)));
        }
        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);
        }