Skip to content

Commit

Permalink
Update Invoker to handle Message objects as input
Browse files Browse the repository at this point in the history
Now it can accept both string and Message objects as inputs. This change simplifies the process when the input is already a Message object, eliminating the need to convert it again.
  • Loading branch information
butschster committed Nov 30, 2023
1 parent 94b5ec7 commit 00cd2f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ final class Invoker implements InvokerInterface
'Method %s input type must be an instance of %s, ' .
'but the input is type of %s';

public function invoke(ServiceInterface $service, Method $method, ContextInterface $ctx, ?string $input): string
{
public function invoke(
ServiceInterface $service,
Method $method,
ContextInterface $ctx,
string|Message|null $input,
): string {
/** @var callable $callable */
$callable = [$service, $method->name];

$input = $input instanceof Message ? $input : $this->makeInput($method, $input);

/** @var Message $message */
$message = $callable($ctx, $this->makeInput($method, $input));
$message = $callable($ctx, $input);

\assert($this->assertResultType($method, $message));

Expand All @@ -45,7 +51,7 @@ private function assertResultType(Method $method, mixed $result): bool
$type = \get_debug_type($result);

throw new \BadFunctionCallException(
\sprintf(self::ERROR_METHOD_RETURN, $method->name, Message::class, $type)
\sprintf(self::ERROR_METHOD_RETURN, $method->name, Message::class, $type),
);
}

Expand Down Expand Up @@ -86,7 +92,7 @@ private function assertInputType(Method $method, string $class): bool
{
if (!\is_subclass_of($class, Message::class)) {
throw new \InvalidArgumentException(
\sprintf(self::ERROR_METHOD_IN_TYPE, $method->name, Message::class, $class)
\sprintf(self::ERROR_METHOD_IN_TYPE, $method->name, Message::class, $class),
);
}

Expand Down
22 changes: 21 additions & 1 deletion tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public function testInvoke(): void
$this->assertSame('pong', $m->getMsg());
}

public function testInvokeWithInputMessage(): void
{
$s = new TestService();
$m = Method::parse(new \ReflectionMethod($s, 'Echo'));

$i = new Invoker();

$out = $i->invoke($s, $m, new Context([]), $this->createMessage('hello'));

$m = new Message();
$m->mergeFromString($out);

$this->assertSame('pong', $m->getMsg());
}

public function testInvokeError(): void
{
$this->expectException(\Spiral\RoadRunner\GRPC\Exception\InvokeException::class);
Expand All @@ -41,10 +56,15 @@ public function testInvokeError(): void
}

private function packMessage(string $message): string
{
return $this->createMessage($message)->serializeToString();
}

private function createMessage(string $message): Message
{
$m = new Message();
$m->setMsg($message);

return $m->serializeToString();
return $m;
}
}

0 comments on commit 00cd2f6

Please sign in to comment.