Skip to content
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

Enhanced Generator Code to generate Endpoints using OpenSearch API Specifications #194

Merged
merged 1 commit into from
May 29, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
### Added
- Enhanced Generator Code to generate Endpoints using OpenSearch API Specifications ([#194](https://github.com/opensearch-project/opensearch-php/pull/194))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "Enhanced Generator Code " is kinda redundant, simplify "Generate endpoints from OpenSearch OpenAPI spec" or something like that.

### Changed
### Deprecated
### Removed
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"ext-json": ">=1.3.7",
"ext-curl": "*",
"ezimuel/ringphp": "^1.1.2",
"psr/log": "^1|^2|^3"
"psr/log": "^1|^2|^3",
"symfony/yaml": "*"
},
"require-dev": {
"ext-zip": "*",
Expand Down
1 change: 1 addition & 0 deletions src/OpenSearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OpenSearch\Common\Exceptions\UnexpectedValueException;
use OpenSearch\Serializers\SerializerInterface;

use function array_filter;

abstract class AbstractEndpoint
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSearch/Namespaces/IndicesNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ public function getDataStream(array $params = [])
}
/**
* $params['index'] = (list) A comma-separated list of index names to refresh analyzers for
*
*
* @param array $params Associative array of parameters
* @return array
*/
Expand Down
1 change: 1 addition & 0 deletions src/OpenSearch/Namespaces/SqlNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace OpenSearch\Namespaces;

use OpenSearch\Endpoints\AbstractEndpoint;

use function array_filter;

class SqlNamespace extends AbstractNamespace
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function performRequest(string $method, string $uri, array $params = [],
//onSuccess
function ($response) {
$this->retryAttempts = 0;
// Note, this could be a 4xx or 5xx error
// Note, this could be a 4xx or 5xx error
},
//onFailure
function ($response) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionPool/StaticConnectionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function testAllExceptLastHostFailPingRevivesPreSkip()
$goodConnection->expects('isAlive')->andReturns(false);
$goodConnection->expects('markDead');
$goodConnection->expects('getPingFailures')->andReturns(0);
$goodConnection->expects('getLastPing')->andReturns(time()-10000);
$goodConnection->expects('getLastPing')->andReturns(time() - 10000);

$connections[] = $goodConnection;

Expand Down
1 change: 1 addition & 0 deletions tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use ReflectionClass;

use function base64_encode;
use function random_bytes;

Expand Down
6 changes: 3 additions & 3 deletions util/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private function match(array $actions)
if (is_string($actions[$key]) && substr($actions[$key], 0, 1) !== '$') {
$expected = sprintf("'%s'", addslashes($actions[$key]));
} elseif (is_string($actions[$key]) && substr($actions[$key], 0, 2) === '${') {
$expected = sprintf("\$%s", substr($actions[$key], 2, strlen($actions[$key])-3));
$expected = sprintf("\$%s", substr($actions[$key], 2, strlen($actions[$key]) - 3));
} elseif (is_bool($actions[$key])) {
$expected = $actions[$key] ? 'true' : 'false';
} elseif (is_array($actions[$key])) {
Expand Down Expand Up @@ -422,8 +422,8 @@ private function convertDotToArrow(string $dot)
$result = str_replace('.', '()->', $dot);
$tot = strlen($result);
for ($i = 0; $i < $tot; $i++) {
if ($result[$i] === '_' && ($i+1) < $tot) {
$result[$i+1] = strtoupper($result[$i+1]);
if ($result[$i] === '_' && ($i + 1) < $tot) {
$result[$i + 1] = strtoupper($result[$i + 1]);
}
}
return str_replace('_', '', $result);
Expand Down
8 changes: 1 addition & 7 deletions util/ClientEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ class ClientEndpoint extends NamespaceEndpoint
protected $endpoints = [];
protected $endpointNames = [];
protected $namespace = [];
protected $version;
protected $buildhash;

public function __construct(array $namespace, string $version, string $buildhash)
public function __construct(array $namespace)
{
$this->namespace = $namespace;
$this->version = $version;
$this->buildhash = $buildhash;
}

public function renderClass(): string
Expand Down Expand Up @@ -108,8 +104,6 @@ public function renderClass(): string
$functions .= $func;
}
$class = str_replace(':functions', $functions, $class);
$class = str_replace(':version', $this->version, $class);
$class = str_replace(':buildhash', $this->buildhash, $class);

return $class;
}
Expand Down
64 changes: 45 additions & 19 deletions util/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Endpoint
public const CHECK_OPTIONS_TEMPLATE = __DIR__ . '/template/check-options';
public const SET_BULK_BODY_TEMPLATE = __DIR__ . '/template/set-bulk-body';
public const DEPRECATED_PART = __DIR__ . '/template/deprecated';
public const DEPRECATED_PARAM = __DIR__ . '/template/deprecated-param-master-timeout';
public const PHP_RESERVED_WORDS = [
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch',
'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do',
Expand All @@ -58,8 +59,6 @@ class Endpoint
public $name;
public $apiName;
protected $content;
protected $version;
protected $buildhash;
protected $parts = [];
protected $requiredParts = [];
protected $useNamespace = [];
Expand All @@ -69,8 +68,6 @@ class Endpoint
public function __construct(
string $fileName,
string $content,
string $version,
string $buildhash
) {
$this->apiName = basename($fileName, '.json');
$parts = explode('.', $this->apiName);
Expand All @@ -95,8 +92,6 @@ public function __construct(
));
}
$this->content = $this->content[$this->apiName];
$this->version = $version;
$this->buildhash = $buildhash;

$this->parts = $this->getPartsFromContent($this->content);
$this->requiredParts = $this->getRequiredParts($this->content);
Expand Down Expand Up @@ -127,6 +122,8 @@ private function getRequiredParts(array $content): array
}
if (count($required) > 1) {
return call_user_func_array('array_intersect', $required);
} elseif (count($required) === 1) {
return $required[0];
}
return $required;
}
Expand All @@ -146,7 +143,7 @@ public function renderClass(): string
}
$class = str_replace(
':uri',
$this->extractUrl($this->content['url']['paths']),
trim($this->extractUrl($this->content['url']['paths'])),
$class
);
$class = str_replace(
Expand All @@ -164,11 +161,15 @@ public function renderClass(): string

// Set the HTTP method
$action = $this->getMethod();
if (!empty($this->content['body']) &&
($action === ['GET', 'POST'] || $action === ['POST', 'GET'])) {
$method = 'isset($this->body) ? \'POST\' : \'GET\'';
if ($action === ['POST', 'PUT'] && $this->getClassName() !== 'Bulk') {
$method = "'PUT'";
} else {
$method = sprintf("'%s'", reset($action));
if (!empty($this->content['body']) &&
($action === ['GET', 'POST'] || $action === ['POST', 'GET'])) {
$method = 'isset($this->body) ? \'POST\' : \'GET\'';
} else {
$method = sprintf("'%s'", reset($action));
}
}
$class = str_replace(':method', $method, $class);

Expand All @@ -186,19 +187,44 @@ public function renderClass(): string
if (in_array($part, ['type', 'index', 'id'])) {
continue;
}
if (isset($value['type']) && $value['type'] === 'list') {
if (isset($value['type']) && $value['type'] === 'array') {
$parts .= $this->getSetPartList($part);
} else {
$parts .= $this->getSetPart($part);
}
}
$class = str_replace(':set-parts', $parts, $class);
$class = str_replace(':endpoint', $this->getClassName(), $class);
$class = str_replace(':version', $this->version, $class);
$class = str_replace(':buildhash', $this->buildhash, $class);
$class = str_replace(':use-namespace', $this->getNamespaces(), $class);
$class = str_replace(':properties', $this->getProperties(), $class);
if (isset($this->content['params']['master_timeout'])) {
$deprecatedContent = file_get_contents(self::DEPRECATED_PARAM);
$class = str_replace(':master_timeout_ParamDeprecation', $deprecatedContent, $class);
} else {
$class = preg_replace('/\s*:master_timeout_ParamDeprecation\s*\}/', '}', $class);
}

// Adding licence header
$currentDir = dirname(__FILE__);
$baseDir = dirname($currentDir);
$EndpointName = $this->getClassName();

if (!empty($this->namespace)) {
$filePath = $baseDir . "/src/OpenSearch/Endpoints/$this->namespace/$EndpointName.php";
} else {
$filePath = $baseDir . "/src/OpenSearch/Endpoints/$EndpointName.php";
}

if (file_exists($filePath)) {
$content = file_get_contents($filePath);

if (strpos($content, 'Copyright OpenSearch') !== false) {
$pattern = '/\/\*\*.*?\*\//s';
if (preg_match($pattern, $content, $matches)) {
$class = str_replace('declare(strict_types=1);', 'declare(strict_types=1);' . PHP_EOL . PHP_EOL . $matches[0], $class);
}
}
}
return str_replace(':apiname', $this->apiName, $class);
}

Expand Down Expand Up @@ -291,7 +317,7 @@ private function extractUrl(array $paths): string
$check = sprintf("isset(\$%s)", $parts[0]);
}
$url = str_replace('{' . $parts[0] .'}', '$' . $parts[0], $path);
for ($i=1; $i<count($parts); $i++) {
for ($i = 1; $i < count($parts); $i++) {
$url = str_replace('{' . $parts[$i] .'}', '$' . $parts[$i], $url);
if (in_array($parts[$i], $this->requiredParts)) {
continue;
Expand Down Expand Up @@ -351,7 +377,7 @@ private function extractPaths(array $paths): array
$urls = $this->removePathWithSameParts($paths);
// Order the url based on descendant length
usort($urls, function ($a, $b) {
return strlen($b)-strlen($a);
return strlen($b) - strlen($a);
});

return $urls;
Expand Down Expand Up @@ -494,8 +520,8 @@ private function extractPartsDescription(int $space): string
" * \$params['%s']%s = %s(%s) %s%s\n",
$part,
str_repeat(' ', $space - strlen($part)),
$part ==='type' || (isset($values['deprecated']) && $values['deprecated']) ? 'DEPRECATED ' : '',
$values['type'],
$part === 'type' || (isset($values['deprecated']) && $values['deprecated']) ? 'DEPRECATED ' : '',
$values['type'] ?? 'any',
$values['description'] ?? '',
in_array($part, $this->requiredParts) ? ' (Required)' : ''
);
Expand All @@ -518,7 +544,7 @@ private function extractParamsDescription(int $space): string
" * \$params['%s']%s = (%s) %s%s%s%s\n",
$param,
str_repeat(' ', $space - strlen($param)),
$values['type'],
$values['type'] ?? 'any',
$values['description'] ?? '',
isset($values['required']) && $values['required'] ? ' (Required)' : '',
isset($values['options']) ? sprintf(" (Options = %s)", implode(',', $values['options'])) : '',
Expand Down
2 changes: 1 addition & 1 deletion util/GenerateDocExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

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

Expand Down
Loading
Loading