From de8c96047c54bd8d239657755a9378a34c22d416 Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 11:47:09 +0300 Subject: [PATCH 1/6] added tags --- src/OutputGenerator/Go/GoOutputGenerator.php | 33 +++++++++++-- ...atorTest__testDtoConstantDoesntThrow__1.go | 2 +- ...oGeneratorTest__testNestedDtoConvert__1.go | 16 +++---- .../GoGeneratorTest__testNormalization__2.go | 24 +++++----- ...atorTest__testNormalizationDirectory__2.go | 46 +++++++++---------- ...NormalizationWithCustomTypeResolvers__1.go | 12 ++--- ...atorTest__testPhp81SuccessWhenBacked__1.go | 6 +-- .../GoGeneratorTest__testUseTypeEnum__1.go | 6 +-- 8 files changed, 85 insertions(+), 60 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index a3226a8..36e3905 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -9,12 +9,14 @@ use Riverwaysoft\PhpConverter\Dto\DtoList; use Riverwaysoft\PhpConverter\Dto\DtoType; use Riverwaysoft\PhpConverter\Dto\ExpressionType; +use Riverwaysoft\PhpConverter\Dto\PhpType\PhpOptionalType; use Riverwaysoft\PhpConverter\OutputGenerator\OutputGeneratorInterface; use Riverwaysoft\PhpConverter\OutputWriter\OutputFile; use Riverwaysoft\PhpConverter\OutputWriter\OutputProcessor\OutputFilesProcessor; use Riverwaysoft\PhpConverter\OutputWriter\OutputWriterInterface; -class GoOutputGenerator implements OutputGeneratorInterface +class GoOutputGenerator + implements OutputGeneratorInterface { private OutputFilesProcessor $outputFilesProcessor; @@ -48,18 +50,41 @@ 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())); } + + $normalizedProperties = []; foreach ($dto->getProperties() as $prop) { $spaces = str_repeat(' ', $maxStructPropNameLength - strlen($prop->getName()) + 1); - $structProps .= sprintf( - "\n\t%s$spaces%s", + $tagsTemplate = '`json:"%s"`'; + if ($prop->getType() instanceof PhpOptionalType) { + $tagsTemplate = '`json:"%s,omitempty"`'; + } + $tagsRow = sprintf($tagsTemplate, $prop->getName()); + + $structPropsRow = sprintf( + "%s$spaces%s", ucfirst($prop->getName()), $this->resolver->resolve($prop->getType(), $dto, $dtoList) ); + + $normalizedProperties[] = [ + 'structPropsRow' => $structPropsRow, + 'tagsRow' => $tagsRow, + ]; + } + + $maxStructPropLength = 0; + foreach ($normalizedProperties as $prop) { + $maxStructPropLength = max($maxStructPropLength, strlen($prop['structPropsRow'])); + } + foreach ($normalizedProperties as $prop) { + $tagsSpaces = str_repeat(' ', $maxStructPropLength - strlen($prop['structPropsRow']) + 1); + $structProps .= sprintf("\n\t%s$tagsSpaces%s", $prop['structPropsRow'], $prop['tagsRow']); } return sprintf("type %s struct {%s\n}", $dto->getName(), $structProps); @@ -67,6 +92,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()); } } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testDtoConstantDoesntThrow/GoGeneratorTest__testDtoConstantDoesntThrow__1.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testDtoConstantDoesntThrow/GoGeneratorTest__testDtoConstantDoesntThrow__1.go index 520e937..253d7e0 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testDtoConstantDoesntThrow/GoGeneratorTest__testDtoConstantDoesntThrow__1.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testDtoConstantDoesntThrow/GoGeneratorTest__testDtoConstantDoesntThrow__1.go @@ -5,7 +5,7 @@ package gen type A struct { - CreatedAt string + CreatedAt string `json:"createdAt"` } type GenderEnum int diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNestedDtoConvert/GoGeneratorTest__testNestedDtoConvert__1.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNestedDtoConvert/GoGeneratorTest__testNestedDtoConvert__1.go index 1e72a04..68314ce 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNestedDtoConvert/GoGeneratorTest__testNestedDtoConvert__1.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNestedDtoConvert/GoGeneratorTest__testNestedDtoConvert__1.go @@ -5,21 +5,21 @@ package gen type FullName struct { - FirstName string - LastName string + FirstName string `json:"firstName"` + LastName string `json:"lastName"` } type Me struct { - Request *UserCreate + Request *UserCreate `json:"request"` } type Profile struct { - Name *FullName - Age int + Name *FullName `json:"name"` + Age int `json:"age"` } type UserCreate struct { - Id string - Profile *Profile - Me *Me + Id string `json:"id"` + Profile *Profile `json:"profile"` + Me *Me `json:"me"` } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalization/GoGeneratorTest__testNormalization__2.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalization/GoGeneratorTest__testNormalization__2.go index 5081c99..e229778 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalization/GoGeneratorTest__testNormalization__2.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalization/GoGeneratorTest__testNormalization__2.go @@ -5,22 +5,22 @@ package gen type CloudNotify struct { - Id string - FcmToken *string + Id string `json:"id"` + FcmToken *string `json:"fcmToken"` } type Response struct { - Data interface{} + Data interface{} `json:"data"` } type UserCreate struct { - Achievements []string - Matrix [][]int - Name *string - DuplicatesInType *string - Age int - IsApproved *bool - Latitude float64 - Longitude float64 - Mixed interface{} + Achievements []string `json:"achievements"` + Matrix [][]int `json:"matrix"` + Name *string `json:"name"` + DuplicatesInType *string `json:"duplicatesInType"` + Age int `json:"age"` + IsApproved *bool `json:"isApproved"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + Mixed interface{} `json:"mixed"` } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationDirectory/GoGeneratorTest__testNormalizationDirectory__2.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationDirectory/GoGeneratorTest__testNormalizationDirectory__2.go index f4f8196..d2450d3 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationDirectory/GoGeneratorTest__testNormalizationDirectory__2.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationDirectory/GoGeneratorTest__testNormalizationDirectory__2.go @@ -5,13 +5,13 @@ package gen type Activity struct { - Id string - CreatedAt string + Id string `json:"id"` + CreatedAt string `json:"createdAt"` } type FullName struct { - FirstName string - LastName string + FirstName string `json:"firstName"` + LastName string `json:"lastName"` } type NumberEnum int @@ -30,29 +30,29 @@ const ( ) type Profile struct { - Name *FullName - Age int + Name *FullName `json:"name"` + Age int `json:"age"` } type User struct { - Id string - BestFriend *User - Friends []User - SelfProperty *User - SelfConstructor *User + Id string `json:"id"` + BestFriend *User `json:"bestFriend"` + Friends []User `json:"friends"` + SelfProperty *User `json:"selfProperty"` + SelfConstructor *User `json:"selfConstructor"` } 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 `json:"id"` + Permissions PermissionsEnum `json:"permissions"` + Profile *Profile `json:"profile"` + Age int `json:"age"` + Name *string `json:"name"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` + Achievements []interface{} `json:"achievements"` + Tags []string `json:"tags"` + Activities []Activity `json:"activities"` + Mixed interface{} `json:"mixed"` + IsApproved *bool `json:"isApproved"` } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationWithCustomTypeResolvers/GoGeneratorTest__testNormalizationWithCustomTypeResolvers__1.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationWithCustomTypeResolvers/GoGeneratorTest__testNormalizationWithCustomTypeResolvers__1.go index f43f8d6..5db5d77 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationWithCustomTypeResolvers/GoGeneratorTest__testNormalizationWithCustomTypeResolvers__1.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testNormalizationWithCustomTypeResolvers/GoGeneratorTest__testNormalizationWithCustomTypeResolvers__1.go @@ -5,13 +5,13 @@ package gen type UserCreate struct { - CreatedAt string - UpdatedAt string - PromotedAt *string + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + PromotedAt *string `json:"promotedAt"` } type UserCreateConstructor struct { - CreatedAt string - UpdatedAt string - PromotedAt *string + CreatedAt string `json:"createdAt"` + UpdatedAt string `json:"updatedAt"` + PromotedAt *string `json:"promotedAt"` } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testPhp81SuccessWhenBacked/GoGeneratorTest__testPhp81SuccessWhenBacked__1.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testPhp81SuccessWhenBacked/GoGeneratorTest__testPhp81SuccessWhenBacked__1.go index c3fe7f8..f01df0e 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testPhp81SuccessWhenBacked/GoGeneratorTest__testPhp81SuccessWhenBacked__1.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testPhp81SuccessWhenBacked/GoGeneratorTest__testPhp81SuccessWhenBacked__1.go @@ -21,7 +21,7 @@ const ( ) type User struct { - Color Color - User int - Role Role + Color Color `json:"color"` + User int `json:"user"` + Role Role `json:"role"` } diff --git a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testUseTypeEnum/GoGeneratorTest__testUseTypeEnum__1.go b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testUseTypeEnum/GoGeneratorTest__testUseTypeEnum__1.go index 51eeb4a..fb85c20 100644 --- a/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testUseTypeEnum/GoGeneratorTest__testUseTypeEnum__1.go +++ b/tests/OutputGenerator/__snapshots__/GoGeneratorTest/testUseTypeEnum/GoGeneratorTest__testUseTypeEnum__1.go @@ -21,7 +21,7 @@ const ( ) type User struct { - Id string - ThemeColor ColorEnum - Role RoleEnum + Id string `json:"id"` + ThemeColor ColorEnum `json:"themeColor"` + Role RoleEnum `json:"role"` } From 06daaa89a8175f4d286ea70f064ff451df4d944f Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 11:59:15 +0300 Subject: [PATCH 2/6] added tags --- src/OutputGenerator/Go/GoOutputGenerator.php | 25 ++++++++------------ 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index 36e3905..fdbd60b 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -51,39 +51,34 @@ private function convert(DtoType $dto, DtoList $dtoList): string if ($dto->getExpressionType()->equals(ExpressionType::class())) { $structProps = ''; - $maxStructPropNameLength = 0; + $maxPropLen = 0; + $maxPropRowLen = 0; foreach ($dto->getProperties() as $prop) { - $maxStructPropNameLength = max($maxStructPropNameLength, strlen($prop->getName())); + $maxPropLen = max($maxPropLen, strlen($prop->getName())); } - $normalizedProperties = []; + $normProps = []; foreach ($dto->getProperties() as $prop) { - $spaces = str_repeat(' ', $maxStructPropNameLength - strlen($prop->getName()) + 1); + $spaces = str_repeat(' ', $maxPropLen - strlen($prop->getName()) + 1); $tagsTemplate = '`json:"%s"`'; if ($prop->getType() instanceof PhpOptionalType) { $tagsTemplate = '`json:"%s,omitempty"`'; } - $tagsRow = sprintf($tagsTemplate, $prop->getName()); - $structPropsRow = sprintf( "%s$spaces%s", ucfirst($prop->getName()), $this->resolver->resolve($prop->getType(), $dto, $dtoList) ); - - $normalizedProperties[] = [ + $maxPropRowLen = max($maxPropRowLen, strlen($structPropsRow)); + $normProps[] = [ 'structPropsRow' => $structPropsRow, - 'tagsRow' => $tagsRow, + 'tagsRow' => sprintf($tagsTemplate, $prop->getName()), ]; } - $maxStructPropLength = 0; - foreach ($normalizedProperties as $prop) { - $maxStructPropLength = max($maxStructPropLength, strlen($prop['structPropsRow'])); - } - foreach ($normalizedProperties as $prop) { - $tagsSpaces = str_repeat(' ', $maxStructPropLength - strlen($prop['structPropsRow']) + 1); + foreach ($normProps as $prop) { + $tagsSpaces = str_repeat(' ', $maxPropRowLen - strlen($prop['structPropsRow']) + 1); $structProps .= sprintf("\n\t%s$tagsSpaces%s", $prop['structPropsRow'], $prop['tagsRow']); } From 693edc859713f676567190e927f82107062dbb38 Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 12:04:32 +0300 Subject: [PATCH 3/6] added tags --- src/OutputGenerator/Go/GoOutputGenerator.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index fdbd60b..aa41df8 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -51,15 +51,15 @@ private function convert(DtoType $dto, DtoList $dtoList): string if ($dto->getExpressionType()->equals(ExpressionType::class())) { $structProps = ''; - $maxPropLen = 0; - $maxPropRowLen = 0; + $maxStructPropNameLength = 0; + $maxStructPropNameAndTypeLength = 0; foreach ($dto->getProperties() as $prop) { - $maxPropLen = max($maxPropLen, strlen($prop->getName())); + $maxStructPropNameLength = max($maxStructPropNameLength, strlen($prop->getName())); } $normProps = []; foreach ($dto->getProperties() as $prop) { - $spaces = str_repeat(' ', $maxPropLen - strlen($prop->getName()) + 1); + $spaces = str_repeat(' ', $maxStructPropNameLength - strlen($prop->getName()) + 1); $tagsTemplate = '`json:"%s"`'; if ($prop->getType() instanceof PhpOptionalType) { @@ -70,7 +70,7 @@ private function convert(DtoType $dto, DtoList $dtoList): string ucfirst($prop->getName()), $this->resolver->resolve($prop->getType(), $dto, $dtoList) ); - $maxPropRowLen = max($maxPropRowLen, strlen($structPropsRow)); + $maxStructPropNameAndTypeLength = max($maxStructPropNameAndTypeLength, strlen($structPropsRow)); $normProps[] = [ 'structPropsRow' => $structPropsRow, 'tagsRow' => sprintf($tagsTemplate, $prop->getName()), @@ -78,7 +78,7 @@ private function convert(DtoType $dto, DtoList $dtoList): string } foreach ($normProps as $prop) { - $tagsSpaces = str_repeat(' ', $maxPropRowLen - strlen($prop['structPropsRow']) + 1); + $tagsSpaces = str_repeat(' ', $maxStructPropNameAndTypeLength - strlen($prop['structPropsRow']) + 1); $structProps .= sprintf("\n\t%s$tagsSpaces%s", $prop['structPropsRow'], $prop['tagsRow']); } From 2f1e7e8d27f3e4495880e92ee75b304a6d060aaa Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 12:05:09 +0300 Subject: [PATCH 4/6] fix cs --- src/OutputGenerator/Go/GoOutputGenerator.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index aa41df8..eb35962 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -15,8 +15,7 @@ use Riverwaysoft\PhpConverter\OutputWriter\OutputProcessor\OutputFilesProcessor; use Riverwaysoft\PhpConverter\OutputWriter\OutputWriterInterface; -class GoOutputGenerator - implements OutputGeneratorInterface +class GoOutputGenerator implements OutputGeneratorInterface { private OutputFilesProcessor $outputFilesProcessor; @@ -87,6 +86,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()); } } From ac902ec235308075ab7798626d8b142db5d1da01 Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 12:06:41 +0300 Subject: [PATCH 5/6] added tags --- src/OutputGenerator/Go/GoOutputGenerator.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index eb35962..5aa0242 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -49,27 +49,25 @@ private function convert(DtoType $dto, DtoList $dtoList): string { if ($dto->getExpressionType()->equals(ExpressionType::class())) { $structProps = ''; - $maxStructPropNameLength = 0; $maxStructPropNameAndTypeLength = 0; foreach ($dto->getProperties() as $prop) { $maxStructPropNameLength = max($maxStructPropNameLength, strlen($prop->getName())); } - $normProps = []; foreach ($dto->getProperties() as $prop) { $spaces = str_repeat(' ', $maxStructPropNameLength - strlen($prop->getName()) + 1); - $tagsTemplate = '`json:"%s"`'; - if ($prop->getType() instanceof PhpOptionalType) { - $tagsTemplate = '`json:"%s,omitempty"`'; - } $structPropsRow = sprintf( "%s$spaces%s", ucfirst($prop->getName()), $this->resolver->resolve($prop->getType(), $dto, $dtoList) ); $maxStructPropNameAndTypeLength = max($maxStructPropNameAndTypeLength, strlen($structPropsRow)); + $tagsTemplate = '`json:"%s"`'; + if ($prop->getType() instanceof PhpOptionalType) { + $tagsTemplate = '`json:"%s,omitempty"`'; + } $normProps[] = [ 'structPropsRow' => $structPropsRow, 'tagsRow' => sprintf($tagsTemplate, $prop->getName()), From 1b227eb2ee3499d74c4a8eeafcdd6d9854f53502 Mon Sep 17 00:00:00 2001 From: KoNekoD Date: Sun, 21 Apr 2024 12:10:48 +0300 Subject: [PATCH 6/6] added tags --- src/OutputGenerator/Go/GoOutputGenerator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OutputGenerator/Go/GoOutputGenerator.php b/src/OutputGenerator/Go/GoOutputGenerator.php index 5aa0242..f419bef 100644 --- a/src/OutputGenerator/Go/GoOutputGenerator.php +++ b/src/OutputGenerator/Go/GoOutputGenerator.php @@ -69,14 +69,14 @@ private function convert(DtoType $dto, DtoList $dtoList): string $tagsTemplate = '`json:"%s,omitempty"`'; } $normProps[] = [ - 'structPropsRow' => $structPropsRow, - 'tagsRow' => sprintf($tagsTemplate, $prop->getName()), + 'prop' => $structPropsRow, + 'tags' => sprintf($tagsTemplate, $prop->getName()), ]; } foreach ($normProps as $prop) { - $tagsSpaces = str_repeat(' ', $maxStructPropNameAndTypeLength - strlen($prop['structPropsRow']) + 1); - $structProps .= sprintf("\n\t%s$tagsSpaces%s", $prop['structPropsRow'], $prop['tagsRow']); + $tagsSpaces = str_repeat(' ', $maxStructPropNameAndTypeLength - strlen($prop['prop']) + 1); + $structProps .= sprintf("\n\t%s$tagsSpaces%s", $prop['prop'], $prop['tags']); } return sprintf("type %s struct {%s\n}", $dto->getName(), $structProps);