Commit Graph

20 Commits

Author SHA1 Message Date
Damien Neil
e358d430e7 compiler/protogen: disable warnings in tests
Change-Id: I80ff3c31dc1039a833e6c962d5f3fc6e0aa46f6a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/222377
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Herbie Ong <herbie@google.com>
2020-03-09 17:16:35 +00:00
Joe Tsai
ab0ca4ff8a compiler/protogen: use consistent options pattern
Throughout the module options usually expressed as a struct where the
acting function is a method hanging off the options type.
Use this pattern for protogen as well.

Change-Id: I533a61387cb74971e4efc9313d400b66b8aac451
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/221424
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-28 23:03:10 +00:00
Joe Tsai
e0daf31d84 all: trivial formatting changes
Changes:
* import grouping for third-party dependencies
* import grouping for generated protobufs
* blank space removal

Change-Id: I2950b0606bb2064046d79a23a78b05c23147cbfe
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/221017
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-25 21:59:54 +00:00
Joe Tsai
222a0006a6 compiler/protogen: add devsite URL to protogen warnings
Change-Id: Ida47b31a4f62d00d9f756da6da98cd852f36bb69
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/220594
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-24 20:14:07 +00:00
Joe Tsai
04f03cb038 compiler/protogen: improve documentation
Change-Id: I68e918d297716178bc1d9f459edb1cd1678d4159
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/220340
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-20 19:04:19 +00:00
Joe Tsai
25fc6fb4f2 reflect/protodesc: add FileOptions
The FileOptions type provides the ability to specify specialized options
for how a file descriptor is constructed. It follows the same optional
arguments pattern as used in the proto package.

The resolver is not an option since it almost always necessary
when constructing a file descriptor.

Change-Id: Ib98ac6289881ad8402dd615f6c895da5899cb8d9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/218940
Reviewed-by: Damien Neil <dneil@google.com>
2020-02-11 23:14:57 +00:00
Damien Neil
c826885a2a reflect/protoregistry: add (*Types).Register{Message,Enum,Extension}
Add type-safe methods to register message, enum, and extension types.
Deprecate the NewTypes function and the (*Types).Register method.

Add (*File).RegisterFile and deprecate the NewFiles function and
the (*File).Register method.

Updates golang/protobuf#963

Change-Id: Ie89e77526e0874539e9bd929ca0ba8d758e65a6e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/199898
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-10-31 16:23:42 +00:00
Joe Tsai
3e80249d38 compiler/protogen: deprecate certain ways to derive Go package information
There are currently at least 5 ways to derive Go package information:
* From the 'M' command-line flag.
* From the 'import_path' command-line flag.
* From the 'go_package' proto file option.
* From the proto package name in proto source file.
* From the path of the proto source file.
Technically, there are more than 5 ways since information can be derived
from a convoluted combination of all the methods.

We should move towards a sensible and consistent world where each
Go package information for each proto source file is:
* only derived from the command-line OR
* only derived from the proto source file itself.
It should never be derived from a mixture of methods.

In the future, all .proto source files will be required to have a
"go_package" option specified, unless the "M" command-line argument is
specified. If the "M" flag is given it takes precedence over 'go_package'.

This CL prints warnings if the user is generating proto files without
a "M" flag or "go_package" option specified. It suggests to the user
a "go_package" option that preserves the current semantics.

Change-Id: I5cf8d40a245146bb145b3b610d42f1bcd140b449
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/194158
Reviewed-by: Damien Neil <dneil@google.com>
2019-09-09 23:57:33 +00:00
Joe Tsai
2e7817f117 compiler/protogen, internal/strs, internal/impl: expose enum Go name derivation
In order to migrate v1 to wrap v2, we need a way to reproduce
the awful enum "names" that v1 used, which was the concatenation of
the proto package with the Go identifier used for the enum.

To support this:
* Move the camel case logic from compiler/protogen to internal/strs
* Add a small stub in internal/impl to expose this functionality

Change-Id: I8ff31daa9ae541e5788dc04d2e89eae1574877e4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191637
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-26 17:49:17 +00:00
Joe Tsai
ef6e524dca compiler/protogen: move name mangling logic to protogen
The name mangling logic should be unified in a single place
rather than being split between compiler/protogen and cmd/protoc-gen-go.
Move it over compiler/protogen.

Change-Id: Iea65e0b3fba45e0c95c76e3fc1f061e0fa8f84d7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191117
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-21 20:49:58 +00:00
Joe Tsai
4df99fd526 compiler/protogen: add EnumValue.Parent
EnumValues, Fields, Oneofs, and Methods are all declarations that exist
within some parent declaration. The later three have a Parent field.
It seems only consistent that EnumValue have one as well.

Change-Id: I774576565046ede4a96f86ceaa446a39613a39f5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191097
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-21 20:40:24 +00:00
Joe Tsai
2cec484ed7 compiler/protogen: export Plugin.FilesByPath
The Plugin.FileByName method seems like an unnecessary indirection that
provides little benefit over just exposing the map.
If the intention to provide some form of immutability, there are already
many leakages in the abstraction given that most things returned by
protogen is by pointer.

Change-Id: I0c793a8d7dab7632b92968a74f8c1ba5accb242f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191039
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-21 20:35:20 +00:00
Joe Tsai
7762ec2098 compiler/protogen: reorder code
This CL reorders code in the protogen package to consistently match
the typical ordering of enum, message, extension, service seen in
the rest of the module.

Change-Id: I42c3a2ab7ff01857ce9a8e0a71c757b7ea791521
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191038
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-21 20:31:39 +00:00
Joe Tsai
70fdd5d140 compiler/protogen, cmd/protoc-gen-go: use alternative comments API
This is a breaking change. High-level protogen API changes:
* remove GeneratedFile.PrintLeadingComments method
* add {Message,Field,Oneof,Enum,EnumValue,Service,Method}.Comments field
* add CommentSet and Comments type

CL/183157 added protoreflect.SourceLocations and it was discovered
that there can actually be duplicate locations for certain paths.
For that reason, we decided not to expose any helper methods
for looking up locations by path since it is unclear which location
to return if multiple matches.

The protogen.GeneratedFile.PrintLeadingComments has a similar dilemma
where it also needs to figure out what to do when duplicates exist.
Previously, it just chooses the first one with comments,
which may not be the right choice in a given context.

Analysis of current PrintLeadingComments usage shows that it is only
ever used (except once) for descriptor declarations.
In the case of descriptor declarations, they are guaranteed by protoc
to have only location.

Thus, we avoid the duplicate location problem by:
* Providing a CommentSet for every descriptor. The CommentSet contains
a set of leading and trailing comments of the Comments type.
* The Comments.String method knows how to interpret the comments
as provided by protoc and format them as // prefixed line comments.
* Values of the Comments type can be passed to the P method.

We drop direct support printing leading comments for non-descriptor locations,
but the exposure of the Comments type makes it easy for users to manually
handle other types of comments themselves.

Change-Id: Id4851456dc4e64d76bd6a30e8ad6137408dfb27a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189198
Reviewed-by: Damien Neil <dneil@google.com>
2019-08-07 06:00:36 +00:00
Joe Tsai
691d8563c7 compiler/protogen: use protoreflect.SourcePath
Change-Id: I3770d8952e12bb12d8bdc326635008843cd8a6af
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/186000
Reviewed-by: Damien Neil <dneil@google.com>
2019-07-15 18:21:33 +00:00
Damien Neil
a8a2cea3e7 proto: move T->*T wrappers from internal/scalar to proto
Usage of these is pervasive in code which works with proto2, and proto2
will be with us for a long, long time to come. Move them to the proto
package.

Change-Id: I1b2e57429fd5a8f107a848a4492d20c27f304bd7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185543
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-07-12 17:35:01 +00:00
Joe Tsai
8d30bbeede all: remove dependency on proto v1
This does not remove all dependencies,
but all of the cases where it can now be implemented in terms of v2.

Change-Id: Idc5b0273f0d35c284bf2141eb9cce998692ceb15
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184878
Reviewed-by: Herbie Ong <herbie@google.com>
2019-07-03 04:59:17 +00:00
Koichi Shiraishi
ea2076d064 compiler/protogen: rename shadowed path package variables
The use of the variable name "path" shadows the "path" package import.
Rename the variable to avoid shadowing.

Change-Id: Iefcd49146753b52cca1ced1d0758699092cbdeb8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/178778
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-01 01:34:16 +00:00
Joe Tsai
a95b29fbf6 types: consistently name generated protos
Rename each generated protobuf package such that the base of the
Go package path is always equal to the Go package name to follow
proper Go package naming conventions.

The Go package name is derived from the .proto source file name by
replacing ".proto" with "pb" and stripping all underscores.

Change-Id: Iea05d1b5d94b1b2821ae10276ab771bb2df93c0e
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177380
Reviewed-by: Damien Neil <dneil@google.com>
2019-05-16 21:55:40 +00:00
Damien Neil
5c5b531562 protogen, encoding/jsonpb, encoding/textpb: rename packages
Rename encoding/*pb to follow the convention of prefixing package names
with 'proto':

	google.golang.org/protobuf/encoding/protojson
	google.golang.org/protobuf/encoding/prototext

Move protogen under a compiler/ directory, just in case we ever do add
more compiler-related packages.

	google.golang.org/protobuf/compiler/protogen

Change-Id: I31010cb5cabcea8274fffcac468477b58b56e8eb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177178
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-05-14 20:33:22 +00:00