Skip to content

Commit

Permalink
Add support for simplified syntax of x-go-type: foo-go::foo.Type.
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Apr 4, 2020
1 parent 14dddc1 commit 70cf318
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.32] - 2020-04-04

### Added
- Support for simplified syntax of `x-go-type: foo-go::foo.Type`.

## [0.4.31] - 2020-04-03

### Added
Expand Down Expand Up @@ -191,6 +196,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Removed unnecessary regexp dependency, #7.

[0.4.32]: https://github.com/swaggest/go-code-builder/compare/v0.4.31...v0.4.32
[0.4.31]: https://github.com/swaggest/go-code-builder/compare/v0.4.30...v0.4.31
[0.4.30]: https://github.com/swaggest/go-code-builder/compare/v0.4.29...v0.4.30
[0.4.29]: https://github.com/swaggest/go-code-builder/compare/v0.4.28...v0.4.29
Expand Down
28 changes: 20 additions & 8 deletions src/Templates/Type/TypeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,28 @@ public static function fromString($typeString)

$parts = explode('::', $typeString);
$import = null;
if (false !== $dotPos = strrpos($parts[0], '.')) {
if (isset($parts[1])) {
list($packageName) = explode('.', $parts[1]);
$import = new Import(substr($parts[0], 0, $dotPos), null, $packageName);
} else {
$import = new Import(substr($parts[0], 0, $dotPos));

if (isset($parts[1])) {
// type with import not matching package name, e.g. "foo-go::foo.Symbol"
list($packageName, $typeName) = explode('.', $parts[1]);

$importPath = $parts[0];
if (false !== $dotPos = strrpos($importPath, '.')) {
if (substr($importPath, $dotPos + 1) === $typeName) {
$importPath = substr($importPath, 0, $dotPos);
}
}
$typeName = substr($parts[0], $dotPos + 1);
$import = new Import($importPath, null, $packageName);
} else {
$typeName = $parts[0];
if (false !== $dotPos = strrpos($parts[0], '.')) {
// type import matching package name, e.g. "encoding/json.Marshaler"
$typeName = substr($parts[0], $dotPos + 1);
$importPath = substr($parts[0], 0, $dotPos);
$import = new Import($importPath);
} else {
// builtin type, e.g. "string"
$typeName = $parts[0];
}
}

return new Type($typeName, $import);
Expand Down
8 changes: 8 additions & 0 deletions tests/src/PHPUnit/Type/TypeFromStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function testSlice()
$this->assertSame('nulltypes.NullString', $type->getType()->render());
}

public function testGoTypeSimplified()
{
/** @var Slice $type */
$type = TypeUtil::fromString('[]blabla.com/go-null-types::nulltypes.NullString');
$this->assertSame('[]nulltypes.NullString', $type->render());
$this->assertSame('nulltypes.NullString', $type->getType()->render());
}

public function testMapSlice()
{
/** @var Slice $type */
Expand Down

0 comments on commit 70cf318

Please sign in to comment.