Skip to content

Commit

Permalink
fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
KoNekoD committed Apr 18, 2024
1 parent ab8b74a commit a9d8d3b
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 95 deletions.
11 changes: 9 additions & 2 deletions src/OutputGenerator/Go/GoEnumResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function resolve(DtoType $dto): string
}
$props = self::convertEnumToGoEnumProperties($dto->getProperties(), $dto->getName());

return sprintf("type %s %s \n\nconst(%s\n)", $dto->getName(), $type, $props);
return sprintf("type %s %s\n\nconst (%s\n)", $dto->getName(), $type, $props);
}

/**
Expand All @@ -42,6 +42,11 @@ private function convertEnumToGoEnumProperties(array $properties, string $enum):
{
$string = '';

$maxEnumPropNameLength = 0;
foreach ($properties as $prop) {
$maxEnumPropNameLength = max($maxEnumPropNameLength, strlen($prop->getName()));
}

foreach ($properties as $prop) {
$const = $prop->getName();
if (in_array($const, $this->usedConstantsStore)) {
Expand All @@ -52,8 +57,10 @@ private function convertEnumToGoEnumProperties(array $properties, string $enum):
}
$this->usedConstantsStore[] = $const;

$spaces = str_repeat(' ', $maxEnumPropNameLength - strlen($prop->getName()) + 1);

$value = $prop->isNumeric() ? $prop->getValue() : sprintf('"%s"', $prop->getValue());
$string .= sprintf("\n %s %s = %s", $const, $enum, $value);
$string .= sprintf("\n\t%s$spaces%s = %s", $const, $enum, $value);
}

return $string;
Expand Down
13 changes: 10 additions & 3 deletions src/OutputGenerator/Go/GoOutputGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Riverwaysoft\PhpConverter\OutputWriter\OutputProcessor\OutputFilesProcessor;
use Riverwaysoft\PhpConverter\OutputWriter\OutputWriterInterface;

class GoOutputGenerator implements OutputGeneratorInterface
class GoOutputGenerator
implements OutputGeneratorInterface
{
private OutputFilesProcessor $outputFilesProcessor;

Expand Down Expand Up @@ -48,9 +49,15 @@ private function convert(DtoType $dto, DtoList $dtoList): string
{
if ($dto->getExpressionType()->equals(ExpressionType::class())) {
$structProps = '';
$maxStructPropNameLength = 0;
foreach ($dto->getProperties() as $prop) {
$maxStructPropNameLength = max($maxStructPropNameLength, strlen($prop->getName()));
}
foreach ($dto->getProperties() as $prop) {
$spaces = str_repeat(' ', $maxStructPropNameLength - strlen($prop->getName()) + 1);

$structProps .= sprintf(
"\n %s %s",
"\n\t%s$spaces%s",
ucfirst($prop->getName()),
$this->resolver->resolve($prop->getType(), $dto, $dtoList)
);
Expand All @@ -61,6 +68,6 @@ private function convert(DtoType $dto, DtoList $dtoList): string
if ($dto->getExpressionType()->isAnyEnum()) {
return $this->enumResolver->resolve($dto);
}
throw new Exception('Unknown expression type ' . $dto->getExpressionType()->jsonSerialize());
throw new Exception('Unknown expression type '.$dto->getExpressionType()->jsonSerialize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
package gen

type A struct {
CreatedAt string
CreatedAt string
}

type GenderEnum int
type GenderEnum int

const(
UNKNOWN GenderEnum = 2
MAN GenderEnum = 0
WOMAN GenderEnum = 1
const (
UNKNOWN GenderEnum = 2
MAN GenderEnum = 0
WOMAN GenderEnum = 1
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
package gen

type FullName struct {
FirstName string
LastName string
FirstName string
LastName string
}

type Me struct {
Request *UserCreate
Request *UserCreate
}

type Profile struct {
Name *FullName
Age int
Name *FullName
Age int
}

type UserCreate struct {
Id string
Profile *Profile
Me *Me
Id string
Profile *Profile
Me *Me
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
package gen

type CloudNotify struct {
Id string
FcmToken *string
Id string
FcmToken *string
}

type Response struct {
Data interface{}
Data interface{}
}

type UserCreate struct {
Achievements []string
Matrix [][]int
Name *string
DuplicatesInType *string
Age int
IsApproved *bool
Latitude float64
Longitude float64
Mixed interface{}
Achievements []string
Matrix [][]int
Name *string
DuplicatesInType *string
Age int
IsApproved *bool
Latitude float64
Longitude float64
Mixed interface{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,54 @@
package gen

type Activity struct {
Id string
CreatedAt string
Id string
CreatedAt string
}

type FullName struct {
FirstName string
LastName string
FirstName string
LastName string
}

type NumberEnum int
type NumberEnum int

const(
VIEW NumberEnum = 0
EDIT NumberEnum = 1
CREATE NumberEnum = 2
const (
VIEW NumberEnum = 0
EDIT NumberEnum = 1
CREATE NumberEnum = 2
)

type PermissionsEnum string
type PermissionsEnum string

const(
VIEWPermissionsEnum PermissionsEnum = "view"
EDITPermissionsEnum PermissionsEnum = "edit"
const (
VIEWPermissionsEnum PermissionsEnum = "view"
EDITPermissionsEnum PermissionsEnum = "edit"
)

type Profile struct {
Name *FullName
Age int
Name *FullName
Age int
}

type User struct {
Id string
BestFriend *User
Friends []User
SelfProperty *User
SelfConstructor *User
Id string
BestFriend *User
Friends []User
SelfProperty *User
SelfConstructor *User
}

type UserCreate struct {
Id string
Permissions PermissionsEnum
Profile *Profile
Age int
Name *string
Latitude float64
Longitude float64
Achievements []interface{}
Tags []string
Activities []Activity
Mixed interface{}
IsApproved *bool
Id string
Permissions PermissionsEnum
Profile *Profile
Age int
Name *string
Latitude float64
Longitude float64
Achievements []interface{}
Tags []string
Activities []Activity
Mixed interface{}
IsApproved *bool
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
package gen

type UserCreate struct {
CreatedAt string
UpdatedAt string
PromotedAt *string
CreatedAt string
UpdatedAt string
PromotedAt *string
}

type UserCreateConstructor struct {
CreatedAt string
UpdatedAt string
PromotedAt *string
CreatedAt string
UpdatedAt string
PromotedAt *string
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

package gen

type Color int
type Color int

const(
RED Color = 0
BLUE Color = 1
WHITE Color = 2
const (
RED Color = 0
BLUE Color = 1
WHITE Color = 2
)

type Role string
type Role string

const(
ADMIN Role = "admin"
EDITOR Role = "editor"
READER Role = "reader"
const (
ADMIN Role = "admin"
EDITOR Role = "editor"
READER Role = "reader"
)

type User struct {
Color Color
User int
Role Role
Color Color
User int
Role Role
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

package gen

type ColorEnum int
type ColorEnum int

const(
RED ColorEnum = 0
GREEN ColorEnum = 1
BLUE ColorEnum = 2
const (
RED ColorEnum = 0
GREEN ColorEnum = 1
BLUE ColorEnum = 2
)

type RoleEnum string
type RoleEnum string

const(
ADMIN RoleEnum = "admin"
READER RoleEnum = "reader"
EDITOR RoleEnum = "editor"
const (
ADMIN RoleEnum = "admin"
READER RoleEnum = "reader"
EDITOR RoleEnum = "editor"
)

type User struct {
Id string
ThemeColor ColorEnum
Role RoleEnum
Id string
ThemeColor ColorEnum
Role RoleEnum
}

0 comments on commit a9d8d3b

Please sign in to comment.