Skip to content

Commit

Permalink
add logic to generate stable numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 15, 2024
1 parent 3473445 commit 9c8081b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 46 deletions.
20 changes: 16 additions & 4 deletions src/Generator/Protobuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,28 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte
{
$code = 'message ' . $name->getClass() . ' {' . "\n";

$index = 1;
foreach ($properties as $property) {
/** @var Code\Property $property */
$code.= $this->indent . 'optional ' . $property->getType() . ' ' . $property->getName()->getProperty() . ' = ' . $index . ' [json_name="' . $property->getName()->getRaw() . '"];' . "\n";

$index++;
$code.= $this->indent . 'optional ' . $property->getType() . ' ' . $property->getName()->getProperty() . ' = ' . $this->generateNumber($property->getName()->getRaw()) . ' [json_name="' . $property->getName()->getRaw() . '"];' . "\n";
}

$code.= '}' . "\n";

return $code;
}

private function generateNumber(string $name): int
{
$result = 0;
for ($i = 0; $i < strlen($name); $i++) {
$result+= ord($name[$i]);
}

if ($result >= 19_000 && $result <= 19_999) {

Check failure on line 71 in src/Generator/Protobuf.php

View workflow job for this annotation

GitHub Actions / Psalm

TypeDoesNotContainType

src/Generator/Protobuf.php:71:13: TypeDoesNotContainType: Type int<0, 510> for $result is always !<19000 (see https://psalm.dev/056)
// Field numbers 19,000 to 19,999 are reserved for the Protocol Buffers implementation
$result += (19_999 - $result) + 1;
}

return $result;
}
}
54 changes: 27 additions & 27 deletions tests/Generator/resource/protobuf/protobuf.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
message Location {
optional float lat = 1 [json_name="lat"];
optional float long = 2 [json_name="long"];
optional float lat = 321 [json_name="lat"];
optional float long = 432 [json_name="long"];
}


message Author {
optional string title = 1 [json_name="title"];
optional string email = 2 [json_name="email"];
optional repeated string categories = 3 [json_name="categories"];
optional repeated Location locations = 4 [json_name="locations"];
optional Location origin = 5 [json_name="origin"];
optional string title = 546 [json_name="title"];
optional string email = 520 [json_name="email"];
optional repeated string categories = 1062 [json_name="categories"];
optional repeated Location locations = 972 [json_name="locations"];
optional Location origin = 648 [json_name="origin"];
}


message News {
optional map<string, string> config = 1 [json_name="config"];
optional map<string, string> inlineConfig = 2 [json_name="inlineConfig"];
optional map<string, string> mapTags = 3 [json_name="mapTags"];
optional map<string, Author> mapReceiver = 4 [json_name="mapReceiver"];
optional repeated string tags = 5 [json_name="tags"];
optional repeated Author receiver = 6 [json_name="receiver"];
optional bool read = 7 [json_name="read"];
optional Author author = 8 [json_name="author"];
optional map<string, string> meta = 9 [json_name="meta"];
optional Timestamp sendDate = 10 [json_name="sendDate"];
optional Timestamp readDate = 11 [json_name="readDate"];
optional float price = 12 [json_name="price"];
optional int64 rating = 13 [json_name="rating"];
optional string content = 14 [json_name="content"];
optional string question = 15 [json_name="question"];
optional string version = 16 [json_name="version"];
optional string coffeeTime = 17 [json_name="coffeeTime"];
optional string captcha = 18 [json_name="g-recaptcha-response"];
optional string mediaFields = 19 [json_name="media.fields"];
optional Any payload = 20 [json_name="payload"];
optional map<string, string> config = 630 [json_name="config"];
optional map<string, string> inlineConfig = 1237 [json_name="inlineConfig"];
optional map<string, string> mapTags = 717 [json_name="mapTags"];
optional map<string, Author> mapReceiver = 1139 [json_name="mapReceiver"];
optional repeated string tags = 431 [json_name="tags"];
optional repeated Author receiver = 853 [json_name="receiver"];
optional bool read = 412 [json_name="read"];
optional Author author = 659 [json_name="author"];
optional map<string, string> meta = 423 [json_name="meta"];
optional Timestamp sendDate = 808 [json_name="sendDate"];
optional Timestamp readDate = 794 [json_name="readDate"];
optional float price = 531 [json_name="price"];
optional int64 rating = 645 [json_name="rating"];
optional string content = 763 [json_name="content"];
optional string question = 888 [json_name="question"];
optional string version = 774 [json_name="version"];
optional string coffeeTime = 1015 [json_name="coffeeTime"];
optional string captcha = 2011 [json_name="g-recaptcha-response"];
optional string mediaFields = 1189 [json_name="media.fields"];
optional Any payload = 746 [json_name="payload"];
}

30 changes: 15 additions & 15 deletions tests/Generator/resource/protobuf/protobuf_oop.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
message HumanType {
optional string firstName = 1 [json_name="firstName"];
optional HumanType parent = 2 [json_name="parent"];
optional string firstName = 937 [json_name="firstName"];
optional HumanType parent = 650 [json_name="parent"];
}


message Student {
optional string firstName = 1 [json_name="firstName"];
optional HumanType parent = 2 [json_name="parent"];
optional string matricleNumber = 3 [json_name="matricleNumber"];
optional string firstName = 937 [json_name="firstName"];
optional HumanType parent = 650 [json_name="parent"];
optional string matricleNumber = 1466 [json_name="matricleNumber"];
}


message StudentMap {
optional int64 totalResults = 1 [json_name="totalResults"];
optional HumanType parent = 2 [json_name="parent"];
optional repeated Student entries = 3 [json_name="entries"];
optional int64 totalResults = 1302 [json_name="totalResults"];
optional HumanType parent = 650 [json_name="parent"];
optional repeated Student entries = 762 [json_name="entries"];
}


message HumanMap {
optional int64 totalResults = 1 [json_name="totalResults"];
optional HumanType parent = 2 [json_name="parent"];
optional repeated HumanType entries = 3 [json_name="entries"];
optional int64 totalResults = 1302 [json_name="totalResults"];
optional HumanType parent = 650 [json_name="parent"];
optional repeated HumanType entries = 762 [json_name="entries"];
}


message Map {
optional int64 totalResults = 1 [json_name="totalResults"];
optional P parent = 2 [json_name="parent"];
optional repeated T entries = 3 [json_name="entries"];
optional int64 totalResults = 1302 [json_name="totalResults"];
optional P parent = 650 [json_name="parent"];
optional repeated T entries = 762 [json_name="entries"];
}


message RootSchema {
optional StudentMap students = 1 [json_name="students"];
optional StudentMap students = 890 [json_name="students"];
}

0 comments on commit 9c8081b

Please sign in to comment.