From 0b2b1ae26d7c92941d71b0096a7e42aa6e4ec296 Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Sun, 17 Nov 2024 23:04:51 +0100 Subject: [PATCH] improve hash function --- src/Generator/Protobuf.php | 10 +++- .../Generator/resource/protobuf/protobuf.txt | 54 +++++++++---------- .../resource/protobuf/protobuf_oop.txt | 30 +++++------ 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/Generator/Protobuf.php b/src/Generator/Protobuf.php index 868a0a4..db54848 100644 --- a/src/Generator/Protobuf.php +++ b/src/Generator/Protobuf.php @@ -20,6 +20,7 @@ namespace PSX\Schema\Generator; +use PSX\Schema\Exception\GeneratorException; use PSX\Schema\Generator\Type\GeneratorInterface; use PSX\Schema\Type\StructDefinitionType; @@ -63,9 +64,10 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte private function generateNumber(string $name): int { + $hash = sha1($name); $result = 0; - for ($i = 0; $i < strlen($name); $i++) { - $result+= ord($name[$i]); + for ($i = 0; $i < 7; $i++) { + $result+= (hexdec($hash[$i]) << ($i * 4)); } /** @psalm-suppress TypeDoesNotContainType */ @@ -74,6 +76,10 @@ private function generateNumber(string $name): int $result += (19_999 - $result) + 1; } + if ($result > 536_870_911) { + throw new GeneratorException('Generated index is too large'); + } + return $result; } } diff --git a/tests/Generator/resource/protobuf/protobuf.txt b/tests/Generator/resource/protobuf/protobuf.txt index b90f80c..21f08a9 100644 --- a/tests/Generator/resource/protobuf/protobuf.txt +++ b/tests/Generator/resource/protobuf/protobuf.txt @@ -1,38 +1,38 @@ message Location { - optional float lat = 321 [json_name="lat"]; - optional float long = 432 [json_name="long"]; + optional float lat = 210819871 [json_name="lat"]; + optional float long = 259130331 [json_name="long"]; } message Author { - 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"]; + optional string title = 186570435 [json_name="title"]; + optional string email = 215464074 [json_name="email"]; + optional repeated string categories = 142514949 [json_name="categories"]; + optional repeated Location locations = 170963641 [json_name="locations"]; + optional Location origin = 77547563 [json_name="origin"]; } message News { - optional map config = 630 [json_name="config"]; - optional map inlineConfig = 1237 [json_name="inlineConfig"]; - optional map mapTags = 717 [json_name="mapTags"]; - optional map 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 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"]; + optional map config = 178760701 [json_name="config"]; + optional map inlineConfig = 215698315 [json_name="inlineConfig"]; + optional map mapTags = 14364631 [json_name="mapTags"]; + optional map mapReceiver = 167282547 [json_name="mapReceiver"]; + optional repeated string tags = 174057145 [json_name="tags"]; + optional repeated Author receiver = 176067080 [json_name="receiver"]; + optional bool read = 199096954 [json_name="read"]; + optional Author author = 244171887 [json_name="author"]; + optional map meta = 155201724 [json_name="meta"]; + optional Timestamp sendDate = 133269890 [json_name="sendDate"]; + optional Timestamp readDate = 10547211 [json_name="readDate"]; + optional float price = 54294786 [json_name="price"]; + optional int64 rating = 126276291 [json_name="rating"]; + optional string content = 258011200 [json_name="content"]; + optional string question = 237941427 [json_name="question"]; + optional string version = 57813356 [json_name="version"]; + optional string coffeeTime = 145564454 [json_name="coffeeTime"]; + optional string captcha = 21415284 [json_name="g-recaptcha-response"]; + optional string mediaFields = 83269706 [json_name="media.fields"]; + optional Any payload = 145090319 [json_name="payload"]; } diff --git a/tests/Generator/resource/protobuf/protobuf_oop.txt b/tests/Generator/resource/protobuf/protobuf_oop.txt index 9354bc3..c996ecc 100644 --- a/tests/Generator/resource/protobuf/protobuf_oop.txt +++ b/tests/Generator/resource/protobuf/protobuf_oop.txt @@ -1,38 +1,38 @@ message HumanType { - optional string firstName = 937 [json_name="firstName"]; - optional HumanType parent = 650 [json_name="parent"]; + optional string firstName = 5347166 [json_name="firstName"]; + optional HumanType parent = 227794829 [json_name="parent"]; } message Student { - optional string firstName = 937 [json_name="firstName"]; - optional HumanType parent = 650 [json_name="parent"]; - optional string matricleNumber = 1466 [json_name="matricleNumber"]; + optional string firstName = 5347166 [json_name="firstName"]; + optional HumanType parent = 227794829 [json_name="parent"]; + optional string matricleNumber = 110046485 [json_name="matricleNumber"]; } message StudentMap { - optional int64 totalResults = 1302 [json_name="totalResults"]; - optional HumanType parent = 650 [json_name="parent"]; - optional repeated Student entries = 762 [json_name="entries"]; + optional int64 totalResults = 229871694 [json_name="totalResults"]; + optional HumanType parent = 227794829 [json_name="parent"]; + optional repeated Student entries = 219233836 [json_name="entries"]; } message HumanMap { - optional int64 totalResults = 1302 [json_name="totalResults"]; - optional HumanType parent = 650 [json_name="parent"]; - optional repeated HumanType entries = 762 [json_name="entries"]; + optional int64 totalResults = 229871694 [json_name="totalResults"]; + optional HumanType parent = 227794829 [json_name="parent"]; + optional repeated HumanType entries = 219233836 [json_name="entries"]; } message Map { - optional int64 totalResults = 1302 [json_name="totalResults"]; - optional P parent = 650 [json_name="parent"]; - optional repeated T entries = 762 [json_name="entries"]; + optional int64 totalResults = 229871694 [json_name="totalResults"]; + optional P parent = 227794829 [json_name="parent"]; + optional repeated T entries = 219233836 [json_name="entries"]; } message RootSchema { - optional StudentMap students = 890 [json_name="students"]; + optional StudentMap students = 57976611 [json_name="students"]; }