private static MemberDeclarationSyntax CreateClass(string indexName, IndexDefinition indexDefinition)
        {
            var safeName = GetCSharpSafeName(indexName);

            //Create a class
            var @class = RoslynHelper.PublicClass(safeName)
                         .WithBaseClass <AbstractIndexCreationTask>();

            // Create a IndexName get property
            PropertyDeclarationSyntax indexNameProperty =
                PropertyDeclaration(ParseTypeName("string"), Identifier(IndexName))
                .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword))
                .AddAccessorListAccessors(
                    AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, Block(
                                            List(new[] { ReturnStatement(IdentifierName($"\"{indexName}\"")) }
                                                 ))));

            // Add the property to the class
            @class = @class.AddMembers(indexNameProperty);

            //Create CreateIndexDefinition method
            MethodDeclarationSyntax indexDefinitionMethod =
                MethodDeclaration(ParseTypeName(typeof(IndexDefinition).Name), Identifier(CreateIndexDefinition))
                .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword))
                .WithBody(Block(SingletonList <StatementSyntax>(
                                    ReturnStatement(ObjectCreationExpression(IdentifierName(typeof(IndexDefinition).Name))
                                                    .WithInitializer(InitializerExpression(SyntaxKind.ObjectInitializerExpression,
                                                                                           SeparatedList <ExpressionSyntax>(CreateStatements(indexDefinition))))))));

            // Add the Method to the class
            @class = @class.AddMembers(indexDefinitionMethod);

            return(@class);
        }
Пример #2
0
        private static MemberDeclarationSyntax CreateClass(string name, IndexDefinition definition)
        {
            var statements          = new List <StatementSyntax>();
            var maps                = definition.Maps.ToList();
            var fieldNamesValidator = new FieldNamesValidator();
            var methodDetector      = new MethodDetectorRewriter();
            var members             = new SyntaxList <MemberDeclarationSyntax>();

            for (var i = 0; i < maps.Count; i++)
            {
                var map = maps[i];
                statements.AddRange(HandleMap(map, fieldNamesValidator, methodDetector, ref members));
            }

            if (string.IsNullOrWhiteSpace(definition.Reduce) == false)
            {
                statements.Add(HandleReduce(definition.Reduce, fieldNamesValidator, methodDetector, out string[] groupByFields));

                var groupByFieldsArray = GetArrayCreationExpression(groupByFields);
                statements.Add(RoslynHelper.This(nameof(StaticIndexBase.GroupByFields)).Assign(groupByFieldsArray).AsExpressionStatement());
            }

            var fields = GetIndexedFields(definition, fieldNamesValidator);

            var outputFieldsArray = GetArrayCreationExpression(fields);

            statements.Add(RoslynHelper.This(nameof(StaticIndexBase.OutputFields)).Assign(outputFieldsArray).AsExpressionStatement());

            var methods = methodDetector.Methods;

            if (methods.HasCreateField)
            {
                statements.Add(RoslynHelper.This(nameof(StaticIndexBase.HasDynamicFields)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            if (methods.HasBoost)
            {
                statements.Add(RoslynHelper.This(nameof(StaticIndexBase.HasBoostedFields)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            var ctor = RoslynHelper.PublicCtor(name)
                       .AddBodyStatements(statements.ToArray());


            return(RoslynHelper.PublicClass(name)
                   .WithBaseClass <StaticIndexBase>()
                   .WithMembers(members.Add(ctor)));
        }
        private static MemberDeclarationSyntax CreateClass(string name, TransformerDefinition definition)
        {
            var statements = new List <StatementSyntax>();

            var methodDetector = new MethodDetectorRewriter();

            statements.Add(HandleTransformResults(definition.TransformResults, methodDetector));

            var methods = methodDetector.Methods;

            if (methods.HasGroupBy)
            {
                statements.Add(RoslynHelper.This(nameof(TransformerBase.HasGroupBy)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            if (methods.HasLoadDocument)
            {
                statements.Add(RoslynHelper.This(nameof(TransformerBase.HasLoadDocument)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            if (methods.HasTransformWith)
            {
                statements.Add(RoslynHelper.This(nameof(TransformerBase.HasTransformWith)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            if (methods.HasInclude)
            {
                statements.Add(RoslynHelper.This(nameof(TransformerBase.HasInclude)).Assign(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)).AsExpressionStatement());
            }

            var ctor = RoslynHelper.PublicCtor(name).AddBodyStatements(statements.ToArray());

            return(RoslynHelper.PublicClass(name)
                   .WithBaseClass <TransformerBase>()
                   .WithMembers(SyntaxFactory.SingletonList <MemberDeclarationSyntax>(ctor)));
        }