Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 15 additions & 3 deletions src/SDK/Language/Kotlin.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function getParamExample(Schema|Parameter $param, string $lang = 'kotlin'
if ($this->isPermissionString($example)) {
$output .= $this->getPermissionExample($example, $lang);
} else {
$output .= $this->getArrayExample($example, $lang);
$output .= $this->getArrayExample($example, $lang, $this->getArrayLeafType($param));
}
break;
case self::TYPE_BOOLEAN:
Expand Down Expand Up @@ -337,7 +337,7 @@ protected function getJavaMapExample(array $data, int $indentLevel = 0): string
* @param string $example Array example like '[1, 2, 3]' or '[{"key": "value"}]'
* @param string $lang Language variant: 'kotlin' or 'java'
*/
protected function getArrayExample(string $example, string $lang = 'kotlin'): string
protected function getArrayExample(string $example, string $lang = 'kotlin', ?string $leafType = null): string
{
// Try to decode as JSON to handle arrays of objects
$decoded = json_decode($example, true);
Expand All @@ -352,7 +352,7 @@ protected function getArrayExample(string $example, string $lang = 'kotlin'): st
$arrayItems[] = $lang === 'java' ? $this->getJavaMapExample($item) : $this->getKotlinMapExample($item);
} else {
// It's a nested array, recursively convert it
$arrayItems[] = $this->getArrayExample(json_encode($item), $lang);
$arrayItems[] = $this->getArrayExample(json_encode($item), $lang, $leafType);
}
} elseif (is_string($item)) {
// Primitive value
Expand All @@ -361,6 +361,8 @@ protected function getArrayExample(string $example, string $lang = 'kotlin'): st
$arrayItems[] = $item ? 'true' : 'false';
} elseif (is_null($item)) {
$arrayItems[] = 'null';
} elseif ((is_int($item) || is_float($item)) && ($leafType === self::TYPE_INTEGER || $leafType === self::TYPE_NUMBER)) {
$arrayItems[] = $this->getNumberLiteral($item, $leafType, $lang);
} else {
$arrayItems[] = (string)$item;
}
Expand All @@ -382,6 +384,16 @@ protected function getArrayExample(string $example, string $lang = 'kotlin'): st
: 'listOf(' . $example . ')';
}

protected function getArrayLeafType(Schema|Parameter $param): ?string
{
$items = $this->getArraySchema($param);
while ($items instanceof ArraySchema) {
$items = $items->items;
}

return $items instanceof Schema ? $this->getSchemaType($items) : null;
}

/**
* Generate permission example for the given language
*
Expand Down
68 changes: 35 additions & 33 deletions templates/android/docs/java/example.md.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{% set examples = [] %}
{% for parameter in (method | parameters('all')) %}
{% set examples = examples | merge([(parameter | enumValues) | length > 0 ? (parameter | javaEnumExample) : (parameter | javaParamExample)]) %}
{% endfor %}
```java
import android.util.Log;

import {{ sdk.namespace | caseDot }}.Client;
import {{ sdk.namespace | caseDot }}.coroutines.CoroutineCallback;
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.info.title | caseUcfirst }}Exception;
{% if (method | parameters('all')) | filter((param) => (param | schemaType) == 'file') | length > 0 %}
import {{ sdk.namespace | caseDot }}.models.InputFile;
{% endif %}
Expand All @@ -20,44 +25,41 @@ import {{ sdk.namespace | caseDot }}.enums.{{ (parameter | enumName) | caseUcfir
{% endif %}
{% endif %}
{% endfor %}
{% if 'List.of(' in (examples | join) %}
import java.util.List;
{% endif %}
{% if 'Map.of(' in (examples | join) %}
import java.util.Map;
{% endif %}

{% set schemes = method | securitySchemes %}
Client client = new Client(context)
{%~ if [(method | securitySchemes)]|length > 0 %}
.setEndpoint("{{ endpointDocs | raw }}") // Your API Endpoint
{%~ for node in [(method | securitySchemes)] %}
{%~ for key,header in node|keys %}
.set{{header | caseUcfirst}}("{{node[header].extensions['x-appwrite']['demo'] | raw }}"){% if loop.last %};{% endif %} // {{ node[header].description }}
{%~ endfor %}
{%~ endfor %}
{%~ endif %}
.setEndpoint("{{ endpointDocs | raw }}"){% if schemes is empty %};{% endif %} // Your API Endpoint
{% for header in schemes | keys %}
.set{{ header | caseUcfirst }}("{{ schemes[header].extensions['x-appwrite']['demo'] | raw }}"){% if loop.last %};{% endif %} // {{ schemes[header].description }}
{% endfor %}

{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);

{{ service.name | caseCamel }}.{{ (method | methodName) | caseCamel }}({% if (method | parameters('all')) | length == 0 %}new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}

Log.d("{{ spec.info.title | caseUcfirst }}", result.toString());
}));{% endif %}

{% for parameter in (method | parameters('all')) %}
{% if (parameter | enumValues) is not empty %}{{ (parameter | enumName) | caseUcfirst }}.{{ ((parameter | enumKeys)[0] ?? (parameter | enumValues)[0]) | caseEnumKey }}, // {{ parameter.name }} {% if not parameter.required %}(optional){% endif %}
{% else %}{{ parameter | javaParamExample }}, // {{ parameter.name }} {% if not parameter.required %}(optional){% endif %}
{% endif %}
{%~ if loop.last %}

new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
try {
{{ service.name | caseCamel }}.{{ (method | methodName) | caseCamel }}(
{%~ if (method | methodType) == 'webAuth' %}
activity, // activity
{%~ endif %}
{%~ for parameter in (method | parameters('all')) %}
{{ examples[loop.index0] | replace({"\n": "\n "}) | raw }}, // {{ parameter.name }}{% if not parameter.required %} (optional){% endif %}

Log.d("{{ spec.info.title | caseUcfirst }}", result.toString());
})
);
{% endif %}
{%~ endfor %}
new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}

{% endfor %}
Log.d("{{ spec.info.title | caseUcfirst }}", result.toString());
})
);
} catch ({{ spec.info.title | caseUcfirst }}Exception e) {
e.printStackTrace();
}
```
16 changes: 16 additions & 0 deletions templates/android/library/src/main/java/io/package/Channel.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,55 @@ class Channel<out T> private constructor(
override fun toString(): String = segments.joinToString(".")

companion object {
@JvmStatic
fun database(id: String): Channel<_Database> =
Channel(listOf("databases", normalize(id)))

@JvmStatic
fun tablesdb(id: String): Channel<_TablesDB> =
Channel(listOf("tablesdb", normalize(id)))

@JvmStatic
fun bucket(id: String): Channel<_Bucket> =
Channel(listOf("buckets", normalize(id)))

@JvmStatic
fun execution(id: String): Channel<_Execution> =
Channel(listOf("executions", normalize(id)))

@JvmStatic
fun function(id: String): Channel<_Func> =
Channel(listOf("functions", normalize(id)))

@JvmStatic
fun team(id: String): Channel<_Team> =
Channel(listOf("teams", normalize(id)))

@JvmStatic
fun membership(id: String): Channel<_Membership> =
Channel(listOf("memberships", normalize(id)))

@JvmStatic
fun presence(id: String): Channel<_Presence> =
Channel(listOf("presences", normalize(id)))

@JvmStatic
fun account(): String = "account"

// Global events
@JvmStatic
fun documents(): String = "documents"
@JvmStatic
fun rows(): String = "rows"
@JvmStatic
fun files(): String = "files"
@JvmStatic
fun executions(): String = "executions"
@JvmStatic
fun teams(): String = "teams"
@JvmStatic
fun memberships(): String = "memberships"
@JvmStatic
fun presences(): String = "presences"
}
}
Expand Down
3 changes: 3 additions & 0 deletions templates/android/library/src/main/java/io/package/ID.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ID {
return hexTimestamp
}

@JvmStatic
fun custom(id: String): String
= id

Expand All @@ -28,6 +29,8 @@ class ID {
* @param padding The number of characters to add to the ID
* @returns The unique ID
*/
@JvmStatic
@JvmOverloads
fun unique(padding: Int = 7): String {
val baseId = hexTimestamp()
val randomPadding = (1..padding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Operator(
fun toJson(): String = toMap().toJson()

companion object {
@JvmStatic
@JvmOverloads
fun increment(value: Number = 1, max: Number? = null): String {
require(!value.toDouble().isNaN() && !value.toDouble().isInfinite()) { "Value cannot be NaN or Infinity" }
max?.let { require(!it.toDouble().isNaN() && !it.toDouble().isInfinite()) { "Max cannot be NaN or Infinity" } }
Expand All @@ -38,6 +40,8 @@ class Operator(
return Operator("increment", values).toJson()
}

@JvmStatic
@JvmOverloads
fun decrement(value: Number = 1, min: Number? = null): String {
require(!value.toDouble().isNaN() && !value.toDouble().isInfinite()) { "Value cannot be NaN or Infinity" }
min?.let { require(!it.toDouble().isNaN() && !it.toDouble().isInfinite()) { "Min cannot be NaN or Infinity" } }
Expand All @@ -46,6 +50,8 @@ class Operator(
return Operator("decrement", values).toJson()
}

@JvmStatic
@JvmOverloads
fun multiply(factor: Number, max: Number? = null): String {
require(!factor.toDouble().isNaN() && !factor.toDouble().isInfinite()) { "Factor cannot be NaN or Infinity" }
max?.let { require(!it.toDouble().isNaN() && !it.toDouble().isInfinite()) { "Max cannot be NaN or Infinity" } }
Expand All @@ -54,6 +60,8 @@ class Operator(
return Operator("multiply", values).toJson()
}

@JvmStatic
@JvmOverloads
fun divide(divisor: Number, min: Number? = null): String {
require(!divisor.toDouble().isNaN() && !divisor.toDouble().isInfinite()) { "Divisor cannot be NaN or Infinity" }
min?.let { require(!it.toDouble().isNaN() && !it.toDouble().isInfinite()) { "Min cannot be NaN or Infinity" } }
Expand All @@ -63,12 +71,15 @@ class Operator(
return Operator("divide", values).toJson()
}

@JvmStatic
fun modulo(divisor: Number): String {
require(!divisor.toDouble().isNaN() && !divisor.toDouble().isInfinite()) { "Divisor cannot be NaN or Infinity" }
require(divisor.toDouble() != 0.0) { "Divisor cannot be zero" }
return Operator("modulo", listOf(divisor)).toJson()
}

@JvmStatic
@JvmOverloads
fun power(exponent: Number, max: Number? = null): String {
require(!exponent.toDouble().isNaN() && !exponent.toDouble().isInfinite()) { "Exponent cannot be NaN or Infinity" }
max?.let { require(!it.toDouble().isNaN() && !it.toDouble().isInfinite()) { "Max cannot be NaN or Infinity" } }
Expand All @@ -77,59 +88,74 @@ class Operator(
return Operator("power", values).toJson()
}

@JvmStatic
fun arrayAppend(values: List<Any?>): String {
return Operator("arrayAppend", values).toJson()
}

@JvmStatic
fun arrayPrepend(values: List<Any?>): String {
return Operator("arrayPrepend", values).toJson()
}

@JvmStatic
fun arrayInsert(index: Int, value: Any): String {
return Operator("arrayInsert", listOf(index, value)).toJson()
}

@JvmStatic
fun arrayRemove(value: Any): String {
return Operator("arrayRemove", listOf(value)).toJson()
}

@JvmStatic
fun arrayUnique(): String {
return Operator("arrayUnique", emptyList()).toJson()
}

@JvmStatic
fun arrayIntersect(values: List<Any?>): String {
return Operator("arrayIntersect", values).toJson()
}

@JvmStatic
fun arrayDiff(values: List<Any?>): String {
return Operator("arrayDiff", values).toJson()
}

@JvmStatic
@JvmOverloads
fun arrayFilter(condition: Condition, value: Any? = null): String {
val values = listOf<Any?>(condition.value, value)
return Operator("arrayFilter", values).toJson()
}

@JvmStatic
fun stringConcat(value: Any): String {
return Operator("stringConcat", listOf(value)).toJson()
}

@JvmStatic
fun stringReplace(search: String, replace: String): String {
return Operator("stringReplace", listOf(search, replace)).toJson()
}

@JvmStatic
fun toggle(): String {
return Operator("toggle", emptyList()).toJson()
}

@JvmStatic
fun dateAddDays(days: Int): String {
return Operator("dateAddDays", listOf(days)).toJson()
}

@JvmStatic
fun dateSubDays(days: Int): String {
return Operator("dateSubDays", listOf(days)).toJson()
}

@JvmStatic
fun dateSetNow(): String {
return Operator("dateSetNow", emptyList()).toJson()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Permission {
* @param role The role for which to generate the permission.
* @returns The read permission string.
*/
@JvmStatic
fun read(role: String): String {
return "read(\"${role}\")"
}
Expand All @@ -25,6 +26,7 @@ class Permission {
* @param role The role for which to generate the permission.
* @returns The write permission string.
*/
@JvmStatic
fun write(role: String): String {
return "write(\"${role}\")"
}
Expand All @@ -35,6 +37,7 @@ class Permission {
* @param role The role for which to generate the permission.
* @returns The create permission string.
*/
@JvmStatic
fun create(role: String): String {
return "create(\"${role}\")"
}
Expand All @@ -45,6 +48,7 @@ class Permission {
* @param role The role for which to generate the permission.
* @returns The update permission string.
*/
@JvmStatic
fun update(role: String): String {
return "update(\"${role}\")"
}
Expand All @@ -55,6 +59,7 @@ class Permission {
* @param role The role for which to generate the permission.
* @returns The delete permission string.
*/
@JvmStatic
fun delete(role: String): String {
return "delete(\"${role}\")"
}
Expand Down
Loading
Loading