Skip to content

Commit

Permalink
Merge pull request #757 from appwrite/fix-special-enum-keys
Browse files Browse the repository at this point in the history
Properly handle non-ascii characters for all languages enum keys
  • Loading branch information
abnegate authored Mar 8, 2024
2 parents d93ce5d + 3fadc16 commit 3e6a23f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
38 changes: 28 additions & 10 deletions src/SDK/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,42 @@ public function getFilters(): array

protected function toPascalCase(string $value): string
{
return ucfirst($this->toCamelCase($value));
return \ucfirst($this->toCamelCase($value));
}

protected function toCamelCase($str): string
{
$str = preg_replace('/[^a-z0-9' . implode("", []) . ']+/i', ' ', $str);
$str = trim($str);
$str = ucwords($str);
$str = str_replace(" ", "", $str);
return lcfirst($str);
// Normalize the string to decompose accented characters
$str = \Normalizer::normalize($str, \Normalizer::FORM_D);

// Remove accents and other residual non-ASCII characters
$str = \preg_replace('/\p{M}/u', '', $str);

$str = \preg_replace('/[^a-zA-Z0-9]+/', ' ', $str);
$str = \trim($str);
$str = \ucwords($str);
$str = \str_replace(' ', '', $str);
$str = \lcfirst($str);

return $str;
}

protected function toSnakeCase($str): string
{
$str = \preg_replace('/([a-z])([A-Z])/', '$1 $2', $str);
$str = \explode(' ', $str);
$str = \implode('_', $str);
return \strtolower($str);
// Normalize the string to decompose accented characters
$str = \Normalizer::normalize($str, \Normalizer::FORM_D);

// Remove accents and other residual non-ASCII characters
$str = \preg_replace('/\p{M}/u', '', $str);

// Remove apostrophes before replacing non-word characters with underscores
$str = \str_replace("'", '', $str);
$str = \preg_replace('/[^a-zA-Z0-9]+/', '_', $str);
$str = \preg_replace('/_+/', '_', $str);
$str = \trim($str, '_');
$str = \strtolower($str);

return $str;
}

protected function toUpperSnakeCase($str): string
Expand Down
1 change: 0 additions & 1 deletion src/SDK/Language/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ public function getFilters(): array
if (isset($this->getIdentifierOverrides()[$value])) {
$value = $this->getIdentifierOverrides()[$value];
}
$value = \preg_replace('/[^a-zA-Z0-9]/', '', $value);
return $this->toUpperSnakeCase($value);
}),
];
Expand Down
1 change: 0 additions & 1 deletion src/SDK/Language/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ public function getFilters(): array
if (isset($this->getIdentifierOverrides()[$value])) {
$value = $this->getIdentifierOverrides()[$value];
}
$value = \preg_replace('/[^a-zA-Z0-9]/', '', $value);
return $this->toCamelCase($value);
}),
];
Expand Down
2 changes: 1 addition & 1 deletion templates/deno/src/enums/enum.ts.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum {{ enum.name | caseUcfirst | overrideIdentifier }} {
{%~ for value in enum.enum %}
{%~ set key = enum.keys is empty ? value : enum.keys[loop.index0] %}
{{ key | caseUcfirst | replace({'-': ''}) }} = '{{ value }}',
{{ key | caseEnumKey }} = '{{ value }}',
{%~ endfor %}
}
2 changes: 1 addition & 1 deletion templates/python/package/enums/enum.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ from enum import Enum
class {{ enum.name | caseUcfirst | overrideIdentifier }}(Enum):
{% for value in enum.enum %}
{% set key = enum.keys is empty ? value : enum.keys[loop.index0] %}
{{ key | caseUpper | replace({'-': '',' ':'_', '(': '', ')': '', '.': ''})}} = "{{ value }}"
{{ key | caseEnumKey }} = "{{ value }}"
{% endfor %}
2 changes: 1 addition & 1 deletion templates/ruby/lib/container/enums/enum.rb.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module {{ spec.title | caseUcfirst }}
module {{ enum.name | caseUcfirst | overrideIdentifier }}
{%~ for value in enum.enum %}
{%~ set key = enum.keys is empty ? value : enum.keys[loop.index0] %}
{{ key | caseUpper | replace({'-': '_', ' ':'_', '(': '', ')': '', '.': ''}) }} = '{{ value }}'
{{ key | caseEnumKey }} = '{{ value }}'
{%~ endfor %}
end
end
Expand Down

0 comments on commit 3e6a23f

Please sign in to comment.