-
Notifications
You must be signed in to change notification settings - Fork 186
Adding C# to CLI type generation #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Fellmonkey
wants to merge
4
commits into
appwrite:master
Choose a base branch
from
Fellmonkey:sharp-support-type-gen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−1
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
templates/cli/lib/type-generation/languages/csharp.js.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/** @typedef {import('../attribute').Attribute} Attribute */ | ||
const { AttributeType } = require('../attribute'); | ||
const { LanguageMeta } = require("./language"); | ||
|
||
class CSharp extends LanguageMeta { | ||
getType(attribute, collections) { | ||
let type = ""; | ||
switch (attribute.type) { | ||
case AttributeType.STRING: | ||
case AttributeType.EMAIL: | ||
case AttributeType.DATETIME: | ||
type = "string"; | ||
if (attribute.format === AttributeType.ENUM) { | ||
type = LanguageMeta.toPascalCase(attribute.key); | ||
} | ||
break; | ||
case AttributeType.INTEGER: | ||
type = "int"; | ||
break; | ||
case AttributeType.FLOAT: | ||
type = "double"; | ||
break; | ||
case AttributeType.BOOLEAN: | ||
type = "bool"; | ||
break; | ||
case AttributeType.RELATIONSHIP: | ||
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection); | ||
if (!relatedCollection) { | ||
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`); | ||
} | ||
type = LanguageMeta.toPascalCase(relatedCollection.name); | ||
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { | ||
type = `List<${type}>`; | ||
} | ||
break; | ||
default: | ||
throw new Error(`Unknown attribute type: ${attribute.type}`); | ||
} | ||
if (attribute.array) { | ||
type = `List<${type}>`; | ||
} | ||
if (!attribute.required) { | ||
type += "?"; | ||
} | ||
return type; | ||
} | ||
|
||
getTemplate() { | ||
return `/// This file is auto-generated by the Appwrite CLI. | ||
/// You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`. | ||
|
||
#nullable enable | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
<% for (const attribute of collection.attributes) { -%> | ||
<% if (attribute.type === 'relationship') { -%> | ||
using Appwrite.Models; | ||
<% } -%> | ||
<% } %> | ||
namespace Appwrite.Models | ||
{ | ||
<% for (const attribute of collection.attributes) { -%> | ||
<% if (attribute.format === 'enum') { -%> | ||
|
||
public enum <%- toPascalCase(attribute.key) %> { | ||
<% for (const [index, element] of Object.entries(attribute.elements) ) { -%> | ||
[JsonPropertyName("<%- element %>")] | ||
<%- toPascalCase(element) %><% if (index < attribute.elements.length - 1) { %>,<% } %> | ||
<% } -%> | ||
} | ||
<% } -%> | ||
<% } %> | ||
public class <%= toPascalCase(collection.name) %> | ||
{ | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
[JsonPropertyName("<%- attribute.key %>")] | ||
public <%- getType(attribute, collections) %> <%= toPascalCase(attribute.key) %> { get; private set; } | ||
|
||
<% } -%> | ||
|
||
public <%= toPascalCase(collection.name) %>( | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<%- getType(attribute, collections) %> <%= toCamelCase(attribute.key) %><% if (index < collection.attributes.length - 1) { %>,<% } %> | ||
<% } -%> | ||
) | ||
{ | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<%= toPascalCase(attribute.key) %> = <%= toCamelCase(attribute.key) %>; | ||
<% } -%> | ||
} | ||
|
||
public static <%= toPascalCase(collection.name) %> From(Dictionary<string, object> map) | ||
{ | ||
return new <%= toPascalCase(collection.name) %>( | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<%- toCamelCase(attribute.key) %>:<% | ||
// ENUM | ||
if (attribute.format === 'enum') { | ||
if (attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), e.ToString())).ToList()<% | ||
} else { | ||
if (attribute.required) { | ||
-%> (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), map["<%- attribute.key %>"].ToString())<% | ||
} else { | ||
-%> map["<%- attribute.key %>"] != null ? (<%- toPascalCase(attribute.key) %>)Enum.Parse(typeof(<%- toPascalCase(attribute.key) %>), map["<%- attribute.key %>"].ToString()) : null<% | ||
} | ||
} | ||
// RELATIONSHIP | ||
} else if (attribute.type === 'relationship') { | ||
const relatedClass = toPascalCase(collections.find(c => c.$id === attribute.relatedCollection).name); | ||
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany' || attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Models.<%- relatedClass %>.From((Dictionary<string, object>)e)).ToList()<% | ||
} else { | ||
if (attribute.required) { | ||
-%> <%- relatedClass %>.From((Dictionary<string, object>)map["<%- attribute.key %>"])<% | ||
} else { | ||
-%> map["<%- attribute.key %>"] != null ? <%- relatedClass %>.From((Dictionary<string, object>)map["<%- attribute.key %>"]) : null<% | ||
} | ||
} | ||
// INTEGER | ||
} else if (attribute.type === 'integer') { | ||
if (attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToInt32(e)).ToList()<% | ||
} else { | ||
-%> map["<%- attribute.key %>"] != null ? Convert.ToInt32(map["<%- attribute.key %>"]) : (<%- attribute.required ? '0' : 'null' %>)<% | ||
Fellmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// FLOAT | ||
} else if (attribute.type === 'float') { | ||
if (attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToDouble(e)).ToList()<% | ||
} else { | ||
-%> map["<%- attribute.key %>"] != null ? Convert.ToDouble(map["<%- attribute.key %>"]) : (<%- attribute.required ? '0.0' : 'null' %>)<% | ||
Fellmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// BOOLEAN | ||
} else if (attribute.type === 'boolean') { | ||
if (attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToBoolean(e)).ToList()<% | ||
} else { | ||
-%> map["<%- attribute.key %>"] != null ? Convert.ToBoolean(map["<%- attribute.key %>"]) : (<%- attribute.required ? 'false' : 'null' %>)<% | ||
Fellmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// STRING, DATETIME, EMAIL | ||
} else if (attribute.type === 'string' || attribute.type === 'datetime' || attribute.type === 'email') { | ||
if (attribute.array) { | ||
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => e.ToString()).ToList()<% | ||
} else { | ||
if (attribute.required) { | ||
-%> map["<%- attribute.key %>"].ToString()<% | ||
} else { | ||
-%> map["<%- attribute.key %>"]?.ToString()<% | ||
} | ||
} | ||
// UNKNOWN | ||
} else { | ||
-%> null <% | ||
} | ||
-%><% if (index < collection.attributes.length - 1) { %>,<% } %> | ||
<% } -%> | ||
); | ||
} | ||
|
||
public Dictionary<string, object?> ToMap() | ||
{ | ||
return new Dictionary<string, object?> | ||
{ | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
{ "<%= attribute.key %>",<% | ||
// ENUM | ||
if (attribute.format === 'enum') { | ||
if (attribute.array) { | ||
-%> <%= toPascalCase(attribute.key) %>?.Select(e => e.ToString()).ToList()<% | ||
} else { | ||
-%> <%= toPascalCase(attribute.key) %>?.ToString()<% | ||
} | ||
// RELATIONSHIP | ||
} else if (attribute.type === 'relationship') { | ||
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany' || attribute.array) { | ||
-%> <%= toPascalCase(attribute.key) %>?.Select(e => e.ToMap()).ToList()<% | ||
} else { | ||
-%> <%= toPascalCase(attribute.key) %>?.ToMap()<% | ||
} | ||
// OTHER | ||
} else { | ||
-%> <%= toPascalCase(attribute.key) %><% | ||
} | ||
-%> }<% if (index < collection.attributes.length - 1) { %>,<% } %> | ||
<% } -%> | ||
}; | ||
} | ||
} | ||
} | ||
`; | ||
} | ||
|
||
getFileName(collection) { | ||
return LanguageMeta.toPascalCase(collection.name) + ".cs"; | ||
} | ||
} | ||
|
||
module.exports = { CSharp }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.