-
Notifications
You must be signed in to change notification settings - Fork 181
feat: types generator command #784
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
loks0n
wants to merge
47
commits into
master
Choose a base branch
from
feat-types-generator-command
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.
+1,103
−0
Open
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
5bbb5a1
feat: initial draft types generator
loks0n 3a72504
feat: typo in template name
loks0n bbb4c09
Merge branch '1.5.x' of https://github.com/appwrite/sdk-generator int…
loks0n 3d501a5
fix: use cjs
loks0n b058588
feat: add ejs
loks0n 1ef2bdc
feat: more stuff
loks0n ec07a09
feat: python
loks0n 0163bfa
chore: fmt fixes
loks0n 29cbd6b
fix: php, python
loks0n 08689e1
fix: python, php
loks0n f5ce7c4
fix: python formatting
loks0n fe9ed0e
fix: formatting
loks0n d3c471f
fix: formatting
loks0n 731dcf8
fix: formatting 2
loks0n 6f067aa
feat: swift, kotlin
loks0n d3c2134
feat: java
loks0n 69e401f
feat: php fix
loks0n b833573
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
loks0n 61e475d
feat: python models
loks0n 78b5af3
Merge branch 'master' into feat-types-generator-command
ChiragAgg5k 78ecd2a
chore: whitespace fixes
ChiragAgg5k 271a4d6
chore: formatting
ChiragAgg5k a96ed05
chore: more whitespace stuff
ChiragAgg5k 734fbc2
chore: remove python generation
ChiragAgg5k d4dc6d7
chore: work in progress dart
ChiragAgg5k e0ea6ec
chore: fix dart
ChiragAgg5k 737a136
chore: remove python models
ChiragAgg5k a75de52
chore: review changes
ChiragAgg5k eb8bd87
fix: type generation for java with class based approach
ChiragAgg5k 65a27c3
fix: compilation error in java
ChiragAgg5k 1eab080
fix: php and swift
ChiragAgg5k 59a6674
chore: return relationship type in dart
ChiragAgg5k 4cabce3
chore: add javascript jsdocs
ChiragAgg5k 0d46a41
chore: add undefined for options in js
ChiragAgg5k 70b0e93
fix: add constructor for php lang
ChiragAgg5k 4690460
fix: required
ChiragAgg5k 2e4b643
feat: add getters and setters for php
ChiragAgg5k b8f07c0
feat: complete codable for swift
ChiragAgg5k ea25a56
chore: fix naming with spaces
ChiragAgg5k 7a74cc6
feat: fix relationship attribute mapping
ChiragAgg5k 47e51e1
fix: missing relationship, use primitives in java
ChiragAgg5k 0f60155
feat: add support for enum
ChiragAgg5k 41282ce
feat: add java enum
ChiragAgg5k d9ca85a
chore: complete num for js and kotlin
ChiragAgg5k 5ffd5fa
chore: finish enum for php ts and swift
ChiragAgg5k 41efeed
Update templates/cli/lib/type-generation/languages/java.js.twig
ChiragAgg5k 8a759ca
feat: add factory and toMap methods for Dart model generation
ChiragAgg5k 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
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,126 @@ | ||
const ejs = require("ejs"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { LanguageMeta, detectLanguage } = require("../type-generation/languages/language"); | ||
const { Command, Option, Argument } = require("commander"); | ||
const { localConfig } = require("../config"); | ||
const { success, log, actionRunner } = require("../parser"); | ||
const { PHP } = require("../type-generation/languages/php"); | ||
const { TypeScript } = require("../type-generation/languages/typescript"); | ||
const { Kotlin } = require("../type-generation/languages/kotlin"); | ||
const { Swift } = require("../type-generation/languages/swift"); | ||
const { Java } = require("../type-generation/languages/java"); | ||
const { Dart } = require("../type-generation/languages/dart"); | ||
const { JavaScript } = require("../type-generation/languages/javascript"); | ||
|
||
/** | ||
* @param {string} language | ||
* @returns {import("../type-generation/languages/language").LanguageMeta} | ||
*/ | ||
function createLanguageMeta(language) { | ||
switch (language) { | ||
case "ts": | ||
return new TypeScript(); | ||
case "js": | ||
return new JavaScript(); | ||
case "php": | ||
return new PHP(); | ||
case "kotlin": | ||
return new Kotlin(); | ||
case "swift": | ||
return new Swift(); | ||
case "java": | ||
return new Java(); | ||
case "dart": | ||
return new Dart(); | ||
default: | ||
throw new Error(`Language '${language}' is not supported`); | ||
} | ||
} | ||
|
||
const templateHelpers = { | ||
toPascalCase: LanguageMeta.toPascalCase, | ||
toCamelCase: LanguageMeta.toCamelCase, | ||
toSnakeCase: LanguageMeta.toSnakeCase, | ||
toKebabCase: LanguageMeta.toKebabCase, | ||
toUpperSnakeCase: LanguageMeta.toUpperSnakeCase | ||
} | ||
|
||
const typesOutputArgument = new Argument( | ||
"<output-directory>", | ||
"The directory to write the types to" | ||
); | ||
|
||
const typesLanguageOption = new Option( | ||
"-l, --language <language>", | ||
"The language of the types" | ||
) | ||
.choices(["auto", "ts", "js", "php", "kotlin", "swift", "java", "dart"]) | ||
.default("auto"); | ||
|
||
const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => { | ||
if (language === "auto") { | ||
language = detectLanguage(); | ||
log(`Detected language: ${language}`); | ||
} | ||
|
||
const meta = createLanguageMeta(language); | ||
|
||
const outputDirectory = path.resolve(rawOutputDirectory); | ||
if (!fs.existsSync(outputDirectory)) { | ||
log(`Directory: ${outputDirectory} does not exist, creating...`); | ||
fs.mkdirSync(outputDirectory, { recursive: true }); | ||
} | ||
|
||
if (!fs.existsSync("appwrite.json")) { | ||
throw new Error("appwrite.json not found in current directory"); | ||
} | ||
|
||
const collections = localConfig.getCollections(); | ||
if (collections.length === 0) { | ||
throw new Error("No collections found in appwrite.json"); | ||
} | ||
|
||
log(`Found ${collections.length} collections: ${collections.map(c => c.name).join(", ")}`); | ||
|
||
const totalAttributes = collections.reduce((count, collection) => count + collection.attributes.length, 0); | ||
log(`Found ${totalAttributes} attributes across all collections`); | ||
|
||
const templater = ejs.compile(meta.getTemplate()); | ||
|
||
if (meta.isSingleFile()) { | ||
const content = templater({ | ||
collections, | ||
...templateHelpers, | ||
getType: meta.getType | ||
}); | ||
|
||
const destination = path.join(outputDirectory, meta.getFileName()); | ||
|
||
fs.writeFileSync(destination, content); | ||
log(`Added types to ${destination}`); | ||
} else { | ||
for (const collection of collections) { | ||
const content = templater({ | ||
collection, | ||
...templateHelpers, | ||
getType: meta.getType | ||
}); | ||
|
||
const destination = path.join(outputDirectory, meta.getFileName(collection)); | ||
|
||
fs.writeFileSync(destination, content); | ||
log(`Added types for ${collection.name} to ${destination}`); | ||
} | ||
} | ||
|
||
success(`Generated types for all the listed collections`); | ||
}); | ||
|
||
const types = new Command("types") | ||
.description("Generate types for your Appwrite project") | ||
.addArgument(typesOutputArgument) | ||
.addOption(typesLanguageOption) | ||
.action(actionRunner(typesCommand)); | ||
|
||
module.exports = { types }; |
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,16 @@ | ||
const AttributeType = { | ||
STRING: "string", | ||
INTEGER: "integer", | ||
FLOAT: "float", | ||
BOOLEAN: "boolean", | ||
DATETIME: "datetime", | ||
EMAIL: "email", | ||
IP: "ip", | ||
URL: "url", | ||
ENUM: "enum", | ||
RELATIONSHIP: "relationship", | ||
}; | ||
|
||
module.exports = { | ||
AttributeType, | ||
}; |
152 changes: 152 additions & 0 deletions
152
templates/cli/lib/type-generation/languages/dart.js.twig
ChiragAgg5k marked this conversation as resolved.
Show resolved
Hide resolved
|
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,152 @@ | ||
/** @typedef {import('../attribute').Attribute} Attribute */ | ||
const { AttributeType } = require('../attribute'); | ||
const { LanguageMeta } = require("./language"); | ||
|
||
class Dart extends LanguageMeta { | ||
getType(attribute) { | ||
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: | ||
type = LanguageMeta.toPascalCase(attribute.relatedCollection); | ||
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 `<% for (const attribute of collection.attributes) { -%> | ||
<% if (attribute.type === 'relationship') { -%> | ||
import '<%- attribute.relatedCollection.toLowerCase() %>.dart'; | ||
|
||
<% } -%> | ||
<% } -%> | ||
<% for (const attribute of collection.attributes) { -%> | ||
<% if (attribute.format === 'enum') { -%> | ||
enum <%- toPascalCase(attribute.key) %> { | ||
<% for (const element of attribute.elements) { -%> | ||
<%- element %>, | ||
<% } -%> | ||
} | ||
|
||
<% } -%> | ||
<% } -%> | ||
class <%= toPascalCase(collection.name) %> { | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<%- getType(attribute) %> <%= toCamelCase(attribute.key) %>; | ||
<% } -%> | ||
|
||
<%= toPascalCase(collection.name) %>({ | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<% if (attribute.required) { %>required <% } %>this.<%= toCamelCase(attribute.key) %>, | ||
<% } -%> | ||
}); | ||
|
||
factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) { | ||
return <%= toPascalCase(collection.name) %>( | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
<%= toCamelCase(attribute.key) %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%> | ||
<% if (attribute.format === 'enum') { -%> | ||
<% if (attribute.array) { -%> | ||
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) =&g E060 t; <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
<% if (!attribute.required) { -%> | ||
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.key) %>.values.where((e) => e.name == map['<%= attribute.key %>']).firstOrNull : null<% } else { -%> | ||
<%- toPascalCase(attribute.key) %>.values.firstWhere((e) => e.name == map['<%= attribute.key %>'])<% } -%> | ||
<% } -%> | ||
<% } else { -%> | ||
<% if (attribute.array) { -%> | ||
List<String>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% if (!attribute.required) { %> ?? null<% } -%> | ||
<% } -%> | ||
<% } -%> | ||
<% } else if (attribute.type === 'integer') { -%> | ||
<% if (attribute.array) { -%> | ||
List<int>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%> | ||
<% } -%> | ||
<% } else if (attribute.type === 'float') { -%> | ||
<% if (attribute.array) { -%> | ||
List<double>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%> | ||
<% } -%> | ||
<% } else if (attribute.type === 'boolean') { -%> | ||
<% if (attribute.array) { -%> | ||
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%> | ||
<% } -%> | ||
<% } else if (attribute.type === 'relationship') { -%> | ||
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%> | ||
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.relatedCollection) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
<% if (!attribute.required) { -%> | ||
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%> | ||
<%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>'])<% } -%> | ||
<% } -%> | ||
<% } -%>, | ||
<% } -%> | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%> | ||
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%> | ||
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%> | ||
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%> | ||
<% } -%> | ||
<% } else if (attribute.format === 'enum') { -%> | ||
<% if (attribute.array) { -%> | ||
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%> | ||
<% } else { -%> | ||
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%> | ||
<% } -%> | ||
<% } else { -%> | ||
<%= toCamelCase(attribute.key) -%> | ||
<% } -%>, | ||
<% } -%> | ||
}; | ||
} | ||
} | ||
`; | ||
} | ||
|
||
getFileName(collection) { | ||
return LanguageMeta.toSnakeCase(collection.name) + ".dart"; | ||
} | ||
} | ||
|
||
module.exports = { Dart }; |
Oops, something went wrong.
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.