Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of protobuf generation #17

Open
byrnedo opened this issue Apr 9, 2016 · 5 comments
Open

Example of protobuf generation #17

byrnedo opened this issue Apr 9, 2016 · 5 comments

Comments

@byrnedo
Copy link

byrnedo commented Apr 9, 2016

Is it possible to generate php from proto files with Meta? Would be great to see an example workflow.

@jakubkulhan
Copy link
Contributor

It is possible, e.g. everything in gen-src/ directory in this repository is generated from .proto files here.

Check out this repository, run composer install and add bin/ to your $PATH. Then use official protoc compiler, e.g.:

protoc -I./proto --php_out=gen-src proto/skrz/meta/fixtures/protobuf/interoperability.proto

protoc will parse .proto file and then pass it to protoc-gen-php, which will generate the source code for messages & enums (it does not generate any interfaces for services).

You can also add @spec comment above package in .proto file to use custom MetaSpec class - https://github.com/skrz/meta/blob/master/proto/skrz/meta/fixtures/protobuf/interoperability.proto#L3

@byrnedo
Copy link
Author

byrnedo commented Apr 16, 2016

Thanks, will give this all a try.

@byrnedo
Copy link
Author

byrnedo commented Apr 21, 2016

Finally got round to this. I can't seem to get it to work yet.
My setup is 1 proto file which has a field which is of a type defined in another proto file.
This other proto file lives in a vendor dir in my project

My message proto:

package mypkg;

import "github.com/byrnedo/apibase/natsio/protobuf/nats.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";


message Request {
    optional protobuf.NatsContext context = 1;
    ...
}

The command:

protoc --php_out=gen-src  --proto_path ../../vendor/ --proto_path ./ --proto_path ../../vendor/github.com/gogo/protobuf/protobuf request.proto

The error:

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class Protobuf\NatsContext does not exist' in /home/donal/dev/github/meta/src/Skrz/Meta/Reflection/MixedType.php:133
Stack trace:
#0 /home/donal/dev/github/meta/src/Skrz/Meta/Reflection/MixedType.php(133): ReflectionClass->__construct('Protobuf\\NatsCo...')
...

Must I compile the other proto file first? I did try that but I got

@byrnedo
Copy link
Author

byrnedo commented Apr 21, 2016

Ah ok, I think it's because I have multiple messages per file? Is there support for that?

Edit: I got round the issue by making one of the messages in my file to be embedded declaration.

@jakubkulhan
Copy link
Contributor

It seems that message classes are not properly generated before skrz/meta tries to generate *Meta classes. Unfortunately, this isn't tested well, because I mostly used @ProtobufField annotations on existing PHP classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants