From 732d9e5a69487ca29521f779bb609998c791e6ce Mon Sep 17 00:00:00 2001 From: Maxim Kerstens Date: Tue, 10 Nov 2026 22:30:54 +0100 Subject: [PATCH] Let types carry their schema name and doc comment Structs and enums take an optional record id (`path/to/module.skir:Qualified.Name`) and doc, and fields, variants and methods an optional doc. Serialization ignores them; they exist so a server can describe its methods the way the schema does. Co-Authored-By: Claude Fable 5.2 --- README.md | 10 +++++++++ src/Field.php | 5 +++-- src/MethodDescriptor.php | 1 + src/Type.php | 13 ++++++++---- src/Variant.php | 9 ++++---- tests/MethodDescriptorTest.php | 38 ++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 31d73ad..f092d73 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,16 @@ $json = DenseJson::toJson($user, [ $data = DenseJson::fromJson($user, $json); ``` +A struct, enum, field, variant or method can also carry what the schema calls it. This is descriptive only: tools such as the server's RPC Studio show it, and serialization ignores it. + +```php +$session = Type::struct( + [Field::value('token', 0, Type::string(), doc: 'Send it as a bearer token.')], + recordId: 'identity/identity.skir:Session', + doc: 'What a client stores after signing in.', +); +``` + Struct values are encoded as field-number indexed arrays. Removed fields and sparse field numbers are preserved so generated DTOs can stay compatible with existing Skir schemas. `DenseJson::fromJson()` and `DenseJson::decode()` accept both the dense and the readable flavor, like the upstream Skir runtimes. diff --git a/src/Field.php b/src/Field.php index 25af6ba..07bf38b 100644 --- a/src/Field.php +++ b/src/Field.php @@ -11,11 +11,12 @@ private function __construct( public int $number, public ?Type $type, public bool $removed, + public ?string $doc = null, ) {} - public static function value(string $name, int $number, Type $type): self + public static function value(string $name, int $number, Type $type, ?string $doc = null): self { - return new self($name, $number, $type, false); + return new self($name, $number, $type, false, $doc); } public static function removed(int $number): self diff --git a/src/MethodDescriptor.php b/src/MethodDescriptor.php index 92a32ac..2786e52 100644 --- a/src/MethodDescriptor.php +++ b/src/MethodDescriptor.php @@ -11,5 +11,6 @@ public function __construct( public int $number, public Type $requestType, public Type $responseType, + public ?string $doc = null, ) {} } diff --git a/src/Type.php b/src/Type.php index eea613f..699b3d0 100644 --- a/src/Type.php +++ b/src/Type.php @@ -15,6 +15,8 @@ private function __construct( public ?Type $itemType = null, public array $fields = [], public array $variants = [], + public ?string $recordId = null, + public ?string $doc = null, ) {} public static function bool(): self @@ -73,22 +75,25 @@ public static function array(Type $itemType): self } /** + * `$recordId` is the record's identity in the schema, `path/to/module.skir:Qualified.Name`. + * It and `$doc` only describe the type; neither affects serialization. + * * @param array $fields */ - public static function struct(array $fields): self + public static function struct(array $fields, ?string $recordId = null, ?string $doc = null): self { usort($fields, fn (Field $left, Field $right): int => $left->number <=> $right->number); - return new self(TypeKind::Struct, fields: $fields); + return new self(TypeKind::Struct, fields: $fields, recordId: $recordId, doc: $doc); } /** * @param array $variants */ - public static function enum(array $variants): self + public static function enum(array $variants, ?string $recordId = null, ?string $doc = null): self { usort($variants, fn (Variant $left, Variant $right): int => $left->number <=> $right->number); - return new self(TypeKind::Enum, variants: $variants); + return new self(TypeKind::Enum, variants: $variants, recordId: $recordId, doc: $doc); } } diff --git a/src/Variant.php b/src/Variant.php index 4d88cca..910b90f 100644 --- a/src/Variant.php +++ b/src/Variant.php @@ -10,16 +10,17 @@ private function __construct( public string $name, public int $number, public ?Type $payloadType, + public ?string $doc = null, ) {} - public static function constant(string $name, int $number): self + public static function constant(string $name, int $number, ?string $doc = null): self { - return new self($name, $number, null); + return new self($name, $number, null, $doc); } - public static function wrapper(string $name, int $number, Type $payloadType): self + public static function wrapper(string $name, int $number, Type $payloadType, ?string $doc = null): self { - return new self($name, $number, $payloadType); + return new self($name, $number, $payloadType, $doc); } public function isWrapper(): bool diff --git a/tests/MethodDescriptorTest.php b/tests/MethodDescriptorTest.php index 7829ff2..200dd89 100644 --- a/tests/MethodDescriptorTest.php +++ b/tests/MethodDescriptorTest.php @@ -5,8 +5,10 @@ namespace Skir\Runtime\Tests; use PHPUnit\Framework\TestCase; +use Skir\Runtime\Field; use Skir\Runtime\MethodDescriptor; use Skir\Runtime\Type; +use Skir\Runtime\Variant; final class MethodDescriptorTest extends TestCase { @@ -27,4 +29,40 @@ public function test_it_describes_a_skir_rpc_method(): void $this->assertSame($requestType, $method->requestType); $this->assertSame($responseType, $method->responseType); } + + public function test_types_can_carry_the_schema_names_and_doc_comments(): void + { + $status = Type::enum( + [ + Variant::constant('pending', 1, 'Not accepted yet.'), + Variant::wrapper('refused', 2, Type::string()), + ], + recordId: 'invitations/invitations.skir:Invitation.Status', + doc: 'Where an invitation stands.', + ); + $invitation = Type::struct( + [Field::value('status', 0, $status, 'Changes when the invitee answers.')], + recordId: 'invitations/invitations.skir:Invitation', + doc: 'One invitation.', + ); + $method = new MethodDescriptor('GetInvitation', 1, Type::string(), $invitation, doc: 'Looks up an invitation.'); + + $this->assertSame('invitations/invitations.skir:Invitation', $invitation->recordId); + $this->assertSame('One invitation.', $invitation->doc); + $this->assertSame('Changes when the invitee answers.', $invitation->fields[0]->doc); + $this->assertSame('invitations/invitations.skir:Invitation.Status', $status->recordId); + $this->assertSame('Not accepted yet.', $status->variants[0]->doc); + $this->assertNull($status->variants[1]->doc); + $this->assertSame('Looks up an invitation.', $method->doc); + } + + public function test_names_and_doc_comments_are_optional(): void + { + $type = Type::struct([Field::value('name', 0, Type::string())]); + + $this->assertNull($type->recordId); + $this->assertNull($type->doc); + $this->assertNull($type->fields[0]->doc); + $this->assertNull((new MethodDescriptor('Ping', 2, $type, $type))->doc); + } }