Example #1
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)));
        }