Exemplo n.º 1
0
        private Task <SyntaxList <MemberDeclarationSyntax> > GenerateForClass(MemberDeclarationSyntax applyTo, SemanticModel semanticModel, ClassDeclarationSyntax applyToClass)
        {
            var generator = new DefaultDiscriminatedUnionGenerator(_caseFactoryPrefix, _preventNullValues);
            var applyToClassSymbolInfo = semanticModel.GetDeclaredSymbol(applyTo) as INamedTypeSymbol;

            if (applyToClassSymbolInfo == null)
            {
                throw new InvalidOperationException($"applyTo symbol has not been found");
            }
            var applyToClassType = GetTypeSyntax(applyToClass, applyToClassSymbolInfo);
            var partialClass     = GetPartialClass(applyToClass, applyToClassType);
            var cases            = GetCases(applyToClass, applyToClassSymbolInfo, semanticModel);

            var context = new DiscriminatedUnionContext <DiscriminatedUnionCase>(
                applyToClass,
                applyToClassType,
                partialClass,
                semanticModel,
                applyToClassSymbolInfo,
                cases);
            var result      = generator.Build(context);
            var resultClass = BuildClass(result);
            var classes     = List(new[] { (MemberDeclarationSyntax)resultClass });

            return(Task.FromResult(classes));
        }
Exemplo n.º 2
0
        private Task <SyntaxList <MemberDeclarationSyntax> > GenerateForStructs(SemanticModel semanticModel, StructDeclarationSyntax applyToStruct)
        {
            var structGenerator         = new StructDiscriminatedUnionGenerator(_caseFactoryPrefix, _preventNullValues);
            var applyToStructSymbolInfo = semanticModel.GetDeclaredSymbol(applyToStruct) as INamedTypeSymbol;

            if (applyToStructSymbolInfo == null)
            {
                throw new InvalidOperationException($"applyTo symbol has not been found");
            }
            var applyToStructType = GetTypeSyntax(applyToStruct, applyToStructSymbolInfo);
            var structCases       = GetStructCases(applyToStruct, semanticModel);
            var partialStruct     = GetPartialStruct(applyToStruct, applyToStructType);
            var structContext     = new DiscriminatedUnionContext <StructDiscriminatedUnionCase>(
                applyToStruct,
                applyToStructType,
                partialStruct,
                semanticModel,
                applyToStructSymbolInfo,
                structCases
                );
            var structResult = structGenerator.Build(structContext);
            var resultStruct = BuildStruct(structResult);
            var structs      = List(new[] { (MemberDeclarationSyntax)resultStruct });

            return(Task.FromResult(structs));
        }
Exemplo n.º 3
0
        private TypeDeclarationSyntax BuildStruct(DiscriminatedUnionContext <StructDiscriminatedUnionCase> context)
        {
            var casesClass = CreateEmptyCasesPartialClass();

            casesClass = casesClass.WithMembers(List(context.Cases.SelectMany(c => c.Members)));
            var members = context.Members.Add(casesClass);

            return(context.WithMembers(List(members)).GeneratedPartialClass);
        }
Exemplo n.º 4
0
        private TypeDeclarationSyntax BuildClass(DiscriminatedUnionContext <DiscriminatedUnionCase> context)
        {
            var casesPartialClass = CreateEmptyCasesPartialClass();

            return(context.WithMembers(
                       List(
                           context.Members.Add(
                               casesPartialClass.WithMembers(
                                   List(
                                       context.Cases
                                       .Select(c => c.GeneratedPartialClass.WithMembers(List(c.Members)))
                                       .Cast <MemberDeclarationSyntax>()
                                       )
                                   )
                               )
                           )
                       )
                   .GeneratedPartialClass);
        }
 public DiscriminatedUnionContext <T> Build(DiscriminatedUnionContext <T> context)
 {
     return(_innerGenerators.Aggregate(context, (c, g) => g.Build(c)));
 }