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

Enable multiple services in same package across multiple files #140

Closed
wants to merge 1 commit into from

Conversation

jpgneves
Copy link

Motivation

Prior to this change, for each file containing one or more
services, the code generator would generate with the following
modules:

pub mod client {
 //...
}

pub mod server {
 //...
}

While this works fine if all the services in the same protobuf
package are declared in a single file, this breaks horribly if
multiple files belonging to the same protobuf package declare
services. In that case, the code generator would generate several
modules with the same name in the same .rs file, which is
invalid.

For instance, given two files foo.proto and bar.proto like:

// foo.proto
package mypackage;
service Foo {
}

// bar.proto
package mypackage;
service Bar {
}

The generated code would result in a mypackage.rs such as:

// Generated from foo.proto
pub mod client {
  //...
}

pub mod server {
  //...
}

// Generated from bar.proto
pub mod client {
  //...
}

pub mod server {
  //...
}

One codebase where this scenario could be found is in Open Match

Solution

This change makes it so the name of the service is prepended to
the generated module, and therefore we avoid module name collisions.

After this change, given the two proto files mentioned previously,
we will generate the following modules inside mypackage.rs:

pub mod foo_client {
 //...
}

pub mod foo_server {
 //...
}

pub mod bar_client {
 //...
}

pub mod bar_server {
 //...
}

Prior to this change, for each _file_ containing one or more
services, the code generator would generate with the following
modules:

```
pub mod client {
 //...
}

pub mod server {
 //...
}
```

While this works fine if all the services in the same protobuf
package are declared in a single file, this breaks horribly if
multiple files belonging to the same protobuf package declare
services. In that case, the code generator would generate several
modules with the same name in the same `.rs` file, which is
invalid.

For instance, given two files `foo.proto` and `bar.proto` like:

```
// foo.proto
package mypackage;
service Foo {
}

// bar.proto
package mypackage;
service Bar {
}
```

The generated code would result in a `mypackage.rs` such as:

```
// Generated from foo.proto
pub mod client {
  //...
}

pub mod server {
  //...
}

// Generated from bar.proto
pub mod client {
  //...
}

pub mod server {
  //...
}
```

This change makes it so the name of the service is prepended to
the generated module, and therefore we avoid module name collisions.

After this change, given the two proto files mentioned previously,
we will generate the following modules inside `mypackage.rs`:

```
pub mod foo_client {
 //...
}

pub mod foo_server {
 //...
}

pub mod bar_client {
 //...
}

pub mod bar_server {
 //...
}
```

One codebase where this scenario could be found is in
[Open Match](https://github.com/googleforgames/open-match/tree/master/api)
@LucioFranco
Copy link
Member

Hi @jpgneves thanks for this. This seems mostly good to me but I'd like to see what @jen20 thinks.

@alce
Copy link
Collaborator

alce commented Nov 17, 2019

LGTM. I tried this patch on a server with multiple services. Everything worked out of the box with minor changes and enables more options to organize proto definitions/modules.

@jen20
Copy link
Contributor

jen20 commented Nov 19, 2019

I think this is a solid improvement, I actually hit this case the other day.

@LucioFranco
Copy link
Member

Hey @jpgneves sorry for going silent on this, I'd like to get this merged, would you mind fixing the conflicts, and then we can merge it? :)

@LucioFranco
Copy link
Member

Thank you for this contribution! I merged this in #173!

@jpgneves
Copy link
Author

Apologies for the radio silence myself! Glad to see this in! Thank you! 🎉

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

Successfully merging this pull request may close these issues.

4 participants