Skip to content

Commit d752500

Browse files
authored
Generate message type enums (#10)
Generate message type enums for Go and PHP. PHP message factories are now placed next to their message types. The former factory classes are deprecated #9. Although labeled as enums, they are implemented as constants for convenience.
1 parent 10b82e3 commit d752500

14 files changed

Lines changed: 318 additions & 10 deletions

File tree

bin/01-post-build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Go files are generated into their full module path.
6+
# This script moves the files to the correct location.
7+
cp -r /project/go/github.com/gaming-platform/api/go/* /project/go
8+
rm -rf /project/go/github.com
9+
10+
/project/bin/generateAdditionalLanguageFeatures
11+
/project/bin/generateLegacyLanguageFeatures

bin/fixGoPaths

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
const PHP_PACKAGE_NAMESPACE = 'GamingPlatform\\Api';
7+
const PHP_PACKAGE_PATH = '/project/php/GamingPlatform/Api';
8+
const GO_PACKAGE_PATH = '/project/go';
9+
10+
function loadFiles($directory): void
11+
{
12+
$entries = array_diff(scandir($directory), ['.', '..']);
13+
foreach ($entries as $entry) {
14+
$path = $directory . '/' . $entry;
15+
is_dir($path) ? loadFiles($path) : require_once $path;
16+
}
17+
}
18+
19+
function findMessagesClasses(): array
20+
{
21+
$definedMessages = array_filter(
22+
get_declared_classes(),
23+
static fn(string $class) => str_starts_with($class, PHP_PACKAGE_NAMESPACE)
24+
&& in_array(\Google\Protobuf\Internal\Message::class, class_parents($class))
25+
);
26+
$domains = [];
27+
28+
foreach ($definedMessages as $message) {
29+
preg_match(
30+
'/(?<domain>.*)\\\\(?<version>V\d+)\\\\(?<message>.*)/',
31+
substr($message, strlen(PHP_PACKAGE_NAMESPACE) + 1),
32+
$matches
33+
);
34+
35+
$domainKey = $matches['domain'] . $matches['version'];
36+
$domains[$domainKey] ??= ['name' => $matches['domain'], 'version' => $matches['version'], 'messages' => []];
37+
$domains[$domainKey]['messages'][] = [
38+
'name' => $matches['message'],
39+
'snakeName' => str_replace('\\', '_', $matches['message']),
40+
'wireType' => !str_contains($matches['message'], '\\')
41+
? $matches['domain'] . '.' . $matches['message'] . '.' . strtolower($matches['version'])
42+
: null
43+
];
44+
}
45+
46+
return $domains;
47+
}
48+
49+
function writePhpFiles(array $domains): void
50+
{
51+
foreach ($domains as $domain) {
52+
$path = PHP_PACKAGE_PATH . '/' . $domain['name'] . '/' . $domain['version'] . '/' . $domain['name'] . $domain['version'] . '.php';
53+
$namespace = PHP_PACKAGE_NAMESPACE . '\\' . $domain['name'] . '\\' . $domain['version'];
54+
55+
$content = '<?php' . PHP_EOL . PHP_EOL;
56+
$content .= 'declare(strict_types=1);' . PHP_EOL . PHP_EOL;
57+
$content .= 'namespace ' . $namespace . ';' . PHP_EOL . PHP_EOL;
58+
$content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL;
59+
$content .= 'final class ' . $domain['name'] . $domain['version'] . PHP_EOL;
60+
$content .= '{' . PHP_EOL;
61+
foreach ($domain['messages'] as $message) {
62+
if ($message['wireType']) {
63+
$content .= ' public const string ' . $message['name'] . 'Type = \'' . $message['wireType'] . '\';' . PHP_EOL;
64+
}
65+
$content .= ' public static function create' . $message['snakeName'] . '(' . PHP_EOL;
66+
$content .= ' ?string $data = null' . PHP_EOL;
67+
$content .= ' ): ' . $message['name'] . ' {' . PHP_EOL;
68+
$content .= ' static $template;' . PHP_EOL;
69+
$content .= ' $template ??= new ' . $message['name'] . '();' . PHP_EOL;
70+
$content .= ' $message = clone $template;' . PHP_EOL;
71+
$content .= ' if ($data !== null) $message->mergeFromString($data);' . PHP_EOL;
72+
$content .= ' return $message;' . PHP_EOL;
73+
$content .= ' }' . PHP_EOL;
74+
}
75+
$content .= '}' . PHP_EOL;
76+
77+
echo 'Write ' . $path . PHP_EOL;
78+
file_put_contents($path, $content);
79+
}
80+
}
81+
82+
function writeGoFiles(array $domains): void
83+
{
84+
foreach ($domains as $domain) {
85+
$path = GO_PACKAGE_PATH . '/' . strtolower($domain['name'] . '/' . $domain['version']) . '/gamingplatform.go';
86+
87+
$content = 'package ' . strtolower($domain['name'] . $domain['version']) . PHP_EOL . PHP_EOL;
88+
$content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL;
89+
foreach ($domain['messages'] as $message) {
90+
if ($message['wireType']) {
91+
$content .= 'const ' . $message['name'] . 'Type = "' . $message['wireType'] . '"' . PHP_EOL;
92+
}
93+
}
94+
95+
echo 'Write ' . $path . PHP_EOL;
96+
file_put_contents($path, $content);
97+
}
98+
}
99+
100+
loadFiles(PHP_PACKAGE_PATH);
101+
102+
$messageClasses = findMessagesClasses();
103+
writePhpFiles($messageClasses);
104+
writeGoFiles($messageClasses);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ function writeFactories(array $factories): void
6262
$content .= 'declare(strict_types=1);' . PHP_EOL . PHP_EOL;
6363
$content .= 'namespace ' . $factory['namespace'] . ';' . PHP_EOL . PHP_EOL;
6464
$content .= '// This file is auto-generated. Do not edit!' . PHP_EOL . PHP_EOL;
65+
$content .= '/** @deprecated */' . PHP_EOL;
6566
$content .= 'final class ' . $factory['name'] . PHP_EOL;
6667
$content .= '{' . PHP_EOL;
6768
foreach ($factory['methods'] as $method) {
69+
$content .= ' /** @deprecated */' . PHP_EOL;
6870
$content .= ' public static function ' . $method['name'] . '(' . PHP_EOL;
6971
$content .= ' string $data' . PHP_EOL;
7072
$content .= ' ): ' . $method['returnType'] . ' {' . PHP_EOL;

go/chat/v1/gamingplatform.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package chatv1
2+
3+
// This file is auto-generated. Do not edit!
4+
5+
const InitiateChatType = "Chat.InitiateChat.v1"
6+
const InitiateChatResponseType = "Chat.InitiateChatResponse.v1"
7+
const MessageWrittenType = "Chat.MessageWritten.v1"

go/common/v1/gamingplatform.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package commonv1
2+
3+
// This file is auto-generated. Do not edit!
4+
5+
const ErrorResponseType = "Common.ErrorResponse.v1"

go/identity/v1/gamingplatform.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package identityv1
2+
3+
// This file is auto-generated. Do not edit!
4+
5+
const BotType = "Identity.Bot.v1"
6+
const GetBotByUsernameType = "Identity.GetBotByUsername.v1"
7+
const GetBotByUsernameResponseType = "Identity.GetBotByUsernameResponse.v1"
8+
const RegisterBotType = "Identity.RegisterBot.v1"
9+
const RegisterBotResponseType = "Identity.RegisterBotResponse.v1"
10+
const UserArrivedType = "Identity.UserArrived.v1"
11+
const UserSignedUpType = "Identity.UserSignedUp.v1"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GamingPlatform\Api\Chat\V1;
6+
7+
// This file is auto-generated. Do not edit!
8+
9+
final class ChatV1
10+
{
11+
public const string InitiateChatType = 'Chat.InitiateChat.v1';
12+
public static function createInitiateChat(
13+
?string $data = null
14+
): InitiateChat {
15+
static $template;
16+
$template ??= new InitiateChat();
17+
$message = clone $template;
18+
if ($data !== null) $message->mergeFromString($data);
19+
return $message;
20+
}
21+
public const string InitiateChatResponseType = 'Chat.InitiateChatResponse.v1';
22+
public static function createInitiateChatResponse(
23+
?string $data = null
24+
): InitiateChatResponse {
25+
static $template;
26+
$template ??= new InitiateChatResponse();
27+
$message = clone $template;
28+
if ($data !== null) $message->mergeFromString($data);
29+
return $message;
30+
}
31+
public const string MessageWrittenType = 'Chat.MessageWritten.v1';
32+
public static function createMessageWritten(
33+
?string $data = null
34+
): MessageWritten {
35+
static $template;
36+
$template ??= new MessageWritten();
37+
$message = clone $template;
38+
if ($data !== null) $message->mergeFromString($data);
39+
return $message;
40+
}
41+
}

php/GamingPlatform/Api/Chat/V1/ChatV1Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
// This file is auto-generated. Do not edit!
88

9+
/** @deprecated */
910
final class ChatV1Factory
1011
{
12+
/** @deprecated */
1113
public static function createInitiateChat(
1214
string $data
1315
): \GamingPlatform\Api\Chat\V1\InitiateChat {
@@ -18,6 +20,7 @@ public static function createInitiateChat(
1820
return $message;
1921
}
2022

23+
/** @deprecated */
2124
public static function createInitiateChatResponse(
2225
string $data
2326
): \GamingPlatform\Api\Chat\V1\InitiateChatResponse {
@@ -28,6 +31,7 @@ public static function createInitiateChatResponse(
2831
return $message;
2932
}
3033

34+
/** @deprecated */
3135
public static function createMessageWritten(
3236
string $data
3337
): \GamingPlatform\Api\Chat\V1\MessageWritten {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GamingPlatform\Api\Common\V1;
6+
7+
// This file is auto-generated. Do not edit!
8+
9+
final class CommonV1
10+
{
11+
public static function createErrorResponse_Violation_Parameter(
12+
?string $data = null
13+
): ErrorResponse\Violation\Parameter {
14+
static $template;
15+
$template ??= new ErrorResponse\Violation\Parameter();
16+
$message = clone $template;
17+
if ($data !== null) $message->mergeFromString($data);
18+
return $message;
19+
}
20+
public static function createErrorResponse_Violation(
21+
?string $data = null
22+
): ErrorResponse\Violation {
23+
static $template;
24+
$template ??= new ErrorResponse\Violation();
25+
$message = clone $template;
26+
if ($data !== null) $message->mergeFromString($data);
27+
return $message;
28+
}
29+
public const string ErrorResponseType = 'Common.ErrorResponse.v1';
30+
public static function createErrorResponse(
31+
?string $data = null
32+
): ErrorResponse {
33+
static $template;
34+
$template ??= new ErrorResponse();
35+
$message = clone $template;
36+
if ($data !== null) $message->mergeFromString($data);
37+
return $message;
38+
}
39+
}

0 commit comments

Comments
 (0)