-
Notifications
You must be signed in to change notification settings - Fork 200
feat: add Java SDK language support #1477
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
TamsavYT
wants to merge
22
commits into
appwrite:master
Choose a base branch
from
TamsavYT:feat/java-sdk
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.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
39f114c
feat: add Java SDK language support
TamsavYT ae04908
fix(java): address PR review issues
TamsavYT 3ad035e
fix(java): fix binary response corruption and overload condition
TamsavYT e96d414
fix(java): fix Maven image and overload trailing comma
TamsavYT 1f6613c
fix(java): fix List<String>/List<Object> mismatch and invalid IIFE la…
TamsavYT 2358879
fix(java): add missing ArrayList import to Query.java.twig
TamsavYT 7e18a52
fix(java): return early in chunkedUpload when file already fully uplo…
TamsavYT 6717cdc
fix(java): widen List<Object> params to List<?> across Query and Oper…
TamsavYT 6350db8
fix(java): fix redirect cast, Long default sentinel, content-type det…
TamsavYT 8648c1e
fix(java): change optional Double default from 1.0 to null
TamsavYT cdb0f14
fix(java): change optional String/Boolean defaults from sentinel to null
TamsavYT f5b7077
fix(java): detect multipart parts by type and handle array-of-enum in…
TamsavYT b3e7066
fix(java): add array-of-enum branch in toMap() for Model and RequestM…
TamsavYT de354d9
fix(java): add missing comma after last property in additionalPropert…
TamsavYT e5cc1e7
fix(java): pack distance and meters into values array for spatial dis…
TamsavYT 51767f4
fix(java): use chunkLen for Content-Range end and offset advance
TamsavYT 004eab1
fix(java): remove unreachable checked-exception catch and guard resum…
TamsavYT 3cd44d9
fix(java): deserialize query strings before nesting in or/and/elemMatch
TamsavYT 8f6cef7
fix(java): use JsonParser.parseString in parseQueries to preserve key…
TamsavYT 5240561
fix(java): wrap coordinate list as nested element in spatial predicates
TamsavYT c64f9f8
fix(java): wrap [coords,distance,meters] as single values element in …
TamsavYT 7215943
fix(java): use wall-clock nanoseconds for ID microsecond field
TamsavYT 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| <?php | ||
|
|
||
| namespace Appwrite\SDK\Language; | ||
|
|
||
| use Twig\TwigFilter; | ||
|
|
||
| class Java extends Kotlin | ||
| { | ||
| public function getName(): string | ||
| { | ||
| return 'Java'; | ||
| } | ||
|
|
||
| public function getArrayOf(string $elements): string | ||
| { | ||
| return 'List.of(' . $elements . ')'; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $parameter | ||
| * @param array $spec | ||
| * @return string | ||
| */ | ||
| public function getTypeName(array $parameter, array $spec = []): string | ||
| { | ||
| if ( | ||
| ($parameter['type'] ?? null) === self::TYPE_ARRAY | ||
| && (isset($parameter['enumName']) || !empty($parameter['enumValues'])) | ||
| ) { | ||
| $enumType = isset($parameter['enumName']) | ||
| ? \ucfirst($parameter['enumName']) | ||
| : \ucfirst($parameter['name']); | ||
|
|
||
| return 'List<io.appwrite.enums.' . $enumType . '>'; | ||
| } | ||
|
|
||
| if (isset($parameter['enumName'])) { | ||
| return 'io.appwrite.enums.' . \ucfirst($parameter['enumName']); | ||
| } | ||
| if (!empty($parameter['enumValues'])) { | ||
| return 'io.appwrite.enums.' . \ucfirst($parameter['name']); | ||
| } | ||
| if (!empty($parameter['array']['model'])) { | ||
| return 'List<io.appwrite.models.' . $this->toPascalCase($parameter['array']['model']) . '>'; | ||
| } | ||
| if (!empty($parameter['model'])) { | ||
| $modelType = 'io.appwrite.models.' . $this->toPascalCase($parameter['model']); | ||
| return $parameter['type'] === self::TYPE_ARRAY ? 'List<' . $modelType . '>' : $modelType; | ||
| } | ||
| if (isset($parameter['items'])) { | ||
| $parameter['array'] = $parameter['items']; | ||
| } | ||
| return match ($parameter['type']) { | ||
| self::TYPE_INTEGER => 'Long', | ||
| self::TYPE_NUMBER => 'Double', | ||
| self::TYPE_STRING => 'String', | ||
| self::TYPE_FILE => 'InputFile', | ||
| self::TYPE_BOOLEAN => 'Boolean', | ||
| self::TYPE_ARRAY => (!empty(($parameter['array'] ?? [])['type']) && !\is_array($parameter['array']['type'])) | ||
| ? 'List<' . $this->getTypeName($parameter['array']) . '>' | ||
| : 'List<Object>', | ||
| self::TYPE_OBJECT => 'Object', | ||
| default => $parameter['type'], | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $param | ||
| * @return string | ||
| */ | ||
| public function getParamDefault(array $param): string | ||
| { | ||
| $type = $param['type'] ?? ''; | ||
| $default = $param['default'] ?? ''; | ||
| $required = $param['required'] ?? ''; | ||
|
|
||
| if ($required) { | ||
| return ''; | ||
| } | ||
|
|
||
| $output = ' = '; | ||
|
|
||
| if (empty($default) && $default !== 0 && $default !== false) { | ||
| switch ($type) { | ||
| case self::TYPE_INTEGER: | ||
| $output .= 'null'; | ||
| break; | ||
| case self::TYPE_NUMBER: | ||
| $output .= 'null'; | ||
| break; | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| case self::TYPE_ARRAY: | ||
| case self::TYPE_OBJECT: | ||
| $output .= 'null'; | ||
| break; | ||
| case self::TYPE_BOOLEAN: | ||
| $output .= 'null'; | ||
| break; | ||
| case self::TYPE_STRING: | ||
| $output .= 'null'; | ||
| break; | ||
| } | ||
| } else { | ||
| switch ($type) { | ||
| case self::TYPE_INTEGER: | ||
| $output .= $default . 'L'; | ||
| break; | ||
| case self::TYPE_NUMBER: | ||
| $output .= sprintf("%.1f", $default); | ||
| break; | ||
| case self::TYPE_BOOLEAN: | ||
| $output .= ($default) ? 'true' : 'false'; | ||
| break; | ||
| case self::TYPE_STRING: | ||
| $output .= "\"{$default}\""; | ||
| break; | ||
| case self::TYPE_ARRAY: | ||
| case self::TYPE_OBJECT: | ||
| $output .= 'null'; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return $output; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $param | ||
| * @param string $lang unused, always java | ||
| * @return string | ||
| */ | ||
| public function getParamExample(array $param, string $lang = 'java'): string | ||
| { | ||
| return parent::getParamExample($param, 'java'); | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getFiles(): array | ||
| { | ||
| return [ | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => 'README.md', | ||
| 'template' => '/java/README.md.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => 'CHANGELOG.md', | ||
| 'template' => '/java/CHANGELOG.md.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => 'LICENSE.md', | ||
| 'template' => '/java/LICENSE.md.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => 'pom.xml', | ||
| 'template' => '/java/pom.xml.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'copy', | ||
| 'destination' => '.gitignore', | ||
| 'template' => '/java/.gitignore', | ||
| ], | ||
| [ | ||
| 'scope' => 'method', | ||
| 'destination' => 'docs/examples/{{ service.name | caseLower }}/{{ method.name | caseKebab }}.md', | ||
| 'template' => '/java/docs/example.md.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/Client.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/Client.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/Permission.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/Permission.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/Role.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/Role.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/ID.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/ID.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/Query.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/Query.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/Operator.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/Operator.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/exceptions/{{ spec.title | caseUcfirst }}Exception.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/exceptions/Exception.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/models/InputFile.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/models/InputFile.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/models/UploadProgress.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/models/UploadProgress.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'definition', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/models/Model.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'requestModel', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/models/{{ requestModel.name | caseUcfirst }}.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/models/RequestModel.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'enum', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/enums/{{ enum.name | caseUcfirst }}.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/enums/Enum.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'default', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/services/Service.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/services/Service.java.twig', | ||
| ], | ||
| [ | ||
| 'scope' => 'service', | ||
| 'destination' => '/src/main/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}.java', | ||
| 'template' => '/java/src/main/java/io/appwrite/services/ServiceTemplate.java.twig', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function getFilters(): array | ||
| { | ||
| $parentFilters = parent::getFilters(); | ||
|
|
||
| // Replace returnType filter: use Map<String, Object> instead of bare T for generic models | ||
| $overridden = []; | ||
| foreach ($parentFilters as $filter) { | ||
| if ($filter->getName() === 'returnType') { | ||
| $overridden[] = new TwigFilter('returnType', function (array $method, array $spec, string $namespace, string $generic = 'Map<String, Object>') { | ||
| return $this->getReturnType($method, $spec, $namespace, $generic); | ||
| }); | ||
| } else { | ||
| $overridden[] = $filter; | ||
| } | ||
| } | ||
| return $overridden; | ||
| } | ||
| } | ||
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,8 @@ | ||
| target/ | ||
| *.class | ||
| *.jar | ||
| *.war | ||
| *.ear | ||
| .idea/ | ||
| *.iml | ||
| .DS_Store |
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,3 @@ | ||
| # Change Log | ||
|
|
||
| {{ sdk.changelog | raw }} |
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 @@ | ||
| {{ sdk.licenseContent | raw }} |
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,38 @@ | ||
| # {{ spec.title | caseUcfirst }} Java SDK | ||
|
|
||
| {{ sdk.warning | raw }} | ||
|
|
||
| {{ sdk.readme | raw }} | ||
|
|
||
| ## Installation | ||
|
|
||
| Add the following dependency to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>{{ sdk.gitUserName }}</groupId> | ||
| <artifactId>{{ sdk.gitRepoName }}</artifactId> | ||
| <version>{{ sdk.version }}</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| ## Getting Started | ||
|
|
||
| ```java | ||
| import {{ sdk.namespace | caseDot }}.Client; | ||
| import {{ sdk.namespace | caseDot }}.services.*; | ||
|
|
||
| Client client = new Client() | ||
| .setEndpoint("{{ spec.endpointDocs | raw }}") | ||
| {% for header in spec.global.headers %} | ||
| .set{{ header.key | caseUcfirst }}("<YOUR_{{ header.key | caseUpper }}_HERE>"); | ||
| {% endfor %} | ||
| ``` | ||
|
|
||
| ## Changelog | ||
|
|
||
| {{ sdk.changelog | raw }} | ||
|
|
||
| ## Contributing | ||
|
|
||
| All code changes happen through pull requests to the [GitHub repository](https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName }}). |
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,42 @@ | ||
| ```java | ||
| import {{ sdk.namespace | caseDot }}.Client; | ||
| {% if method.parameters.all | filter((param) => param.type == 'file') | length > 0 %} | ||
| import {{ sdk.namespace | caseDot }}.models.InputFile; | ||
| {% endif %} | ||
| {% if method.parameters.all | hasPermissionParam %} | ||
| import {{ sdk.namespace | caseDot }}.Permission; | ||
| import {{ sdk.namespace | caseDot }}.Role; | ||
| {% endif %} | ||
| import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}; | ||
| {% set added = [] %} | ||
| {% for parameter in method.parameters.all %} | ||
| {% if parameter.enumValues is not empty %} | ||
| {% if parameter.enumName not in added %} | ||
| import {{ sdk.namespace | caseDot }}.enums.{{ parameter.enumName | caseUcfirst }}; | ||
| {% set added = added|merge([parameter.enumName]) %} | ||
| {% endif %} | ||
| {% endif %} | ||
| {% endfor %} | ||
|
|
||
| Client client = new Client() | ||
| {% if method.auth|length > 0 %} | ||
| .setEndpoint("{{ spec.endpointDocs | raw }}") | ||
| {% for node in method.auth %} | ||
| {% for key,header in node|keys %} | ||
| .set{{ header | caseUcfirst }}("{{ node[header]['x-appwrite']['demo'] | raw }}"); // {{ node[header].description }} | ||
| {% endfor %}{% endfor %}{% endif %} | ||
|
|
||
| {{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client); | ||
|
|
||
| {{ service.name | caseCamel }}.{{ method.name | caseCamel }}( | ||
| {%~ for parameter in method.parameters.all %} | ||
| {% if parameter.enumValues | length > 0 %}{{ parameter | javaEnumExample }}{% else %}{{ parameter | javaParamExample }}{% endif %}{% if not loop.last %},{% endif %} // {{ parameter.name }}{% if not parameter.required %} (optional){% endif %} | ||
|
|
||
| {%~ endfor %} | ||
| ).thenAccept(result -> { | ||
| System.out.println(result); | ||
| }).exceptionally(e -> { | ||
| e.printStackTrace(); | ||
| return null; | ||
| }); | ||
| ``` |
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.