Skip to content

Commit b0e67a0

Browse files
authored
Merge pull request #1186 from appwrite/feat-object-example-handling
feat: add object param examples handling
2 parents 9630aae + 3fe3c9b commit b0e67a0

File tree

14 files changed

+365
-350
lines changed

14 files changed

+365
-350
lines changed

src/SDK/Language/Dart.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -226,49 +226,29 @@ public function getParamExample(array $param): string
226226
$type = $param['type'] ?? '';
227227
$example = $param['example'] ?? '';
228228

229-
$output = '';
229+
$hasExample = !empty($example) || $example === 0 || $example === false;
230230

231-
if (empty($example) && $example !== 0 && $example !== false) {
232-
switch ($type) {
233-
case self::TYPE_FILE:
234-
$output .= 'InputFile(path: \'./path-to-files/image.jpg\', filename: \'image.jpg\')';
235-
break;
236-
case self::TYPE_NUMBER:
237-
case self::TYPE_INTEGER:
238-
$output .= '0';
239-
break;
240-
case self::TYPE_BOOLEAN:
241-
$output .= 'false';
242-
break;
243-
case self::TYPE_STRING:
244-
$output .= "''";
245-
break;
246-
case self::TYPE_OBJECT:
247-
$output .= '{}';
248-
break;
249-
case self::TYPE_ARRAY:
250-
$output .= '[]';
251-
break;
252-
}
253-
} else {
254-
switch ($type) {
255-
case self::TYPE_OBJECT:
256-
case self::TYPE_FILE:
257-
case self::TYPE_NUMBER:
258-
case self::TYPE_INTEGER:
259-
case self::TYPE_ARRAY:
260-
$output .= $example;
261-
break;
262-
case self::TYPE_BOOLEAN:
263-
$output .= ($example) ? 'true' : 'false';
264-
break;
265-
case self::TYPE_STRING:
266-
$output .= "'{$example}'";
267-
break;
268-
}
231+
if (!$hasExample) {
232+
return match ($type) {
233+
self::TYPE_OBJECT => '{}',
234+
self::TYPE_ARRAY => '[]',
235+
self::TYPE_BOOLEAN => 'false',
236+
self::TYPE_FILE => 'InputFile(path: \'./path-to-files/image.jpg\', filename: \'image.jpg\')',
237+
self::TYPE_INTEGER, self::TYPE_NUMBER => '0',
238+
self::TYPE_STRING => "''",
239+
};
269240
}
270241

271-
return $output;
242+
return match ($type) {
243+
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
244+
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
245+
self::TYPE_OBJECT => ($decoded = json_decode($example, true)) !== null
246+
? (empty($decoded) && $example === '{}'
247+
? '{}'
248+
: preg_replace('/\n/', "\n ", json_encode($decoded, JSON_PRETTY_PRINT)))
249+
: $example,
250+
self::TYPE_STRING => "'{$example}'",
251+
};
272252
}
273253

274254
/**

src/SDK/Language/Deno.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -163,48 +163,28 @@ public function getParamExample(array $param): string
163163
$type = $param['type'] ?? '';
164164
$example = $param['example'] ?? '';
165165

166-
$output = '';
166+
$hasExample = !empty($example) || $example === 0 || $example === false;
167167

168-
if (empty($example) && $example !== 0 && $example !== false) {
169-
switch ($type) {
170-
case self::TYPE_NUMBER:
171-
case self::TYPE_INTEGER:
172-
case self::TYPE_BOOLEAN:
173-
$output .= 'null';
174-
break;
175-
case self::TYPE_STRING:
176-
$output .= "''";
177-
break;
178-
case self::TYPE_ARRAY:
179-
$output .= '[]';
180-
break;
181-
case self::TYPE_OBJECT:
182-
$output .= '{}';
183-
break;
184-
case self::TYPE_FILE:
185-
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
186-
break;
187-
}
188-
} else {
189-
switch ($type) {
190-
case self::TYPE_NUMBER:
191-
case self::TYPE_INTEGER:
192-
case self::TYPE_ARRAY:
193-
case self::TYPE_OBJECT:
194-
$output .= $example;
195-
break;
196-
case self::TYPE_BOOLEAN:
197-
$output .= ($example) ? 'true' : 'false';
198-
break;
199-
case self::TYPE_STRING:
200-
$output .= "'{$example}'";
201-
break;
202-
case self::TYPE_FILE:
203-
$output .= "InputFile.fromPath('/path/to/file.png', 'file.png')";
204-
break;
205-
}
168+
if (!$hasExample) {
169+
return match ($type) {
170+
self::TYPE_ARRAY => '[]',
171+
self::TYPE_FILE => 'InputFile.fromPath(\'/path/to/file.png\', \'file.png\')',
172+
self::TYPE_INTEGER, self::TYPE_NUMBER, self::TYPE_BOOLEAN => 'null',
173+
self::TYPE_OBJECT => '{}',
174+
self::TYPE_STRING => "''",
175+
};
206176
}
207177

208-
return $output;
178+
return match ($type) {
179+
self::TYPE_ARRAY, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
180+
self::TYPE_FILE => 'InputFile.fromPath(\'/path/to/file.png\', \'file.png\')',
181+
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
182+
self::TYPE_OBJECT => ($example === '{}')
183+
? '{}'
184+
: (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT))
185+
? preg_replace('/\n/', "\n ", $formatted)
186+
: $example),
187+
self::TYPE_STRING => "'{$example}'",
188+
};
209189
}
210190
}

src/SDK/Language/DotNet.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,17 @@ public function getParamExample(array $param): string
285285
$output .= $example;
286286
break;
287287
case self::TYPE_OBJECT:
288-
$output .= '[object]';
288+
if ($example === '{}') {
289+
$output .= '[object]';
290+
} else {
291+
$decoded = json_decode($example, true);
292+
if ($decoded && is_array($decoded)) {
293+
$csharpObject = $this->formatCSharpAnonymousObject($decoded, 1);
294+
$output .= 'new ' . $csharpObject;
295+
} else {
296+
$output .= 'new ' . $example;
297+
}
298+
}
289299
break;
290300
case self::TYPE_BOOLEAN:
291301
$output .= ($example) ? 'true' : 'false';
@@ -454,4 +464,48 @@ public function getFilters(): array
454464
}),
455465
];
456466
}
467+
468+
/**
469+
* Format a PHP array as a C# anonymous object
470+
*/
471+
private function formatCSharpAnonymousObject(array $data, int $indentLevel = 0): string
472+
{
473+
$propertyIndent = str_repeat(' ', $indentLevel + 1);
474+
$properties = [];
475+
476+
foreach ($data as $key => $value) {
477+
$formattedValue = $this->formatCSharpValue($value, $indentLevel + 2);
478+
$properties[] = $propertyIndent . $key . ' = ' . $formattedValue;
479+
}
480+
481+
if (count($properties) === 1) {
482+
return '{ ' . trim($properties[0]) . ' }';
483+
}
484+
485+
$baseIndent = str_repeat(' ', $indentLevel);
486+
return "{\n" . implode(",\n", $properties) . "\n" . $baseIndent . "}";
487+
}
488+
489+
/**
490+
* Format a value for C# anonymous object property
491+
*/
492+
private function formatCSharpValue($value, int $indentLevel = 0): string
493+
{
494+
if (is_string($value)) {
495+
return '"' . $value . '"';
496+
} elseif (is_bool($value)) {
497+
return $value ? 'true' : 'false';
498+
} elseif (is_numeric($value)) {
499+
return (string) $value;
500+
} elseif (is_array($value)) {
501+
if (array_keys($value) !== range(0, count($value) - 1)) {
502+
return $this->formatCSharpAnonymousObject($value, $indentLevel);
503+
} else {
504+
$items = array_map(fn($item) => $this->formatCSharpValue($item, $indentLevel), $value);
505+
return 'new[] { ' . implode(', ', $items) . ' }';
506+
}
507+
} else {
508+
return 'null';
509+
}
510+
}
457511
}

src/SDK/Language/Go.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ public function getParamExample(array $param): string
265265
$output .= 'interface{}{' . $example . '}';
266266
break;
267267
case self::TYPE_OBJECT:
268-
$output .= 'map[string]interface{}{}';
268+
$output .= ($example === '{}')
269+
? 'map[string]interface{}{}'
270+
: (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT))
271+
? 'map[string]interface{}' . preg_replace('/\n/', "\n ", $formatted)
272+
: 'map[string]interface{}' . $example);
269273
break;
270274
case self::TYPE_BOOLEAN:
271275
$output .= ($example) ? 'true' : 'false';

src/SDK/Language/GraphQL.php

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -120,49 +120,27 @@ public function getParamExample(array $param): string
120120
$type = $param['type'] ?? '';
121121
$example = $param['example'] ?? '';
122122

123-
$output = '';
124-
125-
if (empty($example) && $example !== 0 && $example !== false) {
126-
switch ($type) {
127-
case self::TYPE_FILE:
128-
$output .= 'null';
129-
break;
130-
case self::TYPE_NUMBER:
131-
case self::TYPE_INTEGER:
132-
$output .= '0';
133-
break;
134-
case self::TYPE_BOOLEAN:
135-
$output .= 'false';
136-
break;
137-
case self::TYPE_STRING:
138-
$output .= '""';
139-
break;
140-
case self::TYPE_OBJECT:
141-
$output .= '"{}"';
142-
break;
143-
case self::TYPE_ARRAY:
144-
$output .= '[]';
145-
break;
146-
}
147-
} else {
148-
switch ($type) {
149-
case self::TYPE_FILE:
150-
case self::TYPE_NUMBER:
151-
case self::TYPE_INTEGER:
152-
case self::TYPE_ARRAY:
153-
$output .= $example;
154-
break;
155-
case self::TYPE_STRING:
156-
case self::TYPE_OBJECT:
157-
$output .= '"' . $example . '"';
158-
break;
159-
case self::TYPE_BOOLEAN:
160-
$output .= ($example) ? 'true' : 'false';
161-
break;
162-
}
123+
$hasExample = !empty($example) || $example === 0 || $example === false;
124+
125+
if (!$hasExample) {
126+
return match ($type) {
127+
self::TYPE_ARRAY => '[]',
128+
self::TYPE_BOOLEAN => 'false',
129+
self::TYPE_FILE => 'null',
130+
self::TYPE_INTEGER, self::TYPE_NUMBER => '0',
131+
self::TYPE_OBJECT => '{}',
132+
self::TYPE_STRING => '""',
133+
};
163134
}
164135

165-
return $output;
136+
return match ($type) {
137+
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
138+
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
139+
self::TYPE_OBJECT => ($example === '{}')
140+
? '"{}"'
141+
: '"' . str_replace('"', '\\"', json_encode(json_decode($example, true))) . '"',
142+
self::TYPE_STRING => '"' . $example . '"',
143+
};
166144
}
167145

168146
/**

src/SDK/Language/Kotlin.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,32 @@ public function getParamExample(array $param): string
224224
} else {
225225
switch ($type) {
226226
case self::TYPE_OBJECT:
227-
$output .= 'mapOf( "a" to "b" )';
227+
$decoded = json_decode($example, true);
228+
if ($decoded && is_array($decoded)) {
229+
$mapEntries = [];
230+
foreach ($decoded as $key => $value) {
231+
$formattedKey = '"' . $key . '"';
232+
if (is_string($value)) {
233+
$formattedValue = '"' . $value . '"';
234+
} elseif (is_bool($value)) {
235+
$formattedValue = $value ? 'true' : 'false';
236+
} elseif (is_null($value)) {
237+
$formattedValue = 'null';
238+
} elseif (is_array($value)) {
239+
$formattedValue = 'listOf()'; // Simplified for nested arrays
240+
} else {
241+
$formattedValue = (string)$value;
242+
}
243+
$mapEntries[] = ' ' . $formattedKey . ' to ' . $formattedValue;
244+
}
245+
if (count($mapEntries) > 0) {
246+
$output .= "mapOf(\n" . implode(",\n", $mapEntries) . "\n )";
247+
} else {
248+
$output .= 'mapOf( "a" to "b" )';
249+
}
250+
} else {
251+
$output .= 'mapOf( "a" to "b" )';
252+
}
228253
break;
229254
case self::TYPE_FILE:
230255
case self::TYPE_NUMBER:

src/SDK/Language/Node.php

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -127,49 +127,28 @@ public function getParamExample(array $param): string
127127
$type = $param['type'] ?? '';
128128
$example = $param['example'] ?? '';
129129

130-
$output = '';
131-
132-
if (empty($example) && $example !== 0 && $example !== false) {
133-
switch ($type) {
134-
case self::TYPE_NUMBER:
135-
case self::TYPE_INTEGER:
136-
case self::TYPE_BOOLEAN:
137-
$output .= 'null';
138-
break;
139-
case self::TYPE_STRING:
140-
$output .= "''";
141-
break;
142-
case self::TYPE_ARRAY:
143-
$output .= '[]';
144-
break;
145-
case self::TYPE_OBJECT:
146-
$output .= '{}';
147-
break;
148-
case self::TYPE_FILE:
149-
$output .= "InputFile.fromPath('/path/to/file', 'filename')";
150-
break;
151-
}
152-
} else {
153-
switch ($type) {
154-
case self::TYPE_NUMBER:
155-
case self::TYPE_INTEGER:
156-
case self::TYPE_ARRAY:
157-
case self::TYPE_OBJECT:
158-
$output .= $example;
159-
break;
160-
case self::TYPE_BOOLEAN:
161-
$output .= ($example) ? 'true' : 'false';
162-
break;
163-
case self::TYPE_STRING:
164-
$output .= "'{$example}'";
165-
break;
166-
case self::TYPE_FILE:
167-
$output .= "InputFile.fromPath('/path/to/file', 'filename')";
168-
break;
169-
}
130+
$hasExample = !empty($example) || $example === 0 || $example === false;
131+
132+
if (!$hasExample) {
133+
return match ($type) {
134+
self::TYPE_ARRAY => '[]',
135+
self::TYPE_FILE => 'InputFile.fromPath(\'/path/to/file\', \'filename\')',
136+
self::TYPE_INTEGER, self::TYPE_NUMBER, self::TYPE_BOOLEAN => 'null',
137+
self::TYPE_OBJECT => '{}',
138+
self::TYPE_STRING => "''",
139+
};
170140
}
171141

172-
return $output;
142+
return match ($type) {
143+
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
144+
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
145+
self::TYPE_OBJECT => ($example === '{}')
146+
? '{}'
147+
: (($formatted = json_encode(json_decode($example, true), JSON_PRETTY_PRINT))
148+
? preg_replace('/\n/', "\n ", $formatted)
149+
: $example),
150+
self::TYPE_STRING => "'{$example}'",
151+
};
173152
}
174153

175154
/**

0 commit comments

Comments
 (0)