private void EmitInterface(TransportModelInterface model, ICodeStream output) { var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(model, ModelType.Transport)}.cs"); var unit = CompilationUnit(); unit = unit.AddUsings( UsingDirective(ParseName(nameof(System)))); var entityInterface = InterfaceDeclaration(CsEmitterHelper.GetCSharpModelShortName(model, ModelType.Transport)); entityInterface = entityInterface.AddModifiers(Token(SyntaxKind.PublicKeyword)); if (model.GenericParameters.Count > 0) { entityInterface = entityInterface.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(model.GenericParameters.Select(p => TypeParameter(p.Key))))); entityInterface = entityInterface.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>( model.GenericParameters.Select(p => TypeParameterConstraintClause( IdentifierName(p.Key), SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Transport))) }))))); } List <string> baseTypes = new List <string>(); foreach (var interfaceModel in model.Interfaces) { baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Transport)); } if (baseTypes.Count > 0) { entityInterface = entityInterface.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t)))))); } entityInterface = entityInterface.WithMembers(List <MemberDeclarationSyntax>(model.Members.Select(m => PropertyDeclaration(ParseTypeName(CsEmitterHelper.GetPropertyTypeName(m.Value, this.settings, ModelType.Transport)), NameHelper.GetSafeVariableName(m.Value.Name)) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.List <AccessorDeclarationSyntax>(new AccessorDeclarationSyntax[] { SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)), SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) }))) ))); var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsTransportModelNamespace)); nsContainer = nsContainer.AddMembers(entityInterface); unit = unit.AddMembers(nsContainer); outputFile.SyntaxTree = unit.SyntaxTree; }
private void EmitEntity(TransportModelEntity entityModel, ICodeStream output) { var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport)}.cs"); var unit = CompilationUnit(); unit = unit.AddUsings( UsingDirective(ParseName(nameof(System)))); var entityClass = ClassDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport)); entityClass = entityClass.AddModifiers(Token(SyntaxKind.PublicKeyword)); if (entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind kindDiscriminant) { entityClass = entityClass.AddMembers( ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Transport)) .AddModifiers(Token(SyntaxKind.PublicKeyword)) .WithBody(Block().AddStatements( ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("kind")), MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName($"{this.settings.CsTransportModelNamespace}.SyntaxKind"), IdentifierName(kindDiscriminant.SyntaxKindValueName))))))); } if (entityModel.GenericParameters.Count > 0) { entityClass = entityClass.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(entityModel.GenericParameters.Select(p => TypeParameter(p.Key))))); entityClass = entityClass.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>( entityModel.GenericParameters.Select(p => TypeParameterConstraintClause( IdentifierName(p.Key), SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Transport))) }))))); } List <string> baseTypes = new List <string>(); if (entityModel.BaseEntity != null) { baseTypes.Add(CsEmitterHelper.GetCSharpModelReferenceName(entityModel.BaseEntity, this.settings, ModelType.Transport)); } foreach (var interfaceModel in entityModel.Interfaces) { baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Transport)); } if (baseTypes.Count > 0) { entityClass = entityClass.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t)))))); } entityClass = entityClass.AddMembers(entityModel.Members.Select(m => PropertyDeclaration(ParseTypeName(CsEmitterHelper.GetPropertyTypeName(m.Value, this.settings, ModelType.Transport)), NameHelper.GetSafeVariableName(m.Value.Name)) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.List <AccessorDeclarationSyntax>(new AccessorDeclarationSyntax[] { SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)), SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) }))) .AddModifiers(Token(SyntaxKind.PublicKeyword)) ).ToArray <MemberDeclarationSyntax>()); var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsTransportModelNamespace)); nsContainer = nsContainer.AddMembers(entityClass); unit = unit.AddMembers(nsContainer); outputFile.SyntaxTree = unit.SyntaxTree; }
private void EmitEntity(TransportModelEntity entityModel, ICodeStream output) { var outputFile = (CodeFileCSharp)output.CreateCodeFile($"{CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast)}.cs"); var unit = CompilationUnit(); unit = unit.AddUsings( UsingDirective(ParseName(nameof(System)))); var entityClass = ClassDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast)); entityClass = entityClass.AddModifiers(Token(SyntaxKind.PublicKeyword)); if (entityModel.TsDiscriminant == null || !(entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind)) { entityClass = entityClass.AddModifiers(Token(SyntaxKind.AbstractKeyword)); } List <StatementSyntax> propertyInitializers = null; List <ArgumentSyntax> baseConstructorArguments = null; var constructorDeclaration = ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast)) .AddModifiers(Token(SyntaxKind.PublicKeyword)) .WithParameterList(ParameterList(SeparatedList <ParameterSyntax>(this.GetConstructorParameters(entityModel, out propertyInitializers, out baseConstructorArguments)))); if (baseConstructorArguments != null && baseConstructorArguments.Count > 0) { constructorDeclaration = constructorDeclaration.WithInitializer( ConstructorInitializer(SyntaxKind.BaseConstructorInitializer) .AddArgumentListArguments(baseConstructorArguments.ToArray())); } var constructorBody = Block(); constructorBody = this.PopulateRequiredConstructorBodyInitializers(constructorBody, entityModel); if (propertyInitializers != null && propertyInitializers.Count > 0) { constructorBody = constructorBody.AddStatements(propertyInitializers.ToArray()); } constructorDeclaration = constructorDeclaration.WithBody(constructorBody); entityClass = entityClass.AddMembers(constructorDeclaration); if (constructorDeclaration.ParameterList.Parameters.Count > 0) { var parameterlessConstructorDeclaration = ConstructorDeclaration(CsEmitterHelper.GetCSharpModelShortName(entityModel, ModelType.Ast)) .AddModifiers(Token(SyntaxKind.PublicKeyword)); var parameterlessConstructorBody = Block(); parameterlessConstructorBody = this.PopulateRequiredConstructorBodyInitializers(parameterlessConstructorBody, entityModel); parameterlessConstructorDeclaration = parameterlessConstructorDeclaration.WithBody(parameterlessConstructorBody); entityClass = entityClass.AddMembers(parameterlessConstructorDeclaration); } if (entityModel.GenericParameters.Count > 0) { entityClass = entityClass.WithTypeParameterList(TypeParameterList(SeparatedList <TypeParameterSyntax>(entityModel.GenericParameters.Select(p => TypeParameter(p.Key))))); entityClass = entityClass.WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>( entityModel.GenericParameters.Select(p => TypeParameterConstraintClause( IdentifierName(p.Key), SeparatedList <TypeParameterConstraintSyntax>(new[] { TypeConstraint(ParseTypeName(CsEmitterHelper.GetModelGenericParameterConstraintTypeName(p.Value, ModelType.Ast))) }))))); } List <string> baseTypes = new List <string>(); if (entityModel.Name == this.settings.AstNodeBaseTypeQualified.Split('.').Last()) { baseTypes.Add($"{settings.CsAstModelNamespace}.{baseNodeType}"); } if (entityModel.BaseEntity != null) { baseTypes.Add(CsEmitterHelper.GetCSharpModelReferenceName(entityModel.BaseEntity, this.settings, ModelType.Ast)); } foreach (var interfaceModel in entityModel.Interfaces) { baseTypes.Add(CsEmitterHelper.GetCSharpModelFullyQualifiedName(interfaceModel, this.settings, ModelType.Ast)); } if (baseTypes.Count > 0) { entityClass = entityClass.WithBaseList(BaseList(SeparatedList <BaseTypeSyntax>(baseTypes.Select(t => SimpleBaseType(ParseTypeName(t)))))); } entityClass = entityClass.AddMembers( entityModel .Members .Where(m => !CsEmitterHelper.IsNodeCollection(m.Value.Type)) .Select(m => FieldDeclaration(VariableDeclaration(ParseTypeName(CsEmitterHelper.GetAstModelPropertyTypeName(m.Value, this.settings)), SeparatedList <VariableDeclaratorSyntax>(new[] { VariableDeclarator(this.GetFieldName(m.Key)) }))) ).ToArray <MemberDeclarationSyntax>()); entityClass = entityClass.AddMembers(entityModel.Members.Select(m => this.CreatePropertyDeclaration(m.Value)).ToArray <MemberDeclarationSyntax>()); if (entityModel.TsDiscriminant is TransportModelEntityTsDiscriminantSyntaxKind) { entityClass = entityClass.AddMembers(this.GenerateConversionMethod(entityModel)); } var nsContainer = NamespaceDeclaration(ParseName(this.settings.CsAstModelNamespace)); nsContainer = nsContainer.AddMembers(entityClass); unit = unit.AddMembers(nsContainer); outputFile.SyntaxTree = unit.SyntaxTree; }