Skip to content

Commit

Permalink
added-go-support
Browse files Browse the repository at this point in the history
  • Loading branch information
KoNekoD committed Apr 12, 2024
1 parent 9f14db7 commit 1a45736
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/OutputGenerator/Go/GoGeneratorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

class GoGeneratorOptions
{
public function __construct() {}
public function __construct()
{
}
}
10 changes: 4 additions & 6 deletions src/OutputGenerator/Go/GoOutputGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
use Riverwaysoft\PhpConverter\OutputWriter\OutputWriterInterface;
use function sprintf;

class GoOutputGenerator
implements OutputGeneratorInterface
class GoOutputGenerator implements OutputGeneratorInterface
{

/** @var PropertyNameGeneratorInterface[] */
private array $propertyNameGenerators;

Expand Down Expand Up @@ -126,7 +124,7 @@ private function convertToGoType(DtoType $dto, DtoList $dtoList): string
);
}
throw new Exception(
'Unknown expression type '.
'Unknown expression type ' .
$dto->getExpressionType()->jsonSerialize()
);
}
Expand All @@ -145,7 +143,7 @@ private function getPropertyNameGenerator(
}

throw new Exception(
'Property name generator not found for type '.$dto->getName()
'Property name generator not found for type ' . $dto->getName()
);
}

Expand All @@ -168,7 +166,7 @@ private function convertEnumToTypeScriptEnumProperties(

if (array_key_exists($constName, $this->usedConstantsStore)) {
throw new Exception(
'Please rename constant '.$constName
'Please rename constant ' . $constName
);
}
$this->usedConstantsStore[] = $constName;
Expand Down
11 changes: 7 additions & 4 deletions src/OutputGenerator/Go/GoTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
class GoTypeResolver
{
/** @param UnknownTypeResolverInterface[] $unknownTypeResolvers */
public function __construct(private array $unknownTypeResolvers = []) {}
public function __construct(
private array $unknownTypeResolvers = []
) {
}

public function getTypeFromPhp(
PhpTypeInterface $type,
?DtoType $dto,
DtoList $dtoList
): string {
if ($type instanceof PhpUnionType) {
$fn = fn(PhpTypeInterface $type) => $this->getTypeFromPhp(
$fn = fn (PhpTypeInterface $type) => $this->getTypeFromPhp(
$type,
$dto,
$dtoList
Expand Down Expand Up @@ -85,14 +88,14 @@ public function getTypeFromPhp(
$type->hasGenerics() && (
$dtoList->hasDtoWithType($type->getName()) ||
!empty(
$type->getContext()[PhpUnknownType::GENERIC_IGNORE_NO_RESOLVER]
$type->getContext()[PhpUnknownType::GENERIC_IGNORE_NO_RESOLVER]
)
)
) {
$result = $type->getName();

$generics = array_map(
fn(PhpTypeInterface $innerGeneric) => $this->getTypeFromPhp(
fn (PhpTypeInterface $innerGeneric) => $this->getTypeFromPhp(
$innerGeneric,
$dto,
$dtoList,
Expand Down
18 changes: 8 additions & 10 deletions tests/OutputGenerator/GoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
use Riverwaysoft\PhpConverter\Ast\DtoVisitor;
use Riverwaysoft\PhpConverter\CodeProvider\FileSystemCodeProvider;
use Riverwaysoft\PhpConverter\Filter\PhpAttributeFilter;
use Riverwaysoft\PhpConverter\OutputGenerator\Go\GoGeneratorOptions;
use Riverwaysoft\PhpConverter\OutputGenerator\Go\GoOutputGenerator;
use Riverwaysoft\PhpConverter\OutputGenerator\Go\GoTypeResolver;
use Riverwaysoft\PhpConverter\OutputGenerator\UnknownTypeResolver\ClassNameTypeResolver;
use Riverwaysoft\PhpConverter\OutputGenerator\UnknownTypeResolver\DateTimeTypeResolver;
use Riverwaysoft\PhpConverter\OutputWriter\SingleFileOutputWriter\SingleFileOutputWriter;
use Spatie\Snapshots\MatchesSnapshots;

class GoGeneratorTest
extends TestCase
class GoGeneratorTest extends TestCase
{
use MatchesSnapshots;

Expand All @@ -32,19 +30,19 @@ protected function getSnapshotDirectory(): string
return (
dirname(
(new ReflectionClass($this))->getFileName()
).
DIRECTORY_SEPARATOR.
) .
DIRECTORY_SEPARATOR .
'__snapshots__'
);
}

return (
dirname(
(new ReflectionClass($this))->getFileName()
).
DIRECTORY_SEPARATOR.
'__snapshots__'.
DIRECTORY_SEPARATOR.
) .
DIRECTORY_SEPARATOR .
'__snapshots__' .
DIRECTORY_SEPARATOR .
$this->snapshotSubDirectory
);
}
Expand Down Expand Up @@ -189,7 +187,7 @@ public function testNormalizationDirectory(): void
{
$this->snapshotSubDirectory = 'GoGeneratorTest_testNormalizationDirectory';
$converter = new Converter([new DtoVisitor()]);
$fileProvider = FileSystemCodeProvider::phpFiles(__DIR__.'/Fixtures');
$fileProvider = FileSystemCodeProvider::phpFiles(__DIR__ . '/Fixtures');
$result = $converter->convert($fileProvider->getListings());
$this->assertMatchesJsonSnapshot($result->dtoList->getList());
$results = (new GoOutputGenerator(
Expand Down

0 comments on commit 1a45736

Please sign in to comment.