public NamespaceBuilderResult BuildNamespacesForCalculations(IEnumerable <Calculation> calculations)
        {
            NamespaceBuilderResult result = new NamespaceBuilderResult();

            IEnumerable <IGrouping <string, Calculation> > fundingStreamCalculationGroups
                = calculations.Where(_ => _.Current.Namespace == CalculationNamespace.Template)
                  .GroupBy(_ => _.FundingStreamId)
                  .ToArray();
            IEnumerable <string>      fundingStreamNamespaces = fundingStreamCalculationGroups.Select(_ => _.Key).ToArray();
            IEnumerable <Calculation> additionalCalculations  = calculations.Where(_ => _.Current.Namespace == CalculationNamespace.Additional)
                                                                .ToArray();

            IEnumerable <string> propertyAssignments = CreatePropertyAssignments(fundingStreamNamespaces);
            IEnumerable <(StatementSyntax Syntax, bool IsFundingLines, string Namespace)> propertyDefinitions = CreateProperties(fundingStreamNamespaces);

            result.PropertiesDefinitions = propertyDefinitions.Where(_ => !_.IsFundingLines).Select(_ => _.Syntax).ToArray();

            result.EnumsDefinitions = CreateEnums(calculations);

            foreach (IGrouping <string, Calculation> fundingStreamCalculationGroup in fundingStreamCalculationGroups)
            {
                result.InnerClasses.Add(CreateNamespaceDefinition(_typeIdentifierGenerator.GenerateIdentifier(fundingStreamCalculationGroup.Key),
                                                                  fundingStreamCalculationGroup,
                                                                  propertyDefinitions.Where(_ => _.Namespace == fundingStreamCalculationGroup.Key || string.IsNullOrWhiteSpace(_.Namespace)).Select(_ => _.Syntax),
                                                                  propertyAssignments));
            }

            result.InnerClasses.Add(CreateNamespaceDefinition("Calculations",
                                                              additionalCalculations,
                                                              propertyDefinitions.Where(_ => !_.IsFundingLines).Select(_ => _.Syntax),
                                                              propertyAssignments,
                                                              "AdditionalCalculations"));

            return(result);
        }
Esempio n. 2
0
        public NamespaceBuilderResult BuildNamespacesForFundingLines(IDictionary <string, Funding> funding,
                                                                     int decimalPlaces = 2)
        {
            NamespaceBuilderResult result = new NamespaceBuilderResult
            {
                PropertiesDefinitions = Array.Empty <StatementSyntax>(),
                EnumsDefinitions      = Array.Empty <StatementSyntax>()
            };

            foreach (string @namespace in funding.Keys)
            {
                ClassBlockSyntax @class = SyntaxFactory.ClassBlock(
                    SyntaxFactory.ClassStatement(
                        _typeIdentifierGenerator.GenerateIdentifier($"{@namespace}FundingLines")
                        )
                    .WithModifiers(
                        SyntaxFactory.TokenList(
                            SyntaxFactory.Token(SyntaxKind.PublicKeyword))),
                    new SyntaxList <InheritsStatementSyntax>(),
                    new SyntaxList <ImplementsStatementSyntax>(),
                    SyntaxFactory.List(CreateFundingLineClass(funding[@namespace].FundingLines.DistinctBy(_ => _.Id), @namespace, decimalPlaces)),
                    SyntaxFactory.EndClassStatement()
                    );

                result.InnerClasses.Add(new NamespaceClassDefinition(_typeIdentifierGenerator.GenerateIdentifier(@namespace), @class, "FundingLines", "FundingLines"));
            }

            return(result);
        }