Skip to content

Commit

Permalink
import go external import
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 23, 2024
1 parent aaa9361 commit 67ed589
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
35 changes: 35 additions & 0 deletions src/Generator/Go.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

namespace PSX\Schema\Generator;

use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Exception\GeneratorException;
use PSX\Schema\Generator\Normalizer\NormalizerInterface;
use PSX\Schema\Generator\Type\GeneratorInterface;
use PSX\Schema\Type\ArrayDefinitionType;
use PSX\Schema\Type\DefinitionTypeAbstract;
use PSX\Schema\Type\MapDefinitionType;
use PSX\Schema\Type\ReferencePropertyType;
use PSX\Schema\Type\StructDefinitionType;
use PSX\Schema\TypeUtil;

/**
* Go
Expand Down Expand Up @@ -103,6 +106,13 @@ protected function writeHeader(DefinitionTypeAbstract $origin, Code\Name $classN
$code.= 'package ' . $this->namespace . "\n";
}

$imports = $this->getImports($origin, $className);
if (!empty($imports)) {
$code.= "\n";
$code.= implode("\n", $imports);
$code.= "\n";
}

$comment = $origin->getDescription();
if (!empty($comment)) {
$code.= "\n";
Expand All @@ -111,4 +121,29 @@ protected function writeHeader(DefinitionTypeAbstract $origin, Code\Name $classN

return $code;
}

private function getImports(DefinitionTypeAbstract $origin, Code\Name $className): array
{
$imports = [];
$refs = TypeUtil::findRefs($origin);
foreach ($refs as $ref) {
[$ns, $name] = TypeUtil::split($ref);

$typeName = $this->normalizer->class($name);
if ($typeName === $className->getClass()) {
// we dont need to include the same class
continue;
}

if ($ns !== DefinitionsInterface::SELF_NAMESPACE) {
if (!isset($this->mapping[$ns])) {
throw new GeneratorException('Provided namespace "' . $ns . '" is not configured');
}

$imports[$ns] = 'import "' . $this->mapping[$ns] . '"';
}
}

return array_values($imports);
}
}
2 changes: 1 addition & 1 deletion src/Generator/Type/Go.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ protected function getAny(): string

protected function getNamespaced(string $namespace, string $name): string
{
return $namespace . '.' . $name;
return $name;
}
}
4 changes: 2 additions & 2 deletions tests/Generator/GoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGenerateOOP()

public function testGenerateImport()
{
$generator = new Go();
$generator = new Go(Config::of('app', ['my_import' => 'github.com/apioo/my/import']));

$chunks = $generator->generate($this->getImportSchema());
$this->write($generator, $chunks, __DIR__ . '/resource/go/import');
Expand All @@ -74,7 +74,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Go(Config::of('Foo.Bar', ['my_import' => 'My.Import']));
$generator = new Go(Config::of('app', ['my_import' => 'github.com/apioo/my/import']));

$chunks = $generator->generate($this->getImportSchema());
$this->write($generator, $chunks, __DIR__ . '/resource/go/namespace');
Expand Down
4 changes: 4 additions & 0 deletions tests/Generator/resource/go/import/import.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package app

import "github.com/apioo/my/import"

type Import struct {
Students *StudentMap `json:"students"`
Student *Student `json:"student"`
Expand Down
4 changes: 4 additions & 0 deletions tests/Generator/resource/go/import/my_map.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package app

import "github.com/apioo/my/import"

type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Expand Down
8 changes: 5 additions & 3 deletions tests/Generator/resource/go/namespace/import.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package Foo.Bar
package app

import "github.com/apioo/my/import"

type Import struct {
Students *My.Import.StudentMap `json:"students"`
Student *My.Import.Student `json:"student"`
Students *StudentMap `json:"students"`
Student *Student `json:"student"`
}

6 changes: 4 additions & 2 deletions tests/Generator/resource/go/namespace/my_map.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package Foo.Bar
package app

import "github.com/apioo/my/import"

type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Parent *My.Import.HumanType `json:"parent"`
Parent *HumanType `json:"parent"`
}

0 comments on commit 67ed589

Please sign in to comment.