Skip to content

Commit

Permalink
update handle import
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 16, 2024
1 parent 903f427 commit e925ac1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PSX\Record\Record;
use TypeAPI\Editor\Exception\GeneratorException;
use TypeAPI\Editor\Model\Document;
use TypeAPI\Editor\Model\Import;
use TypeAPI\Editor\Model\Operation;
use TypeAPI\Editor\Model\Property;
use TypeAPI\Editor\Model\Security;
Expand Down Expand Up @@ -104,6 +105,7 @@ public function toModel(Document $document, ?string $baseUrl = null): Model\Type
}

/**
* @param array<Import>|null $imports
* @return Record<string>|null
*/
private function generateImport(?array $imports): ?Record
Expand All @@ -116,15 +118,13 @@ private function generateImport(?array $imports): ?Record
$result = new Record();
foreach ($imports as $import) {
$alias = $import->getAlias() ?? null;
$user = $import->getDocument()->user->name ?? null;
$document = $import->getDocument()->name ?? null;
$version = $import->getVersion() ?? null;
$url = $import->getUrl() ?? null;

if (empty($alias) || empty($user) || empty($document) || empty($version)) {
if (empty($alias) || empty($url)) {
continue;
}

$result->put($alias, 'typehub://' . $user . ':' . $document . '@' . $version);
$result->put($alias, $url);
}

return $result;
Expand Down
44 changes: 30 additions & 14 deletions src/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
class Import implements \JsonSerializable
{
private ?string $alias;
private ?string $version;
private ?object $document;
private ?string $url;
private ?array $types;

public function __construct(array $import)
{
$this->alias = $import['alias'] ?? null;
$this->version = $import['version'] ?? null;
$this->document = $import['document'] ?? null;
$this->url = $import['url'] ?? null;
$this->types = $this->convertTypes($import['types'] ?? []);
}

public function getAlias(): ?string
Expand All @@ -50,32 +50,48 @@ public function setAlias(?string $alias): void
$this->alias = $alias;
}

public function getVersion(): ?string
public function getUrl(): ?string
{
return $this->version;
return $this->url;
}

public function setVersion(?string $version): void
public function setUrl(?string $url): void
{
$this->version = $version;
$this->url = $url;
}

public function getDocument(): ?object
public function getTypes(): ?array
{
return $this->document;
return $this->types;
}

public function setDocument(?object $document): void
public function setTypes(?array $types): void
{
$this->document = $document;
$this->types = $types;
}

public function jsonSerialize(): array
{
return [
'alias' => $this->alias,
'version' => $this->version,
'document' => $this->document,
'url' => $this->url,
'types' => $this->types,
];
}

private function convertTypes(array $types): array
{
$result = [];
foreach ($types as $type) {
if ($type instanceof \stdClass) {
$result[] = new Type((array) $type);
} elseif (is_array($type)) {
$result[] = new Type($type);
} elseif ($type instanceof Type) {
$result[] = $type;
}
}

return $result;
}
}
41 changes: 37 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

namespace TypeAPI\Editor;

use PSX\Schema\Exception\InvalidSchemaException;
use PSX\Schema\SchemaManagerInterface;
use PSX\Schema\SchemaSource;
use TypeAPI\Editor\Exception\ParserException;
use TypeAPI\Editor\Model\Argument;
use TypeAPI\Editor\Model\Document;
Expand Down Expand Up @@ -83,11 +85,27 @@ public function parse(\stdClass $data): Document
}

$imports = [];
// @TODO handle import
if (isset($data->import) && $data->import instanceof \stdClass) {
foreach (get_object_vars($data->import) as $name => $import) {
if (!is_string($import)) {
continue;
}

try {
$imports[] = $this->parseImport($name, $import);
} catch (InvalidSchemaException $e) {
throw new ParserException('Could not parse import ' . $name . ', got: ' . $e->getMessage(), previous: $e);
}
}
}

$operations = [];
if (isset($data->operations) && $data->operations instanceof \stdClass) {
foreach (get_object_vars($data->operations) as $name => $operation) {
if (!$operation instanceof \stdClass) {
continue;
}

$operations[] = $this->parseOperation($name, $operation);
}
}
Expand All @@ -99,6 +117,10 @@ public function parse(\stdClass $data): Document
$index = 0;
if (isset($data->definitions) && $data->definitions instanceof \stdClass) {
foreach (get_object_vars($data->definitions) as $name => $type) {
if (!$type instanceof \stdClass) {
continue;
}

if ($rootRef === $name) {
$root = $index;
}
Expand Down Expand Up @@ -135,12 +157,23 @@ public function parseFile(string $file): Document
return $this->parseJson(file_get_contents($file));
}

private function parseImport(string $name, \stdClass $import): Import
/**
* @throws ParserException
* @throws InvalidSchemaException
*/
private function parseImport(string $name, string $url): Import
{
$return = new Import([]);
$return->setAlias($name);
//$import->setVersion();
//$import->setDocument();
$return->setUrl($url);

$types = [];
$schema = $this->schemaManager->getSchema(SchemaSource::fromString($url));
foreach ($schema->getDefinitions()->getTypes() as $name => $type) {
$types[] = $this->parseDefinitionType($name, (object) $type->toArray());

Check failure on line 173 in src/Parser.php

View workflow job for this annotation

GitHub Actions / Psalm

ArgumentTypeCoercion

src/Parser.php:173:58: ArgumentTypeCoercion: Argument 2 of TypeAPI\Editor\Parser::parseDefinitionType expects stdClass, but parent type object provided (see https://psalm.dev/193)
}

$return->setTypes($types);

return $return;
}
Expand Down

0 comments on commit e925ac1

Please sign in to comment.