Skip to content
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

feat: added constants for Error Types #724

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
812a6e6
feat: added constants for Error Types
35C4n0r Oct 10, 2023
061fea8
feat: added tests for Python ErrorEnums
35C4n0r Oct 21, 2023
2c37098
feat: add tests for Ruby ErrorEnums
35C4n0r Oct 24, 2023
a07a4b6
feat: add tests EnumTests
35C4n0r Oct 25, 2023
85fa20d
feat: add tests for ErrorEnums
35C4n0r Oct 25, 2023
3f3afb0
update: update snake_case to camelCase
35C4n0r Nov 2, 2023
33fe9cf
update: update test generator's data source.
35C4n0r Nov 4, 2023
ecb6742
Merge branch 'master' into feat-error-enum
35C4n0r Nov 12, 2023
4f55b35
feat: add test_exception.py.twig
35C4n0r Nov 12, 2023
273bb15
fix: fix AppwriteException Import
35C4n0r Nov 12, 2023
c57715a
fix: tests and ErrorEnums generated only if spec has Error info
35C4n0r Nov 17, 2023
677f4cf
Merge branch 'master' into feat-error-enum
35C4n0r Nov 17, 2023
db22b05
feat: update tests/spec.json
35C4n0r Nov 17, 2023
c429aa0
fix: update tests/spec.json and Node Import
35C4n0r Nov 17, 2023
caf5d27
fix: remove Model if name has Exception in it.
35C4n0r Nov 17, 2023
dd855af
fix: remove space from Android class name.
35C4n0r Nov 17, 2023
d22707a
fix: remove space from Android class name.
35C4n0r Nov 17, 2023
c6b70d7
fix: fix camelCase to caseCamel in swift
35C4n0r Nov 17, 2023
3350831
fix: fix ruby imports
35C4n0r Nov 22, 2023
c19f7ea
fix: fix ruby imports to exclude AppwriteException
35C4n0r Nov 22, 2023
cac17d0
fix: change pascal case to camel case
35C4n0r Nov 22, 2023
2a9b36f
fix: fix lint errors
35C4n0r Nov 22, 2023
cd5111b
Merge branch 'master' into feat-error-enum
35C4n0r Nov 22, 2023
4271b7c
refactor: refactor flutter files
35C4n0r Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/SDK/Language/Android.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ public function getFiles(): array
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
],
[
'scope' => 'default',
'destination' => 'tests/TestException.kt',
'template' => '/android/tests/TestException.kt.twig',
],
];
}
}
5 changes: 5 additions & 0 deletions src/SDK/Language/Deno.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function getFiles(): array
'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md',
'template' => 'deno/docs/example.md.twig',
],
[
'scope' => 'default',
'destination' => 'tests/test_exception.ts',
'template' => 'deno/tests/test_exception.ts.twig',
],
];
}

Expand Down
7 changes: 6 additions & 1 deletion src/SDK/Language/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ public function getFiles(): array
'scope' => 'definition',
'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs',
'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig',
]
],
[
'scope' => 'default',
'destination' => 'tests/Test{{ spec.title | caseUcfirst }}Exception.cs',
'template' => 'dotnet/tests/TestException.cs.twig',
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/SDK/Language/Flutter.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ public function getFiles(): array
'destination' => '.travis.yml',
'template' => 'flutter/.travis.yml.twig',
],
[
'scope' => 'default',
'destination' => '/test/src/exception_test.dart',
'template' => 'flutter/test/src/exception_test.dart.twig',
],
];
}
}
5 changes: 5 additions & 0 deletions src/SDK/Language/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ public function getFiles(): array
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt',
'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig',
],
[
'scope' => 'default',
'destination' => 'tests/TestException.kt',
'template' => '/kotlin/tests/TestException.kt.twig',
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/SDK/Language/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public function getFiles(): array
'destination' => '.travis.yml',
'template' => 'node/.travis.yml.twig',
],
[
'scope' => 'default',
'destination' => '/tests/test_exception.js',
'template' => 'node/tests/test_exception.js.twig',
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/SDK/Language/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ public function getFiles(): array
'destination' => '/src/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}.php',
'template' => 'php/src/Services/Service.php.twig',
],
[
'scope' => 'default',
'destination' => 'tests/TestException.php',
'template' => 'php/tests/TestException.php.twig',
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/SDK/Language/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public function getFiles(): array
'destination' => '.travis.yml',
'template' => 'python/.travis.yml.twig',
],
[
'scope' => 'default',
'destination' => 'test/{{ spec.title | caseSnake}}/test_exception.py',
'template' => 'python/tests/package/test_exception.py.twig',
],
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/SDK/Language/Ruby.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public function getFiles(): array
'destination' => '/lib/{{ spec.title | caseDash }}/models/{{ definition.name | caseSnake }}.rb',
'template' => 'ruby/lib/container/models/model.rb.twig',
],
[
'scope' => 'default',
'destination' => 'test/lib/{{ spec.title | caseDash }}/test_exception.rb',
'template' => 'ruby/tests/lib/appwrite/test_exception.rb.twig',
],
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/SDK/Language/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ public function getFiles(): array
'destination' => '/Sources/{{ spec.title | caseUcfirst}}Models/{{ definition.name | caseUcfirst }}.swift',
'template' => '/swift/Sources/Models/Model.swift.twig',
],
[
'scope' => 'default',
'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Exception.swift',
'template' => 'swift/Sources/Exception.swift.twig',
],
[
'scope' => 'default',
'destination' => '/Tests/{{ spec.title | caseUcfirst}}Tests/TestException.swift',
'template' => 'swift/Tests/TestException.swift.twig',
],
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/SDK/Language/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public function getFiles(): array
'destination' => '.travis.yml',
'template' => 'web/.travis.yml.twig',
],
[
'scope' => 'default',
'destination' => 'tests/test_exception.ts',
'template' => 'web/tests/test_exception.ts.twig',
],
[
'scope' => 'default',
'destination' => 'src/exception.ts',
'template' => 'web/src/exception.ts.twig',
],
];
}

Expand Down
6 changes: 5 additions & 1 deletion src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public function generate(string $target): void
'name' => $this->language->getName(),
'params' => $this->language->getParams(),
],
'sdk' => $this->getParams(),
'sdk' => $this->getParams()
];

foreach ($this->language->getFiles() as $file) {
Expand Down Expand Up @@ -602,6 +602,10 @@ public function generate(string $target): void
foreach ($this->spec->getDefinitions() as $key => $definition) {
$params['definition'] = $definition;

if (strpos($definition['name'], "Exception")) {
continue;
}

if ($this->exclude($file, $params)) {
continue;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public function getDefinitions()
"name" => $key,
"properties" => $schema['properties'] ?? [],
"description" => $schema['description'] ?? [],
"errorTypes" => $schema['x-appwrite']['types'] ?? null,
"required" => $schema['required'] ?? [],
"additionalProperties" => $schema['additionalProperties'] ?? []
];
Expand All @@ -337,6 +338,17 @@ public function getDefinitions()
}
}
}
if (isset($sch['errorTypes'])) {
$types = [];
foreach ($sch['errorTypes'] as $type) {
$types[] = [
'code' => $type['code'],
'type' => $type['type'],
'message' => $type['message']
];
}
$sch['errorTypes'] = $types;
}
$list[$key] = $sch;
}
return $list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception(
val code: Int? = null,
val type: String? = null,
val response: String? = null
) : Exception(message)
) : Exception(message)

{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
enum class ErrorType(val value: String) {
{% for error in spec.definitions.appwriteException.errorTypes %}
/**
* {{ error.message }}
*/
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
{% endfor %}
}
{% endif %}
17 changes: 17 additions & 0 deletions templates/android/tests/TestException.kt.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
package tests

import {{ sdk.namespace | caseDot }}.exceptions.ErrorType
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class TestException {

@Test
fun testEnumValues() {
{% for error in spec.definitions.appwriteException.errorTypes %}
assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}.value), "{{ error.type }}")
{% endfor %}
}
}
{% endif %}
2 changes: 2 additions & 0 deletions templates/dart/lib/models.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ library {{ language.params.packageName }}.models;

part 'src/models/model.dart';
{% for definition in spec.definitions %}
{% if 'Exception' not in definition.name %}
part 'src/models/{{definition.name | caseSnake}}.dart';
{% endif %}
{% endfor %}
9 changes: 9 additions & 0 deletions templates/dart/lib/src/exception.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ class {{spec.title | caseUcfirst}}Exception implements Exception {
return "{{spec.title | caseUcfirst}}Exception: ${type ?? ''}, $message (${code ?? 0})";
}
}

{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
enum ErrorType {
{% for error in spec.definitions.appwriteException.errorTypes %}
/// {{ error.message }}
{{ error.type|title|replace({'_': ''}) }},
{% endfor %}
}
{% endif %}
8 changes: 8 additions & 0 deletions templates/dart/test/src/exception_test.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ void main() {
expect(exception3.toString(), equals('{{spec.title | caseUcfirst}}Exception: ValidationError, Invalid request (400)'));
});
});

group('ErrorType Enum Test', () {
{% for key, value in testData.enumData %}
test('ErrorType.{{ key }} should have correct value', () {
expect(ErrorType.{{ error.type|title|replace({'_': ''}) }}.toString(), equals('ErrorType.{{ error.type }}'));
});
{% endfor %}
});
}
13 changes: 12 additions & 1 deletion templates/deno/src/exception.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ export class {{ spec.title | caseUcfirst}}Exception {
public toString(): String {
return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`;
}
}
}

{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
export enum ErrorType {
{% for error in spec.definitions.appwriteException.errorTypes %}
/**
* {{ error.message }}
*/
{{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}",
{% endfor %}
}
{% endif %}
10 changes: 10 additions & 0 deletions templates/deno/tests/test_exception.ts.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
import {assertEquals} from "https://deno.land/std/testing/asserts.ts";
import {ErrorType} from "../src/exception.ts";

Deno.test("ErrorType values should match expected strings", () => {
{% for error in spec.definitions.appwriteException.errorTypes %}
assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}, '{{ error.type }}');
{% endfor %}
})
{% endif %}
12 changes: 11 additions & 1 deletion templates/dotnet/src/Appwrite/Exception.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ namespace {{spec.title | caseUcfirst}}
{
}
}
}

{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
public enum ErrorType {
{% for error in spec.definitions.appwriteException.errorTypes %}
/// <summary>
/// {{ error.message }}
/// </summary>
{{ error.type|title|replace({'_': ''}) }},
{% endfor %}
}
}
{% endif %}
16 changes: 16 additions & 0 deletions templates/dotnet/tests/TestException.cs.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using {{spec.title | caseUcfirst}};

[TestClass]
public class TestException
{
[TestMethod]
public void TestEnumValues()
{
{% for error in spec.definitions.appwriteException.errorTypes %}
Assert.AreEqual(ErrorType.{{ error.type|title|replace({'_': ''}) }}, "{{ error.type }}");
{% endfor %}
}
}
{% endif %}
8 changes: 8 additions & 0 deletions templates/flutter/lib/src/exception.dart.twig
35C4n0r marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
enum ErrorType {
{% for error in spec.definitions.appwriteException.errorTypes %}
/// {{ error.message }}
{{ error.type|title|replace({'_': ''}) }},
{% endfor %}
}
{% endif %}
18 changes: 18 additions & 0 deletions templates/flutter/test/src/exception_test.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
import 'package:{{language.params.packageName}}/src/exception.dart';
{% if 'dart' in language.params.packageName %}
import 'package:test/test.dart';
{% else %}
import 'package:flutter_test/flutter_test.dart';
{% endif %}

void main() {
group('ErrorType Enum Test', () {
{% for error in spec.definitions.appwriteException.errorTypes %}
test('ErrorType.{{ key }} should have correct value', () {
expect(ErrorType.{{ error.type|title|replace({'_': ''}) }}.toString(), equals('ErrorType.{{ error.type }}'));
});
{% endfor %}
});
}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception(
val code: Int? = null,
val type: String? = null,
val response: String? = null
) : Exception(message)
) : Exception(message)

{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
enum class ErrorType(val value: String) {
{% for error in spec.definitions.appwriteException.errorTypes %}
/**
* {{ error.message }}
*/
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
{% endfor %}
}
{% endif %}
17 changes: 17 additions & 0 deletions templates/kotlin/tests/TestException.kt.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if spec.definitions.appwriteException.errorTypes|length > 0 %}
package tests

import {{ sdk.namespace | caseDot }}.exceptions.ErrorType
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class TestException {

@Test
fun testEnumValues() {
{% for error in spec.definitions.appwriteException.errorTypes %}
assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}.value), "{{ error.type }}")
{% endfor %}
}
}
{% endif %}
Loading