Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added go json tags #120

Merged
merged 6 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/OutputGenerator/Go/GoOutputGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -49,17 +50,33 @@ 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);

$structProps .= sprintf(
"\n\t%s$spaces%s",
$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[] = [
'prop' => $structPropsRow,
'tags' => sprintf($tagsTemplate, $prop->getName()),
];
}

foreach ($normProps as $prop) {
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package gen

type A struct {
CreatedAt string
CreatedAt string `json:"createdAt"`
}

type GenderEnum int
Expand Down
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 `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"`
}
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 `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"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}
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 `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"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Loading