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

fix enum already exist declaration #122

Merged
merged 1 commit into from
Apr 23, 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
17 changes: 10 additions & 7 deletions src/OutputGenerator/Go/GoEnumResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ private function convertEnumToGoEnumProperties(array $properties, string $enum):
$string = '';

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

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

$spaces = str_repeat(' ', $maxEnumPropNameLength - strlen($const) + 1);
$maxEnumPropNameLength = max($maxEnumPropNameLength, strlen($const));
$normProps[] = [
'const' => $const,
'value' => $prop->isNumeric() ? $prop->getValue() : sprintf('"%s"', $prop->getValue()),
];
}

$value = $prop->isNumeric() ? $prop->getValue() : sprintf('"%s"', $prop->getValue());
$string .= sprintf("\n\t%s$spaces%s = %s", $const, $enum, $value);
foreach ($normProps as $prop) {
$spaces = str_repeat(' ', $maxEnumPropNameLength - strlen($prop['const']) + 1);
$string .= sprintf("\n\t%s$spaces%s = %s", $prop['const'], $enum, $prop['value']);
}

return $string;
Expand Down
2 changes: 2 additions & 0 deletions tests/OutputGenerator/GoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ final class ColorEnum extends Enum
private const RED = 0;
private const GREEN = 1;
private const BLUE = 2;
private const UNIFIED_ENUM_CASE = 111;
}

final class RoleEnum extends Enum
{
private const ADMIN = 'admin';
private const READER = 'reader';
private const EDITOR = 'editor';
private const UNIFIED_ENUM_CASE = 'one_one_one';
}

class User
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading