elixir-protobuf / protobuf

A pure Elixir implementation of Google Protobuf.
https://hexdocs.pm/protobuf/readme.html
MIT License
823 stars 143 forks source link

Feature request: option for different package name (module name) in elixir #26

Closed qinix closed 4 years ago

qinix commented 6 years ago

Like this:

package google.bigtable.v1;

option go_package = "google.golang.org/genproto/googleapis/bigtable/v1;bigtable";
option java_package = "com.google.bigtable.v1";
tony612 commented 6 years ago

One problem is Elixir has no concept of package. So what should we do with the package option? One solution is treat them as module prefix. But if so, maybe elixir_module_prefix is a better name.

fschuindt commented 6 years ago

I was searching for those options when I found this issue.

@tony612 I think elixir_module_prefix is great. It should also allow the basic nesting, something like:

option elixir_module_prefix = "MyApp.ProtoBuf";

Edit:

I've achieved my goal by using package myapp.protobuf;.
Isn't the same thing?

tony612 commented 6 years ago

@fschuindt It's similar, but package is for all languages. btw, I thought about this again and elixir_package maybe a good choice. I'll support this option later.

bamorim commented 6 years ago

What if we add an option module_prefix=XXX in the cli? This could just add that to the namespace context, wouldnt that work? This would be nice if, for example, I have two elixir applications and I want to place the generated code in different places for each application.

tony612 commented 6 years ago

@bamorim There's a problem with option in cli: if you refer to a message in another package, it's hard to know its prefix like:

// msg.proto
import "other/message.proto";
message FooMsg {
  other.StringMessage str = 1;
}

// other/message.proto
message StringMessage {
  string value = 1;
}

If you generate other/message.proto using custom prefix, then you can't get right name of Stringmessage when you generate msg.proto.

So we'd better support custom option like elixir_module_prefix, which I'm working on.

tony612 commented 6 years ago

I implemented custom option elixir_module_prefix. But it's a little inconvenient to use because there's no official support. Usage is:

# Add this to your protobuf file
import "elixir.proto";
option (elixir_module_prefix) = "custom.prefix";

# Install latest elixir plugin
mix escript.install github tony612/protobuf-elixir

# Generate elixir files
protoc -I deps/protobuf/src --elixir_out . your.proto

For the protoc command, -I option should be be your local path to https://github.com/tony612/protobuf-elixir/tree/master/src or a path containing file elixir.proto, whose name isn't important. It's very simple so you can just create your own(https://github.com/tony612/protobuf-elixir/blob/master/src/elixir.proto):

syntax = "proto2";

import "google/protobuf/descriptor.proto";

extend google.protobuf.FileOptions {
  optional string elixir_module_prefix = 54637;
}
ammmir commented 5 years ago

@tony612 will you be releasing a new hex package with this feature? thanks!

tony612 commented 4 years ago

Closing this. Please notice that the usage is different from before since extension is implemented in https://github.com/tony612/protobuf-elixir/pull/83. See README and src/elixirpb.proto for details.