~bduggan/raku-protobuf

1111fa768f7a83817e7b58bc8a8e0d5ba43739eb — Brian Duggan 3 months ago 204a01f
repeated
M eg/nested.proto => eg/nested.proto +4 -0
@@ 5,6 5,7 @@ package helloworld;
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  rpc Echo (EchoRequest) returns (HelloReply) {}
  rpc Hellos (Hellos) returns (HelloReply) {}
}

message HelloRequest {


@@ 20,3 21,6 @@ message EchoRequest {
  string world = 2;
}

message Hellos {
  repeated HelloRequest holas = 1;
}

M lib/Protobuf/Actions.rakumod => lib/Protobuf/Actions.rakumod +3 -1
@@ 18,6 18,7 @@ class Protobuf::Message {
class Protobuf::Field {
  has $.name;
  has $.type;
  has Bool $.repeated = False;
}

class Protobuf::Actions {


@@ 57,7 58,8 @@ class Protobuf::Actions {

  method field($/) {
    my $type = ~$<type>;
    $/.make: Protobuf::Field.new( name => ~$<fieldName>, type => %*messages{ $type } // $type );
    my $repeated = $<repeated>.chars > 0;
    $/.make: Protobuf::Field.new( name => ~$<fieldName>, type => %*messages{ $type } // $type, :$repeated );
  }

  method service($/) {

M lib/Protobuf/Grammar.rakumod => lib/Protobuf/Grammar.rakumod +1 -1
@@ 174,7 174,7 @@ regex fieldNumber {

# field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
rule field {
  [ repeated ]? <type> <fieldName> '=' <fieldNumber> [ '[' <fieldOptions> ']' ]? ';'
  $<repeated>=[ repeated ]? <type> <fieldName> '=' <fieldNumber> [ '[' <fieldOptions> ']' ]? ';'
}

# fieldOptions = fieldOption { ","  fieldOption }

M script/protoc.raku => script/protoc.raku +2 -0
@@ 12,11 12,13 @@ for $p.services -> $svc {
    for $e.request.fields -> $f {
      say '   name : ' ~ $f.name;
      say '   type : ' ~ $f.type;
      say ' repeated' if $f.repeated;
    }
    say '  response params ' ~ $e.response.name;
    for $e.response.fields -> $f {
      say '   name : ' ~ $f.name;
      say '   type : ' ~ $f.type;
      say ' repeated' if $f.repeated;
    }
  }
}

M t/04-nested.rakutest => t/04-nested.rakutest +7 -3
@@ 2,8 2,12 @@ use Protobuf;
use Test;

my $m = parse-proto($?FILE.IO.parent.parent.child('eg/nested.proto').slurp);
my @services = $m.services;
is @services[0].endpoints[1].request.fields[0].type.name, 'HelloRequest', 'type name';
is @services[0].endpoints[1].request.fields[0].type.fields[0].name, 'name', 'type has nested attributes';
my $service = $m.services[0];
is $service.endpoints[1].request.fields[0].type.name, 'HelloRequest', 'type name';
is $service.endpoints[1].request.fields[0].type.fields[0].name, 'name', 'type has nested attributes';
nok $service.endpoints[1].request.fields[0].type.repeated, 'not repeated';

is $service.endpoints[2].request.fields[0].name, 'holas', 'holas';
ok $service.endpoints[2].request.fields[0].repeated, 'repeated';

done-testing;