diff --git a/lib/commands/types.js b/lib/commands/types.js index 78428f1e..cae9e1f6 100644 --- a/lib/commands/types.js +++ b/lib/commands/types.js @@ -13,6 +13,7 @@ const { Java } = require("../type-generation/languages/java"); const { Dart } = require("../type-generation/languages/dart"); const { JavaScript } = require("../type-generation/languages/javascript"); const { CSharp } = require("../type-generation/languages/csharp"); +const { Go } = require("../type-generation/languages/go"); /** * @param {string} language @@ -36,6 +37,8 @@ function createLanguageMeta(language) { return new Dart(); case "cs": return new CSharp(); + case "go": + return new Go(); default: throw new Error(`Language '${language}' is not supported`); } @@ -58,7 +61,7 @@ const typesLanguageOption = new Option( "-l, --language ", "The language of the types" ) - .choices(["auto", "ts", "js", "php", "kotlin", "swift", "java", "dart", "cs"]) + .choices(["auto", "ts", "js", "php", "kotlin", "swift", "java", "dart", "cs", "go"]) .default("auto"); const typesStrictOption = new Option( diff --git a/lib/type-generation/languages/go.js b/lib/type-generation/languages/go.js new file mode 100644 index 00000000..810c08a9 --- /dev/null +++ b/lib/type-generation/languages/go.js @@ -0,0 +1,83 @@ +/** @typedef {import('../attribute').Attribute} Attribute */ +const { AttributeType } = require('../attribute'); +const { LanguageMeta } = require("./language"); + +class Go extends LanguageMeta { + getType(attribute, collections) { + let type = ""; + switch (attribute.type) { + case AttributeType.STRING: + case AttributeType.EMAIL: + case AttributeType.DATETIME: + case AttributeType.IP: + case AttributeType.URL: + type = "string"; + if (attribute.format === AttributeType.ENUM) { + type = LanguageMeta.toPascalCase(attribute.key); + } + break; + case AttributeType.INTEGER: + type = "int"; + break; + case AttributeType.FLOAT: + type = "float64"; + 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 = `[]${type}`; + } + break; + default: + throw new Error(`Unknown attribute type: ${attribute.type}`); + } + if (attribute.array) { + type = "[]" + type; + } + if (!attribute.required && attribute.default === null) { + type = "*" + type; + } + return type; + } + + getTemplate() { + return `package models + +/** + * This file is auto-generated by the Appwrite CLI. + * You can regenerate it by running \`appwrite ${process.argv.slice(2).join(' ')}\`. + */ + +<% for (const attribute of collection.attributes) { -%> +<% if (attribute.format === 'enum') { -%> +type <%- toPascalCase(attribute.key) %> string + +const ( +<% for (const [index, element] of Object.entries(attribute.elements)) { -%> + <%- toPascalCase(attribute.key) %><%- toPascalCase(element) %> <%- toPascalCase(attribute.key) %> = "<%- element %>" +<% } -%> +) + +<% } -%> +<% } -%> +type <%- toPascalCase(collection.name) %> struct { +<% for (const attribute of collection.attributes) { -%> + <%- toPascalCase(attribute.key) %> <%- getType(attribute, collections) %> \`json:"<%- attribute.key %>"\` +<% } -%> +} +`; + } + + getFileName(collection) { + return LanguageMeta.toSnakeCase(collection.name) + ".go"; + } +} + +module.exports = { Go }; diff --git a/lib/type-generation/languages/language.js b/lib/type-generation/languages/language.js index 96ba0bba..c7d71998 100644 --- a/lib/type-generation/languages/language.js +++ b/lib/type-generation/languages/language.js @@ -119,6 +119,9 @@ function detectLanguage() { if (existsFiles("pubspec.yaml")) { return "dart"; } + if (existsFiles("go.mod", "go.sum")) { + return "go"; + } throw new Error("Could not detect language, please specify with -l"); }