Replies: 1 comment 1 reply
-
I agree that we need to talk about this. Sorry for the delay! Merging #167 for now – we can revisit this! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem
What guide do we give for naming protocol buffer files? The overarching goals are:
The concrete goals are:
How Protocol Buffers work
A protocol buffer project consists of multiple files:
Every single file declares a package name, which might be like
my_package.sub_package
. When you import the file, any types defined in this file are then prefixed with the package path.In this example, the full name of the
MyMessage
type ismy_package.sub_package.MyMessage
.Usually, when you use a protocol buffer type, you know the full path and you need to import it. To import it, you need to know which file it is defined in. By default, there is no requirement that the package name inside the file (
my_package.sub_package
) has anything to do with the file name. So you could have a file namedproto/physics/temperature.rs
which declares that it is the package namedmusic_library.jazz
.What we want to achieve is having some kind of correspondence between file names and package names to fulfil goals 1 and 2.
Previous Ideas
We have had some ideas previously, with varying levels of success. The challenge is that we need to choose a path that allows people to keep using patterns that they are currently using.
Strict mapping
Previously, an idea I suggested is to just have a one-to-one correspondence between package names and file paths. This means that every package name can appear in only a single file, and all types must be defined in that file. For example:
Downsides
For example, if you want to split your
mypackage.sub
into multiple files, you could do this:But these two files would not be able to be in the
mypackage.sub
package, due to their path, but would be inmypackage.sub.this
andmypackage.sub.that
respectively, which might not be what you want. Also, these "internal" modules would still be part of the public API.Non-strict mapping
The next idea we have (and currently try to implement) is to have a non-strict mapping. This means that multiple files may declare the same package name.
Example:
Downsides
What makes this approach suboptimal is a few reasons:
fancylib.sub
is defined in bothproto/sub/one.proto
andproto/sub/two.proto
. Which one do you import? You don't know, unless you read the code. This breaks goals (1) and (2)My Suggestion
I feel like I have come up with an approach that eliminates the downsides of both previous approaches, while preserving the upsides of both.
We have a strict mapping of package name to file name.
We allow for having project-internal "hidden" files. These are prefixed with an underscore!
The idea here is that we have a public API, whereby there is a simple mapping between package name and file name:
im_test_lib
inroot.proto
im_test_lib.mass
inmass.proto
im_test_lib.temperature
intemperature.proto
im_test_lib.units
inunits.proto
But additionally, there are also implementation details, which is files that start with an underscore character:
mass/_this.proto
, which is re-exported bymass.proto
mass/_that.proto
, which is re-exported bymass.proto
temperature/_kelvin.proto
, which is re-exported bytemperature.proto
temperature/_fahrenheit.proto
, which is re-exported bytemperature.proto
These should not be used externally, they should only be used by the current package to re-export types from into the proper file. The idea here is:
mod xyz
in Rust: private module (implementation detail, not usable from outside).import
ing hidden files from other packages (to enforce that you do not use the non-public API).This accomplishes:
mypackage.sub.ject
, you know you need to importmypackage/sub/ject.proto
, you do not have to look it up to find out that the specific type you need is defined inmypackage/sub/ject/somefile.proto
. Goal (1) and (2).What do you think about this idea, @JoeHut and @mara-schulke?
Beta Was this translation helpful? Give feedback.
All reactions