private static void GenerateObjectType( CodeGenerationResult result, string @namespace, IObjectType objectType) { TypeNameDirective?typeNameDirective = objectType.GetFirstDirective <TypeNameDirective>("typeName"); var typeName = typeNameDirective?.Name ?? objectType.Name.Value; ClassDeclarationSyntax modelDeclaration = ClassDeclaration(typeName) .AddModifiers( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.PartialKeyword)) .AddGeneratedAttribute(); foreach (IObjectField field in objectType.Fields.Where(t => !t.IsIntrospectionField)) { RelationshipDirective?relationship = field.GetFirstDirective <RelationshipDirective>("relationship"); modelDeclaration = modelDeclaration.AddProperty( field.GetPropertyName(), IdentifierName(field.GetTypeName(@namespace)), field.Description, setable: true, configure: p => { if (relationship is not null) { p = p.AddNeo4JRelationshipAttribute( relationship.Name, relationship.Direction); } return(p); }); } NamespaceDeclarationSyntax namespaceDeclaration = NamespaceDeclaration(IdentifierName(@namespace)) .AddMembers(modelDeclaration); CompilationUnitSyntax compilationUnit = CompilationUnit() .AddMembers(namespaceDeclaration); compilationUnit = compilationUnit.NormalizeWhitespace(elasticTrivia: true); result.AddSource(@namespace + $".{typeName}.cs", compilationUnit.ToFullString()); }
public static ClassDeclarationSyntax AddPropertyIfNotExists(this ClassDeclarationSyntax node, string typeName, string identifier, Func <PropertyDeclarationSyntax, PropertyDeclarationSyntax> modify = null) { var stateProperty = node.DescendantNodes().OfType <PropertyDeclarationSyntax>() .FirstOrDefault(p => p.Type.ToString() == typeName && p.Name() == identifier); if (stateProperty == null) { node = node.AddProperty(typeName, identifier, modify); } return(node); }