From a9c3385973f36538a0267d4d15ccd22342b1444b Mon Sep 17 00:00:00 2001 From: Markus Reinhold Date: Fri, 5 Dec 2025 23:56:29 +0100 Subject: [PATCH 1/6] Generate message type enums --- bin/01-post-build | 11 +++ bin/fixGoPaths | 8 -- bin/generatePhpEnums | 91 +++++++++++++++++++ php/GamingPlatform/Api/Chat/V1/ChatV1Type.php | 14 +++ .../Api/Common/V1/CommonV1Type.php | 12 +++ .../Api/Identity/V1/IdentityV1Type.php | 18 ++++ project | 3 +- 7 files changed, 147 insertions(+), 10 deletions(-) create mode 100755 bin/01-post-build delete mode 100755 bin/fixGoPaths create mode 100755 bin/generatePhpEnums create mode 100644 php/GamingPlatform/Api/Chat/V1/ChatV1Type.php create mode 100644 php/GamingPlatform/Api/Common/V1/CommonV1Type.php create mode 100644 php/GamingPlatform/Api/Identity/V1/IdentityV1Type.php diff --git a/bin/01-post-build b/bin/01-post-build new file mode 100755 index 0000000..f84909e --- /dev/null +++ b/bin/01-post-build @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -e + +# Go files are generated into their full module path. +# This script moves the files to the correct location. +cp -r /project/go/github.com/gaming-platform/api/go/* /project/go +rm -rf /project/go/github.com + +/project/bin/generatePhpEnums +/project/bin/generatePhpFactories diff --git a/bin/fixGoPaths b/bin/fixGoPaths deleted file mode 100755 index 1d741ee..0000000 --- a/bin/fixGoPaths +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -e - -# Go files are generated into their full module path. -# This script moves the files to the correct location. -cp -r ./go/github.com/gaming-platform/api/go/* ./go -rm -rf ./go/github.com diff --git a/bin/generatePhpEnums b/bin/generatePhpEnums new file mode 100755 index 0000000..f8295a6 --- /dev/null +++ b/bin/generatePhpEnums @@ -0,0 +1,91 @@ +#!/usr/bin/env php + str_starts_with($class, $packageNamespace) + && in_array(\Google\Protobuf\Internal\Message::class, class_parents($class)) + ); +} + +function generateEnums(string $packageNamespace, string $packagePath, array $classes): array +{ + $enums = []; + + foreach ($classes as $class) { + preg_match( + '/(?.*)\\\\(?V\d+)\\\\(?.*)/', + substr($class, strlen($packageNamespace) + 1), + $matches + ); + + $message = $matches['message']; + if (str_contains($matches['message'], '\\')) { + continue; // Ignore nested types. + } + + $enumName = str_replace('\\', '', $matches['namespace']) . $matches['version'] . 'Type'; + $enumPath = str_replace('\\', '/', $matches['namespace']) . '/' . $matches['version']; + $caseName = $message; + $caseValue = $matches['namespace'] . '.' . $message . '.' . strtolower($matches['version']); + + $enums[$enumName] ??= [ + 'path' => $packagePath . '/' . $enumPath . '/' . $enumName . '.php', + 'namespace' => $packageNamespace . '\\' . $matches['namespace'] . '\\' . $matches['version'], + 'name' => $enumName, + 'cases' => [] + ]; + + $enums[$enumName]['cases'][] = [ + 'name' => $caseName, + 'value' => $caseValue + ]; + } + + return $enums; +} + +function writeEnums(array $enums): void +{ + foreach ($enums as $enum) { + $content = ' Date: Sat, 6 Dec 2025 21:46:20 +0100 Subject: [PATCH 2/6] Generate new PHP and Go files * Group PHP type names and factories in the same class. * Generate Go type names. * Deprecate legacy factories. --- bin/01-post-build | 4 +- bin/generateAdditionalLanguageFeatures | 101 ++++++++++++++++++ ...ctories => generateLegacyLanguageFeatures} | 2 + bin/generatePhpEnums | 91 ---------------- go/chat/v1/gamingplatform.go | 7 ++ go/common/v1/gamingplatform.go | 5 + go/identity/v1/gamingplatform.go | 11 ++ php/GamingPlatform/Api/Chat/V1/ChatV1.php | 41 +++++++ .../Api/Chat/V1/ChatV1Factory.php | 4 + php/GamingPlatform/Api/Chat/V1/ChatV1Type.php | 14 --- php/GamingPlatform/Api/Common/V1/CommonV1.php | 21 ++++ .../Api/Common/V1/CommonV1Factory.php | 4 + .../Api/Common/V1/CommonV1Type.php | 12 --- .../Api/Identity/V1/IdentityV1.php | 81 ++++++++++++++ .../Api/Identity/V1/IdentityV1Factory.php | 8 ++ .../Api/Identity/V1/IdentityV1Type.php | 18 ---- 16 files changed, 287 insertions(+), 137 deletions(-) create mode 100755 bin/generateAdditionalLanguageFeatures rename bin/{generatePhpFactories => generateLegacyLanguageFeatures} (96%) delete mode 100755 bin/generatePhpEnums create mode 100644 go/chat/v1/gamingplatform.go create mode 100644 go/common/v1/gamingplatform.go create mode 100644 go/identity/v1/gamingplatform.go create mode 100644 php/GamingPlatform/Api/Chat/V1/ChatV1.php delete mode 100644 php/GamingPlatform/Api/Chat/V1/ChatV1Type.php create mode 100644 php/GamingPlatform/Api/Common/V1/CommonV1.php delete mode 100644 php/GamingPlatform/Api/Common/V1/CommonV1Type.php create mode 100644 php/GamingPlatform/Api/Identity/V1/IdentityV1.php delete mode 100644 php/GamingPlatform/Api/Identity/V1/IdentityV1Type.php diff --git a/bin/01-post-build b/bin/01-post-build index f84909e..b9f9fb5 100755 --- a/bin/01-post-build +++ b/bin/01-post-build @@ -7,5 +7,5 @@ set -e cp -r /project/go/github.com/gaming-platform/api/go/* /project/go rm -rf /project/go/github.com -/project/bin/generatePhpEnums -/project/bin/generatePhpFactories +/project/bin/generateAdditionalLanguageFeatures +/project/bin/generateLegacyLanguageFeatures diff --git a/bin/generateAdditionalLanguageFeatures b/bin/generateAdditionalLanguageFeatures new file mode 100755 index 0000000..2ba2cea --- /dev/null +++ b/bin/generateAdditionalLanguageFeatures @@ -0,0 +1,101 @@ +#!/usr/bin/env php + str_starts_with($class, PHP_PACKAGE_NAMESPACE) + && in_array(\Google\Protobuf\Internal\Message::class, class_parents($class)) + ); + $domains = []; + + foreach ($definedMessages as $message) { + preg_match( + '/(?.*)\\\\(?V\d+)\\\\(?.*)/', + substr($message, strlen(PHP_PACKAGE_NAMESPACE) + 1), + $matches + ); + + if (str_contains($matches['message'], '\\')) { + continue; // Ignore nested types. + } + + $domainKey = $matches['domain'] . $matches['version']; + $domains[$domainKey] ??= ['name' => $matches['domain'], 'version' => $matches['version'], 'messages' => []]; + $domains[$domainKey]['messages'][] = [ + 'name' => $matches['message'], + 'type' => $matches['domain'] . '.' . $matches['message'] . '.' . strtolower($matches['version']) + ]; + } + + return $domains; +} + +function writePhpClasses(array $domains): void +{ + foreach ($domains as $domain) { + $path = PHP_PACKAGE_PATH . '/' . $domain['name'] . '/' . $domain['version'] . '/' . $domain['name'] . $domain['version'] . '.php'; + $namespace = PHP_PACKAGE_NAMESPACE . '\\' . $domain['name'] . '\\' . $domain['version']; + + $content = 'mergeFromString($data);' . PHP_EOL; + $content .= ' return $message;' . PHP_EOL; + $content .= ' }' . PHP_EOL; + } + $content .= '}' . PHP_EOL; + + echo 'Write ' . $path . PHP_EOL; + file_put_contents($path, $content); + } +} + +function writeGoFile(array $domains): void +{ + foreach ($domains as $domain) { + $path = GO_PACKAGE_PATH . '/' . strtolower($domain['name'] . '/' . $domain['version']) . '/gamingplatform.go'; + + $content = 'package ' . $domain['name'] . $domain['version'] . PHP_EOL . PHP_EOL; + $content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL; + foreach ($domain['messages'] as $message) { + $content .= 'const ' . $message['name'] . 'Type = "' . $message['type'] . '"' . PHP_EOL; + } + + echo 'Write ' . $path . PHP_EOL; + file_put_contents($path, $content); + } +} + +loadFiles(PHP_PACKAGE_PATH); + +$rootMessagesClasses = findRootMessagesClasses(); +writePhpClasses($rootMessagesClasses); +writeGoFile($rootMessagesClasses); diff --git a/bin/generatePhpFactories b/bin/generateLegacyLanguageFeatures similarity index 96% rename from bin/generatePhpFactories rename to bin/generateLegacyLanguageFeatures index 4217342..c128251 100755 --- a/bin/generatePhpFactories +++ b/bin/generateLegacyLanguageFeatures @@ -62,9 +62,11 @@ function writeFactories(array $factories): void $content .= 'declare(strict_types=1);' . PHP_EOL . PHP_EOL; $content .= 'namespace ' . $factory['namespace'] . ';' . PHP_EOL . PHP_EOL; $content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL; + $content .= '/** @deprecated */' . PHP_EOL; $content .= 'final class ' . $factory['name'] . PHP_EOL; $content .= '{' . PHP_EOL; foreach ($factory['methods'] as $method) { + $content .= ' /** @deprecated */' . PHP_EOL; $content .= ' public static function ' . $method['name'] . '(' . PHP_EOL; $content .= ' string $data' . PHP_EOL; $content .= ' ): ' . $method['returnType'] . ' {' . PHP_EOL; diff --git a/bin/generatePhpEnums b/bin/generatePhpEnums deleted file mode 100755 index f8295a6..0000000 --- a/bin/generatePhpEnums +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env php - str_starts_with($class, $packageNamespace) - && in_array(\Google\Protobuf\Internal\Message::class, class_parents($class)) - ); -} - -function generateEnums(string $packageNamespace, string $packagePath, array $classes): array -{ - $enums = []; - - foreach ($classes as $class) { - preg_match( - '/(?.*)\\\\(?V\d+)\\\\(?.*)/', - substr($class, strlen($packageNamespace) + 1), - $matches - ); - - $message = $matches['message']; - if (str_contains($matches['message'], '\\')) { - continue; // Ignore nested types. - } - - $enumName = str_replace('\\', '', $matches['namespace']) . $matches['version'] . 'Type'; - $enumPath = str_replace('\\', '/', $matches['namespace']) . '/' . $matches['version']; - $caseName = $message; - $caseValue = $matches['namespace'] . '.' . $message . '.' . strtolower($matches['version']); - - $enums[$enumName] ??= [ - 'path' => $packagePath . '/' . $enumPath . '/' . $enumName . '.php', - 'namespace' => $packageNamespace . '\\' . $matches['namespace'] . '\\' . $matches['version'], - 'name' => $enumName, - 'cases' => [] - ]; - - $enums[$enumName]['cases'][] = [ - 'name' => $caseName, - 'value' => $caseValue - ]; - } - - return $enums; -} - -function writeEnums(array $enums): void -{ - foreach ($enums as $enum) { - $content = 'mergeFromString($data); + return $message; + } + public const string InitiateChatResponse = 'Chat.InitiateChatResponse.v1'; + public static function createInitiateChatResponse( + string $data + ): InitiateChatResponse { + static $template; + $template ??= new InitiateChatResponse(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string MessageWritten = 'Chat.MessageWritten.v1'; + public static function createMessageWritten( + string $data + ): MessageWritten { + static $template; + $template ??= new MessageWritten(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } +} diff --git a/php/GamingPlatform/Api/Chat/V1/ChatV1Factory.php b/php/GamingPlatform/Api/Chat/V1/ChatV1Factory.php index c1320b6..5419e3b 100644 --- a/php/GamingPlatform/Api/Chat/V1/ChatV1Factory.php +++ b/php/GamingPlatform/Api/Chat/V1/ChatV1Factory.php @@ -6,8 +6,10 @@ // This file is auto-generated. Do not edit! +/** @deprecated */ final class ChatV1Factory { + /** @deprecated */ public static function createInitiateChat( string $data ): \GamingPlatform\Api\Chat\V1\InitiateChat { @@ -18,6 +20,7 @@ public static function createInitiateChat( return $message; } + /** @deprecated */ public static function createInitiateChatResponse( string $data ): \GamingPlatform\Api\Chat\V1\InitiateChatResponse { @@ -28,6 +31,7 @@ public static function createInitiateChatResponse( return $message; } + /** @deprecated */ public static function createMessageWritten( string $data ): \GamingPlatform\Api\Chat\V1\MessageWritten { diff --git a/php/GamingPlatform/Api/Chat/V1/ChatV1Type.php b/php/GamingPlatform/Api/Chat/V1/ChatV1Type.php deleted file mode 100644 index 0060830..0000000 --- a/php/GamingPlatform/Api/Chat/V1/ChatV1Type.php +++ /dev/null @@ -1,14 +0,0 @@ -mergeFromString($data); + return $message; + } +} diff --git a/php/GamingPlatform/Api/Common/V1/CommonV1Factory.php b/php/GamingPlatform/Api/Common/V1/CommonV1Factory.php index 68b9c79..3a01ea5 100644 --- a/php/GamingPlatform/Api/Common/V1/CommonV1Factory.php +++ b/php/GamingPlatform/Api/Common/V1/CommonV1Factory.php @@ -6,8 +6,10 @@ // This file is auto-generated. Do not edit! +/** @deprecated */ final class CommonV1Factory { + /** @deprecated */ public static function createErrorResponse_Violation_Parameter( string $data ): \GamingPlatform\Api\Common\V1\ErrorResponse\Violation\Parameter { @@ -18,6 +20,7 @@ public static function createErrorResponse_Violation_Parameter( return $message; } + /** @deprecated */ public static function createErrorResponse_Violation( string $data ): \GamingPlatform\Api\Common\V1\ErrorResponse\Violation { @@ -28,6 +31,7 @@ public static function createErrorResponse_Violation( return $message; } + /** @deprecated */ public static function createErrorResponse( string $data ): \GamingPlatform\Api\Common\V1\ErrorResponse { diff --git a/php/GamingPlatform/Api/Common/V1/CommonV1Type.php b/php/GamingPlatform/Api/Common/V1/CommonV1Type.php deleted file mode 100644 index 43decbf..0000000 --- a/php/GamingPlatform/Api/Common/V1/CommonV1Type.php +++ /dev/null @@ -1,12 +0,0 @@ -mergeFromString($data); + return $message; + } + public const string GetBotByUsername = 'Identity.GetBotByUsername.v1'; + public static function createGetBotByUsername( + string $data + ): GetBotByUsername { + static $template; + $template ??= new GetBotByUsername(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string GetBotByUsernameResponse = 'Identity.GetBotByUsernameResponse.v1'; + public static function createGetBotByUsernameResponse( + string $data + ): GetBotByUsernameResponse { + static $template; + $template ??= new GetBotByUsernameResponse(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string RegisterBot = 'Identity.RegisterBot.v1'; + public static function createRegisterBot( + string $data + ): RegisterBot { + static $template; + $template ??= new RegisterBot(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string RegisterBotResponse = 'Identity.RegisterBotResponse.v1'; + public static function createRegisterBotResponse( + string $data + ): RegisterBotResponse { + static $template; + $template ??= new RegisterBotResponse(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string UserArrived = 'Identity.UserArrived.v1'; + public static function createUserArrived( + string $data + ): UserArrived { + static $template; + $template ??= new UserArrived(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } + public const string UserSignedUp = 'Identity.UserSignedUp.v1'; + public static function createUserSignedUp( + string $data + ): UserSignedUp { + static $template; + $template ??= new UserSignedUp(); + $message = clone $template; + $message->mergeFromString($data); + return $message; + } +} diff --git a/php/GamingPlatform/Api/Identity/V1/IdentityV1Factory.php b/php/GamingPlatform/Api/Identity/V1/IdentityV1Factory.php index b164f73..bcf4b54 100644 --- a/php/GamingPlatform/Api/Identity/V1/IdentityV1Factory.php +++ b/php/GamingPlatform/Api/Identity/V1/IdentityV1Factory.php @@ -6,8 +6,10 @@ // This file is auto-generated. Do not edit! +/** @deprecated */ final class IdentityV1Factory { + /** @deprecated */ public static function createBot( string $data ): \GamingPlatform\Api\Identity\V1\Bot { @@ -18,6 +20,7 @@ public static function createBot( return $message; } + /** @deprecated */ public static function createGetBotByUsername( string $data ): \GamingPlatform\Api\Identity\V1\GetBotByUsername { @@ -28,6 +31,7 @@ public static function createGetBotByUsername( return $message; } + /** @deprecated */ public static function createGetBotByUsernameResponse( string $data ): \GamingPlatform\Api\Identity\V1\GetBotByUsernameResponse { @@ -38,6 +42,7 @@ public static function createGetBotByUsernameResponse( return $message; } + /** @deprecated */ public static function createRegisterBot( string $data ): \GamingPlatform\Api\Identity\V1\RegisterBot { @@ -48,6 +53,7 @@ public static function createRegisterBot( return $message; } + /** @deprecated */ public static function createRegisterBotResponse( string $data ): \GamingPlatform\Api\Identity\V1\RegisterBotResponse { @@ -58,6 +64,7 @@ public static function createRegisterBotResponse( return $message; } + /** @deprecated */ public static function createUserArrived( string $data ): \GamingPlatform\Api\Identity\V1\UserArrived { @@ -68,6 +75,7 @@ public static function createUserArrived( return $message; } + /** @deprecated */ public static function createUserSignedUp( string $data ): \GamingPlatform\Api\Identity\V1\UserSignedUp { diff --git a/php/GamingPlatform/Api/Identity/V1/IdentityV1Type.php b/php/GamingPlatform/Api/Identity/V1/IdentityV1Type.php deleted file mode 100644 index 2a2b381..0000000 --- a/php/GamingPlatform/Api/Identity/V1/IdentityV1Type.php +++ /dev/null @@ -1,18 +0,0 @@ - Date: Sun, 7 Dec 2025 14:53:41 +0100 Subject: [PATCH 3/6] Align namings for constants --- bin/generateAdditionalLanguageFeatures | 2 +- php/GamingPlatform/Api/Chat/V1/ChatV1.php | 6 +++--- php/GamingPlatform/Api/Common/V1/CommonV1.php | 2 +- php/GamingPlatform/Api/Identity/V1/IdentityV1.php | 14 +++++++------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bin/generateAdditionalLanguageFeatures b/bin/generateAdditionalLanguageFeatures index 2ba2cea..4f077a9 100755 --- a/bin/generateAdditionalLanguageFeatures +++ b/bin/generateAdditionalLanguageFeatures @@ -60,7 +60,7 @@ function writePhpClasses(array $domains): void $content .= 'final class ' . $domain['name'] . $domain['version'] . PHP_EOL; $content .= '{' . PHP_EOL; foreach ($domain['messages'] as $message) { - $content .= ' public const string ' . $message['name'] . ' = \'' . $message['type'] . '\';' . PHP_EOL; + $content .= ' public const string ' . $message['name'] . 'Type = \'' . $message['type'] . '\';' . PHP_EOL; $content .= ' public static function create' . $message['name'] . '(' . PHP_EOL; $content .= ' string $data' . PHP_EOL; $content .= ' ): ' . $message['name'] . ' {' . PHP_EOL; diff --git a/php/GamingPlatform/Api/Chat/V1/ChatV1.php b/php/GamingPlatform/Api/Chat/V1/ChatV1.php index 8152022..f6976e1 100644 --- a/php/GamingPlatform/Api/Chat/V1/ChatV1.php +++ b/php/GamingPlatform/Api/Chat/V1/ChatV1.php @@ -8,7 +8,7 @@ final class ChatV1 { - public const string InitiateChat = 'Chat.InitiateChat.v1'; + public const string InitiateChatType = 'Chat.InitiateChat.v1'; public static function createInitiateChat( string $data ): InitiateChat { @@ -18,7 +18,7 @@ public static function createInitiateChat( $message->mergeFromString($data); return $message; } - public const string InitiateChatResponse = 'Chat.InitiateChatResponse.v1'; + public const string InitiateChatResponseType = 'Chat.InitiateChatResponse.v1'; public static function createInitiateChatResponse( string $data ): InitiateChatResponse { @@ -28,7 +28,7 @@ public static function createInitiateChatResponse( $message->mergeFromString($data); return $message; } - public const string MessageWritten = 'Chat.MessageWritten.v1'; + public const string MessageWrittenType = 'Chat.MessageWritten.v1'; public static function createMessageWritten( string $data ): MessageWritten { diff --git a/php/GamingPlatform/Api/Common/V1/CommonV1.php b/php/GamingPlatform/Api/Common/V1/CommonV1.php index ce35ef9..d96486d 100644 --- a/php/GamingPlatform/Api/Common/V1/CommonV1.php +++ b/php/GamingPlatform/Api/Common/V1/CommonV1.php @@ -8,7 +8,7 @@ final class CommonV1 { - public const string ErrorResponse = 'Common.ErrorResponse.v1'; + public const string ErrorResponseType = 'Common.ErrorResponse.v1'; public static function createErrorResponse( string $data ): ErrorResponse { diff --git a/php/GamingPlatform/Api/Identity/V1/IdentityV1.php b/php/GamingPlatform/Api/Identity/V1/IdentityV1.php index 20fb110..32e29dd 100644 --- a/php/GamingPlatform/Api/Identity/V1/IdentityV1.php +++ b/php/GamingPlatform/Api/Identity/V1/IdentityV1.php @@ -8,7 +8,7 @@ final class IdentityV1 { - public const string Bot = 'Identity.Bot.v1'; + public const string BotType = 'Identity.Bot.v1'; public static function createBot( string $data ): Bot { @@ -18,7 +18,7 @@ public static function createBot( $message->mergeFromString($data); return $message; } - public const string GetBotByUsername = 'Identity.GetBotByUsername.v1'; + public const string GetBotByUsernameType = 'Identity.GetBotByUsername.v1'; public static function createGetBotByUsername( string $data ): GetBotByUsername { @@ -28,7 +28,7 @@ public static function createGetBotByUsername( $message->mergeFromString($data); return $message; } - public const string GetBotByUsernameResponse = 'Identity.GetBotByUsernameResponse.v1'; + public const string GetBotByUsernameResponseType = 'Identity.GetBotByUsernameResponse.v1'; public static function createGetBotByUsernameResponse( string $data ): GetBotByUsernameResponse { @@ -38,7 +38,7 @@ public static function createGetBotByUsernameResponse( $message->mergeFromString($data); return $message; } - public const string RegisterBot = 'Identity.RegisterBot.v1'; + public const string RegisterBotType = 'Identity.RegisterBot.v1'; public static function createRegisterBot( string $data ): RegisterBot { @@ -48,7 +48,7 @@ public static function createRegisterBot( $message->mergeFromString($data); return $message; } - public const string RegisterBotResponse = 'Identity.RegisterBotResponse.v1'; + public const string RegisterBotResponseType = 'Identity.RegisterBotResponse.v1'; public static function createRegisterBotResponse( string $data ): RegisterBotResponse { @@ -58,7 +58,7 @@ public static function createRegisterBotResponse( $message->mergeFromString($data); return $message; } - public const string UserArrived = 'Identity.UserArrived.v1'; + public const string UserArrivedType = 'Identity.UserArrived.v1'; public static function createUserArrived( string $data ): UserArrived { @@ -68,7 +68,7 @@ public static function createUserArrived( $message->mergeFromString($data); return $message; } - public const string UserSignedUp = 'Identity.UserSignedUp.v1'; + public const string UserSignedUpType = 'Identity.UserSignedUp.v1'; public static function createUserSignedUp( string $data ): UserSignedUp { From 8f3f1f0e2995339846d13b8acec84be89e1e8b26 Mon Sep 17 00:00:00 2001 From: Markus Reinhold Date: Sun, 7 Dec 2025 15:01:30 +0100 Subject: [PATCH 4/6] Allow to use factory methods without data --- bin/generateAdditionalLanguageFeatures | 8 +++--- php/GamingPlatform/Api/Chat/V1/ChatV1.php | 12 ++++---- php/GamingPlatform/Api/Common/V1/CommonV1.php | 4 +-- .../Api/Identity/V1/IdentityV1.php | 28 +++++++++---------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/bin/generateAdditionalLanguageFeatures b/bin/generateAdditionalLanguageFeatures index 4f077a9..3bba182 100755 --- a/bin/generateAdditionalLanguageFeatures +++ b/bin/generateAdditionalLanguageFeatures @@ -47,7 +47,7 @@ function findRootMessagesClasses(): array return $domains; } -function writePhpClasses(array $domains): void +function writePhpFiles(array $domains): void { foreach ($domains as $domain) { $path = PHP_PACKAGE_PATH . '/' . $domain['name'] . '/' . $domain['version'] . '/' . $domain['name'] . $domain['version'] . '.php'; @@ -62,12 +62,12 @@ function writePhpClasses(array $domains): void foreach ($domain['messages'] as $message) { $content .= ' public const string ' . $message['name'] . 'Type = \'' . $message['type'] . '\';' . PHP_EOL; $content .= ' public static function create' . $message['name'] . '(' . PHP_EOL; - $content .= ' string $data' . PHP_EOL; + $content .= ' ?string $data = null' . PHP_EOL; $content .= ' ): ' . $message['name'] . ' {' . PHP_EOL; $content .= ' static $template;' . PHP_EOL; $content .= ' $template ??= new ' . $message['name'] . '();' . PHP_EOL; $content .= ' $message = clone $template;' . PHP_EOL; - $content .= ' $message->mergeFromString($data);' . PHP_EOL; + $content .= ' if ($data !== null) $message->mergeFromString($data);' . PHP_EOL; $content .= ' return $message;' . PHP_EOL; $content .= ' }' . PHP_EOL; } @@ -97,5 +97,5 @@ function writeGoFile(array $domains): void loadFiles(PHP_PACKAGE_PATH); $rootMessagesClasses = findRootMessagesClasses(); -writePhpClasses($rootMessagesClasses); +writePhpFiles($rootMessagesClasses); writeGoFile($rootMessagesClasses); diff --git a/php/GamingPlatform/Api/Chat/V1/ChatV1.php b/php/GamingPlatform/Api/Chat/V1/ChatV1.php index f6976e1..fc6d18e 100644 --- a/php/GamingPlatform/Api/Chat/V1/ChatV1.php +++ b/php/GamingPlatform/Api/Chat/V1/ChatV1.php @@ -10,32 +10,32 @@ final class ChatV1 { public const string InitiateChatType = 'Chat.InitiateChat.v1'; public static function createInitiateChat( - string $data + ?string $data = null ): InitiateChat { static $template; $template ??= new InitiateChat(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string InitiateChatResponseType = 'Chat.InitiateChatResponse.v1'; public static function createInitiateChatResponse( - string $data + ?string $data = null ): InitiateChatResponse { static $template; $template ??= new InitiateChatResponse(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string MessageWrittenType = 'Chat.MessageWritten.v1'; public static function createMessageWritten( - string $data + ?string $data = null ): MessageWritten { static $template; $template ??= new MessageWritten(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } } diff --git a/php/GamingPlatform/Api/Common/V1/CommonV1.php b/php/GamingPlatform/Api/Common/V1/CommonV1.php index d96486d..f8adf20 100644 --- a/php/GamingPlatform/Api/Common/V1/CommonV1.php +++ b/php/GamingPlatform/Api/Common/V1/CommonV1.php @@ -10,12 +10,12 @@ final class CommonV1 { public const string ErrorResponseType = 'Common.ErrorResponse.v1'; public static function createErrorResponse( - string $data + ?string $data = null ): ErrorResponse { static $template; $template ??= new ErrorResponse(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } } diff --git a/php/GamingPlatform/Api/Identity/V1/IdentityV1.php b/php/GamingPlatform/Api/Identity/V1/IdentityV1.php index 32e29dd..d3f44a1 100644 --- a/php/GamingPlatform/Api/Identity/V1/IdentityV1.php +++ b/php/GamingPlatform/Api/Identity/V1/IdentityV1.php @@ -10,72 +10,72 @@ final class IdentityV1 { public const string BotType = 'Identity.Bot.v1'; public static function createBot( - string $data + ?string $data = null ): Bot { static $template; $template ??= new Bot(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string GetBotByUsernameType = 'Identity.GetBotByUsername.v1'; public static function createGetBotByUsername( - string $data + ?string $data = null ): GetBotByUsername { static $template; $template ??= new GetBotByUsername(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string GetBotByUsernameResponseType = 'Identity.GetBotByUsernameResponse.v1'; public static function createGetBotByUsernameResponse( - string $data + ?string $data = null ): GetBotByUsernameResponse { static $template; $template ??= new GetBotByUsernameResponse(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string RegisterBotType = 'Identity.RegisterBot.v1'; public static function createRegisterBot( - string $data + ?string $data = null ): RegisterBot { static $template; $template ??= new RegisterBot(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string RegisterBotResponseType = 'Identity.RegisterBotResponse.v1'; public static function createRegisterBotResponse( - string $data + ?string $data = null ): RegisterBotResponse { static $template; $template ??= new RegisterBotResponse(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string UserArrivedType = 'Identity.UserArrived.v1'; public static function createUserArrived( - string $data + ?string $data = null ): UserArrived { static $template; $template ??= new UserArrived(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } public const string UserSignedUpType = 'Identity.UserSignedUp.v1'; public static function createUserSignedUp( - string $data + ?string $data = null ): UserSignedUp { static $template; $template ??= new UserSignedUp(); $message = clone $template; - $message->mergeFromString($data); + if ($data !== null) $message->mergeFromString($data); return $message; } } From 032941df82713c4df1c38cc1fae66d9f82dc7d27 Mon Sep 17 00:00:00 2001 From: Markus Reinhold Date: Sun, 7 Dec 2025 19:05:16 +0100 Subject: [PATCH 5/6] Use correct Go package names --- bin/generateAdditionalLanguageFeatures | 2 +- go/chat/v1/gamingplatform.go | 2 +- go/common/v1/gamingplatform.go | 2 +- go/identity/v1/gamingplatform.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/generateAdditionalLanguageFeatures b/bin/generateAdditionalLanguageFeatures index 3bba182..89ceb88 100755 --- a/bin/generateAdditionalLanguageFeatures +++ b/bin/generateAdditionalLanguageFeatures @@ -83,7 +83,7 @@ function writeGoFile(array $domains): void foreach ($domains as $domain) { $path = GO_PACKAGE_PATH . '/' . strtolower($domain['name'] . '/' . $domain['version']) . '/gamingplatform.go'; - $content = 'package ' . $domain['name'] . $domain['version'] . PHP_EOL . PHP_EOL; + $content = 'package ' . strtolower($domain['name'] . $domain['version']) . PHP_EOL . PHP_EOL; $content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL; foreach ($domain['messages'] as $message) { $content .= 'const ' . $message['name'] . 'Type = "' . $message['type'] . '"' . PHP_EOL; diff --git a/go/chat/v1/gamingplatform.go b/go/chat/v1/gamingplatform.go index 636d83b..afb83a4 100644 --- a/go/chat/v1/gamingplatform.go +++ b/go/chat/v1/gamingplatform.go @@ -1,4 +1,4 @@ -package ChatV1 +package chatv1 // This file is auto-generated. Do not edit! diff --git a/go/common/v1/gamingplatform.go b/go/common/v1/gamingplatform.go index 94693f0..f1c50c5 100644 --- a/go/common/v1/gamingplatform.go +++ b/go/common/v1/gamingplatform.go @@ -1,4 +1,4 @@ -package CommonV1 +package commonv1 // This file is auto-generated. Do not edit! diff --git a/go/identity/v1/gamingplatform.go b/go/identity/v1/gamingplatform.go index b80ef81..cd96545 100644 --- a/go/identity/v1/gamingplatform.go +++ b/go/identity/v1/gamingplatform.go @@ -1,4 +1,4 @@ -package IdentityV1 +package identityv1 // This file is auto-generated. Do not edit! From 760d398777e6dd96b5440eae2eca0d6270da2448 Mon Sep 17 00:00:00 2001 From: Markus Reinhold Date: Sun, 7 Dec 2025 19:52:47 +0100 Subject: [PATCH 6/6] Create factory methods for subtypes --- bin/generateAdditionalLanguageFeatures | 29 ++++++++++--------- php/GamingPlatform/Api/Common/V1/CommonV1.php | 18 ++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/bin/generateAdditionalLanguageFeatures b/bin/generateAdditionalLanguageFeatures index 89ceb88..284a254 100755 --- a/bin/generateAdditionalLanguageFeatures +++ b/bin/generateAdditionalLanguageFeatures @@ -16,7 +16,7 @@ function loadFiles($directory): void } } -function findRootMessagesClasses(): array +function findMessagesClasses(): array { $definedMessages = array_filter( get_declared_classes(), @@ -32,15 +32,14 @@ function findRootMessagesClasses(): array $matches ); - if (str_contains($matches['message'], '\\')) { - continue; // Ignore nested types. - } - $domainKey = $matches['domain'] . $matches['version']; $domains[$domainKey] ??= ['name' => $matches['domain'], 'version' => $matches['version'], 'messages' => []]; $domains[$domainKey]['messages'][] = [ 'name' => $matches['message'], - 'type' => $matches['domain'] . '.' . $matches['message'] . '.' . strtolower($matches['version']) + 'snakeName' => str_replace('\\', '_', $matches['message']), + 'wireType' => !str_contains($matches['message'], '\\') + ? $matches['domain'] . '.' . $matches['message'] . '.' . strtolower($matches['version']) + : null ]; } @@ -60,8 +59,10 @@ function writePhpFiles(array $domains): void $content .= 'final class ' . $domain['name'] . $domain['version'] . PHP_EOL; $content .= '{' . PHP_EOL; foreach ($domain['messages'] as $message) { - $content .= ' public const string ' . $message['name'] . 'Type = \'' . $message['type'] . '\';' . PHP_EOL; - $content .= ' public static function create' . $message['name'] . '(' . PHP_EOL; + if ($message['wireType']) { + $content .= ' public const string ' . $message['name'] . 'Type = \'' . $message['wireType'] . '\';' . PHP_EOL; + } + $content .= ' public static function create' . $message['snakeName'] . '(' . PHP_EOL; $content .= ' ?string $data = null' . PHP_EOL; $content .= ' ): ' . $message['name'] . ' {' . PHP_EOL; $content .= ' static $template;' . PHP_EOL; @@ -78,7 +79,7 @@ function writePhpFiles(array $domains): void } } -function writeGoFile(array $domains): void +function writeGoFiles(array $domains): void { foreach ($domains as $domain) { $path = GO_PACKAGE_PATH . '/' . strtolower($domain['name'] . '/' . $domain['version']) . '/gamingplatform.go'; @@ -86,7 +87,9 @@ function writeGoFile(array $domains): void $content = 'package ' . strtolower($domain['name'] . $domain['version']) . PHP_EOL . PHP_EOL; $content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL; foreach ($domain['messages'] as $message) { - $content .= 'const ' . $message['name'] . 'Type = "' . $message['type'] . '"' . PHP_EOL; + if ($message['wireType']) { + $content .= 'const ' . $message['name'] . 'Type = "' . $message['wireType'] . '"' . PHP_EOL; + } } echo 'Write ' . $path . PHP_EOL; @@ -96,6 +99,6 @@ function writeGoFile(array $domains): void loadFiles(PHP_PACKAGE_PATH); -$rootMessagesClasses = findRootMessagesClasses(); -writePhpFiles($rootMessagesClasses); -writeGoFile($rootMessagesClasses); +$messageClasses = findMessagesClasses(); +writePhpFiles($messageClasses); +writeGoFiles($messageClasses); diff --git a/php/GamingPlatform/Api/Common/V1/CommonV1.php b/php/GamingPlatform/Api/Common/V1/CommonV1.php index f8adf20..681d421 100644 --- a/php/GamingPlatform/Api/Common/V1/CommonV1.php +++ b/php/GamingPlatform/Api/Common/V1/CommonV1.php @@ -8,6 +8,24 @@ final class CommonV1 { + public static function createErrorResponse_Violation_Parameter( + ?string $data = null + ): ErrorResponse\Violation\Parameter { + static $template; + $template ??= new ErrorResponse\Violation\Parameter(); + $message = clone $template; + if ($data !== null) $message->mergeFromString($data); + return $message; + } + public static function createErrorResponse_Violation( + ?string $data = null + ): ErrorResponse\Violation { + static $template; + $template ??= new ErrorResponse\Violation(); + $message = clone $template; + if ($data !== null) $message->mergeFromString($data); + return $message; + } public const string ErrorResponseType = 'Common.ErrorResponse.v1'; public static function createErrorResponse( ?string $data = null