Skip to content

Commit 3ae14bd

Browse files
committed
Enhanced Generator Code to generate Endpoints Using OpenSearch API Specifications
Signed-off-by: saimedhi <[email protected]>
1 parent 8d47de1 commit 3ae14bd

21 files changed

+434
-124
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"ext-json": ">=1.3.7",
1818
"ext-curl": "*",
1919
"ezimuel/ringphp": "^1.1.2",
20-
"psr/log": "^1|^2|^3"
20+
"psr/log": "^1|^2|^3",
21+
"symfony/yaml": "*"
2122
},
2223
"require-dev": {
2324
"ext-zip": "*",

src/OpenSearch/Endpoints/AbstractEndpoint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use OpenSearch\Common\Exceptions\UnexpectedValueException;
2525
use OpenSearch\Serializers\SerializerInterface;
26+
2627
use function array_filter;
2728

2829
abstract class AbstractEndpoint

src/OpenSearch/Namespaces/IndicesNamespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ public function getDataStream(array $params = [])
11751175
}
11761176
/**
11771177
* $params['index'] = (list) A comma-separated list of index names to refresh analyzers for
1178-
*
1178+
*
11791179
* @param array $params Associative array of parameters
11801180
* @return array
11811181
*/

src/OpenSearch/Namespaces/SqlNamespace.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace OpenSearch\Namespaces;
1515

1616
use OpenSearch\Endpoints\AbstractEndpoint;
17+
1718
use function array_filter;
1819

1920
class SqlNamespace extends AbstractNamespace

src/OpenSearch/Transport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function performRequest(string $method, string $uri, array $params = [],
122122
//onSuccess
123123
function ($response) {
124124
$this->retryAttempts = 0;
125-
// Note, this could be a 4xx or 5xx error
125+
// Note, this could be a 4xx or 5xx error
126126
},
127127
//onFailure
128128
function ($response) {

tests/ConnectionPool/StaticConnectionPoolTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function testAllExceptLastHostFailPingRevivesPreSkip()
190190
$goodConnection->expects('isAlive')->andReturns(false);
191191
$goodConnection->expects('markDead');
192192
$goodConnection->expects('getPingFailures')->andReturns(0);
193-
$goodConnection->expects('getLastPing')->andReturns(time()-10000);
193+
$goodConnection->expects('getLastPing')->andReturns(time() - 10000);
194194

195195
$connections[] = $goodConnection;
196196

tests/Connections/ConnectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Exception;
3232
use Psr\Log\LoggerInterface;
3333
use ReflectionClass;
34+
3435
use function base64_encode;
3536
use function random_bytes;
3637

util/ActionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function match(array $actions)
244244
if (is_string($actions[$key]) && substr($actions[$key], 0, 1) !== '$') {
245245
$expected = sprintf("'%s'", addslashes($actions[$key]));
246246
} elseif (is_string($actions[$key]) && substr($actions[$key], 0, 2) === '${') {
247-
$expected = sprintf("\$%s", substr($actions[$key], 2, strlen($actions[$key])-3));
247+
$expected = sprintf("\$%s", substr($actions[$key], 2, strlen($actions[$key]) - 3));
248248
} elseif (is_bool($actions[$key])) {
249249
$expected = $actions[$key] ? 'true' : 'false';
250250
} elseif (is_array($actions[$key])) {
@@ -422,8 +422,8 @@ private function convertDotToArrow(string $dot)
422422
$result = str_replace('.', '()->', $dot);
423423
$tot = strlen($result);
424424
for ($i = 0; $i < $tot; $i++) {
425-
if ($result[$i] === '_' && ($i+1) < $tot) {
426-
$result[$i+1] = strtoupper($result[$i+1]);
425+
if ($result[$i] === '_' && ($i + 1) < $tot) {
426+
$result[$i + 1] = strtoupper($result[$i + 1]);
427427
}
428428
}
429429
return str_replace('_', '', $result);

util/ClientEndpoint.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ class ClientEndpoint extends NamespaceEndpoint
3434
protected $endpoints = [];
3535
protected $endpointNames = [];
3636
protected $namespace = [];
37-
protected $version;
38-
protected $buildhash;
3937

40-
public function __construct(array $namespace, string $version, string $buildhash)
38+
public function __construct(array $namespace)
4139
{
4240
$this->namespace = $namespace;
43-
$this->version = $version;
44-
$this->buildhash = $buildhash;
4541
}
4642

4743
public function renderClass(): string
@@ -108,8 +104,6 @@ public function renderClass(): string
108104
$functions .= $func;
109105
}
110106
$class = str_replace(':functions', $functions, $class);
111-
$class = str_replace(':version', $this->version, $class);
112-
$class = str_replace(':buildhash', $this->buildhash, $class);
113107

114108
return $class;
115109
}

util/Endpoint.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Endpoint
3535
public const CHECK_OPTIONS_TEMPLATE = __DIR__ . '/template/check-options';
3636
public const SET_BULK_BODY_TEMPLATE = __DIR__ . '/template/set-bulk-body';
3737
public const DEPRECATED_PART = __DIR__ . '/template/deprecated';
38+
public const DEPRECATED_PARAM = __DIR__ . '/template/deprecated-param-master-timeout';
3839
public const PHP_RESERVED_WORDS = [
3940
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch',
4041
'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
@@ -58,8 +59,6 @@ class Endpoint
5859
public $name;
5960
public $apiName;
6061
protected $content;
61-
protected $version;
62-
protected $buildhash;
6362
protected $parts = [];
6463
protected $requiredParts = [];
6564
protected $useNamespace = [];
@@ -69,8 +68,6 @@ class Endpoint
6968
public function __construct(
7069
string $fileName,
7170
string $content,
72-
string $version,
73-
string $buildhash
7471
) {
7572
$this->apiName = basename($fileName, '.json');
7673
$parts = explode('.', $this->apiName);
@@ -95,8 +92,6 @@ public function __construct(
9592
));
9693
}
9794
$this->content = $this->content[$this->apiName];
98-
$this->version = $version;
99-
$this->buildhash = $buildhash;
10095

10196
$this->parts = $this->getPartsFromContent($this->content);
10297
$this->requiredParts = $this->getRequiredParts($this->content);
@@ -127,6 +122,8 @@ private function getRequiredParts(array $content): array
127122
}
128123
if (count($required) > 1) {
129124
return call_user_func_array('array_intersect', $required);
125+
} elseif (count($required) === 1) {
126+
return $required[0];
130127
}
131128
return $required;
132129
}
@@ -146,7 +143,7 @@ public function renderClass(): string
146143
}
147144
$class = str_replace(
148145
':uri',
149-
$this->extractUrl($this->content['url']['paths']),
146+
trim($this->extractUrl($this->content['url']['paths'])),
150147
$class
151148
);
152149
$class = str_replace(
@@ -164,11 +161,15 @@ public function renderClass(): string
164161

165162
// Set the HTTP method
166163
$action = $this->getMethod();
167-
if (!empty($this->content['body']) &&
168-
($action === ['GET', 'POST'] || $action === ['POST', 'GET'])) {
169-
$method = 'isset($this->body) ? \'POST\' : \'GET\'';
164+
if ($action === ['POST', 'PUT'] && $this->getClassName() !== 'Bulk') {
165+
$method = "'PUT'";
170166
} else {
171-
$method = sprintf("'%s'", reset($action));
167+
if (!empty($this->content['body']) &&
168+
($action === ['GET', 'POST'] || $action === ['POST', 'GET'])) {
169+
$method = 'isset($this->body) ? \'POST\' : \'GET\'';
170+
} else {
171+
$method = sprintf("'%s'", reset($action));
172+
}
172173
}
173174
$class = str_replace(':method', $method, $class);
174175

@@ -186,19 +187,44 @@ public function renderClass(): string
186187
if (in_array($part, ['type', 'index', 'id'])) {
187188
continue;
188189
}
189-
if (isset($value['type']) && $value['type'] === 'list') {
190+
if (isset($value['type']) && $value['type'] === 'array') {
190191
$parts .= $this->getSetPartList($part);
191192
} else {
192193
$parts .= $this->getSetPart($part);
193194
}
194195
}
195196
$class = str_replace(':set-parts', $parts, $class);
196197
$class = str_replace(':endpoint', $this->getClassName(), $class);
197-
$class = str_replace(':version', $this->version, $class);
198-
$class = str_replace(':buildhash', $this->buildhash, $class);
199198
$class = str_replace(':use-namespace', $this->getNamespaces(), $class);
200199
$class = str_replace(':properties', $this->getProperties(), $class);
200+
if (isset($this->content['params']['master_timeout'])) {
201+
$deprecatedContent = file_get_contents(self::DEPRECATED_PARAM);
202+
$class = str_replace(':master_timeout_ParamDeprecation', $deprecatedContent, $class);
203+
} else {
204+
$class = preg_replace('/\s*:master_timeout_ParamDeprecation\s*\}/', '}', $class);
205+
}
206+
207+
// Adding licence header
208+
$currentDir = dirname(__FILE__);
209+
$baseDir = dirname($currentDir);
210+
$EndpointName = $this->getClassName();
211+
212+
if (!empty($this->namespace)) {
213+
$filePath = $baseDir . "/src/OpenSearch/Endpoints/$this->namespace/$EndpointName.php";
214+
} else {
215+
$filePath = $baseDir . "/src/OpenSearch/Endpoints/$EndpointName.php";
216+
}
217+
218+
if (file_exists($filePath)) {
219+
$content = file_get_contents($filePath);
201220

221+
if (strpos($content, 'Copyright OpenSearch') !== false) {
222+
$pattern = '/\/\*\*.*?\*\//s';
223+
if (preg_match($pattern, $content, $matches)) {
224+
$class = str_replace('declare(strict_types=1);', 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $matches[0], $class);
225+
}
226+
}
227+
}
202228
return str_replace(':apiname', $this->apiName, $class);
203229
}
204230

@@ -291,7 +317,7 @@ private function extractUrl(array $paths): string
291317
$check = sprintf("isset(\$%s)", $parts[0]);
292318
}
293319
$url = str_replace('{' . $parts[0] .'}', '$' . $parts[0], $path);
294-
for ($i=1; $i<count($parts); $i++) {
320+
for ($i = 1; $i < count($parts); $i++) {
295321
$url = str_replace('{' . $parts[$i] .'}', '$' . $parts[$i], $url);
296322
if (in_array($parts[$i], $this->requiredParts)) {
297323
continue;
@@ -351,7 +377,7 @@ private function extractPaths(array $paths): array
351377
$urls = $this->removePathWithSameParts($paths);
352378
// Order the url based on descendant length
353379
usort($urls, function ($a, $b) {
354-
return strlen($b)-strlen($a);
380+
return strlen($b) - strlen($a);
355381
});
356382

357383
return $urls;
@@ -494,8 +520,8 @@ private function extractPartsDescription(int $space): string
494520
" * \$params['%s']%s = %s(%s) %s%s\n",
495521
$part,
496522
str_repeat(' ', $space - strlen($part)),
497-
$part ==='type' || (isset($values['deprecated']) && $values['deprecated']) ? 'DEPRECATED ' : '',
498-
$values['type'],
523+
$part === 'type' || (isset($values['deprecated']) && $values['deprecated']) ? 'DEPRECATED ' : '',
524+
$values['type'] ?? 'any',
499525
$values['description'] ?? '',
500526
in_array($part, $this->requiredParts) ? ' (Required)' : ''
501527
);
@@ -518,7 +544,7 @@ private function extractParamsDescription(int $space): string
518544
" * \$params['%s']%s = (%s) %s%s%s%s\n",
519545
$param,
520546
str_repeat(' ', $space - strlen($param)),
521-
$values['type'],
547+
$values['type'] ?? 'any',
522548
$values['description'] ?? '',
523549
isset($values['required']) && $values['required'] ? ' (Required)' : '',
524550
isset($values['options']) ? sprintf(" (Options = %s)", implode(',', $values['options'])) : '',

util/GenerateDocExamples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797

9898
printf("\nLanguage statistics:\n");
9999
foreach ($langs as $lang => $num) {
100-
printf("%-8s: %4d (%.2f%% completed)\n", $lang, $num, (100/$tot)*$num);
100+
printf("%-8s: %4d (%.2f%% completed)\n", $lang, $num, (100 / $tot) * $num);
101101
}
102102
printf("\n");
103103

0 commit comments

Comments
 (0)