Commit Graph

42 Commits

Author SHA1 Message Date
Damien Neil
daa4fad986 cmd/protoc-gen-go: remove spurious . from enums with no package
Enums, for historical reasons, are registered with the proto package
under the name "<proto_package>.<go_type_name>". Don't include the dot
if there is no package statement in the .proto source file.

Change-Id: I6fb57d0803506668f60123a29fa06ae87fec523b
Reviewed-on: https://go-review.googlesource.com/c/140657
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-10-16 16:04:35 +00:00
Damien Neil
09f89d0246 protoc-gen-go: generate per-field comments for oneofs
Comment location and formatting matches previous generator.

Change-Id: Id51489bb8ca6e6bbd2c7bc2129a7bc170e56ed44
Reviewed-on: https://go-review.googlesource.com/c/140617
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-10-16 16:04:15 +00:00
Damien Neil
a4a765351c cmd/protoc-gen-go: remove spurious space in "* T"
From:
  func (* T) isT_F() {}

To:
  func (*T) isT_F() {}

Formatting the file removes the space, but the presence or absence of
the space affects the formatter's decision on whether to split the {}
into {\n} or not.

Change-Id: I794c855a3115f9ae1b5f048728d8cad7a5f03e69
Reviewed-on: https://go-review.googlesource.com/c/140637
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-10-08 21:34:46 +00:00
Damien Neil
b9781cc938 cmd/protoc-gen-go: convert field number to uint64 before shifting
Avoid generating invalid tag and wire size for large oneof field numbers
which overflow int32.

Change-Id: I005fe32aba4944b33b6b6ba83ef0ddd4d6e5863b
Reviewed-on: https://go-review.googlesource.com/138519
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 22:01:48 +00:00
Damien Neil
ccf3fa6cad cmd/protoc-gen-go: don't generate default values for [default=""].
The previous generator considers a default value of "" (valid only for
string and bytes fields) to be unset, and does not generate a const or
var to hold the default value.

e.g.,

  message M {
    optional F string = 1 [default=""];
  }

does not generate this constant, even though the field has a default
value:

  const Default_M_F string = ""

Maintain consistent output.

Change-Id: Ib172b02d59c15c05e19a7056d05ce1c619a2fa40
Reviewed-on: https://go-review.googlesource.com/138518
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 21:56:28 +00:00
Damien Neil
ea7baf45f8 cmd/protoc-gen-go: annotate all well-known-types
Change-Id: I3559bb47cb8e93aadeea6224857d65e84ca4b27a
Reviewed-on: https://go-review.googlesource.com/138517
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 21:56:15 +00:00
Damien Neil
183aa3a3e6 cmd/protoc-gen-go-grpc: add deprecation comments
Change-Id: I7cf579acb11eeb4e56ec3dd4baec08758b8dc7e1
Reviewed-on: https://go-review.googlesource.com/138376
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 21:55:39 +00:00
Damien Neil
982684b22e cmd/protoc-gen-go: format default float values as float32
Printing default float values as float64 results in e.g., "3.14"
becoming "3.140000104904175".

Change-Id: I5b21a1f94084b6d9c7b015b34d911972c4a2a1b2
Reviewed-on: https://go-review.googlesource.com/138516
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 21:20:49 +00:00
Damien Neil
1fa8ab0ed5 protogen: add an option to rewrite import paths
This allows us to implement the import_prefix parameter in the v1
protoc-gen-go.

Drop support for import_prefix in protogen, and explicitly produce an
error if it is used in the v2 protoc-gen-go.

Change-Id: I66136b6b3affa3c0e9a93dc565619c90c42c0ecc
Reviewed-on: https://go-review.googlesource.com/138257
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-28 21:19:59 +00:00
Damien Neil
9c420a66b7 cmd/protoc-gen-go: expose internal_gengo.GenerateFile
Move things around a little bit to allow the v1 protoc-gen-go to support
plugins=grpc.

Change-Id: I98d1bb86828450afe7915b1fefaf22bb7915cf44
Reviewed-on: https://go-review.googlesource.com/138256
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-27 22:53:23 +00:00
Damien Neil
2dc6718b59 cmd/protoc-gen-go-grpc: add gRPC code generator
This is a straight translation of the v1 API gRPC "plugin" to protogen.

Add a protoc-gen-go-grpc command. The preferred way to generate gRPC
services is to invoke both plugins separately:

  protoc --go_out=. --go-grpc_out=. foo.proto

When invoked in this fashion, the generators will produce separate
foo.pb.go and foo_grpc.pb.go files.

Change-Id: Ie180385dab3da7063db96f7c2f9de3abbd749f63
Reviewed-on: https://go-review.googlesource.com/137037
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-27 19:32:34 +00:00
Damien Neil
0d1a064764 cmd/protoc-gen-go: rename protoc-gen-go.go -> main.go
Name protoc-gen-go.go was temporarily used to keep git from getting
confused about the revision history of main.go.

Change-Id: I3b20ff93f750eaf54dcf3c5c3488d465fba5eaa1
Reviewed-on: https://go-review.googlesource.com/137036
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-24 21:08:57 +00:00
Damien Neil
1adaec9cae cmd/protoc-gen-go: factor out internals into a new package
To permit the api-v1 branch to import the protoc-gen-go internals, move
them into a quasi-internal package.

Change-Id: I64e50ee299b99da1f648f7abb6cd0347a13e06e3
Reviewed-on: https://go-review.googlesource.com/137035
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-24 21:02:31 +00:00
Damien Neil
d39efc8068 cmd/protog-gen-go: rename File to fileInfo
In preparation for factoring out the protoc-gen-go implementation into an
importable package, change exported names (just File) to unexported ones.

Change-Id: I2c65913332447e75c0dc7622a56f5dc09ff90116
Reviewed-on: https://go-review.googlesource.com/137017
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-24 19:44:49 +00:00
Damien Neil
154da98aa5 cmd/protoc-gen-go: special cases for MessageSet extensions
Add special-case handling for extension fields named
"message_set_extension" that extend a message_set_wire_format message.

Support special cases for a proto1 feature that was superseded by proto2
extensions.

Change-Id: Icbdb711111c66be547bf8d6f37ab3079c320e2a1
Reviewed-on: https://go-review.googlesource.com/136536
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-24 17:25:31 +00:00
Joe Tsai
01ab29648e go.mod: rename google.golang.org/proto as github.com/golang/protobuf/v2
This change was created by running:
	git ls-files | xargs sed -i "s|google.golang.org/proto|github.com/golang/protobuf/v2|g"

This change is *not* an endorsement of "github.com/golang/protobuf/v2" as the
final import path when the v2 API is eventually released as stable.
We continue to reserve the right to make breaking changes as we see fit.

This change enables us to host the v2 API on a repository that is go-gettable
(since go.googlesource.com is not a known host by the "go get" tool;
and google.golang.org/proto was just a stub URL that is not currently served).
Thus, we can start work on a forked version of the v1 API that explores
what it would take to implement v1 in terms of v2 in a backwards compatible way.

Change-Id: Ia3ebc41ac4238af62ee140200d3158b53ac9ec48
Reviewed-on: https://go-review.googlesource.com/136736
Reviewed-by: Damien Neil <dneil@google.com>
2018-09-24 16:11:50 +00:00
Damien Neil
a6c374a8f1 cmd/protoc-gen-go: add test data for proto3 extensions
Change-Id: I2d47db2c177553dc2a3dacc66c70db579c1145fd
Reviewed-on: https://go-review.googlesource.com/136535
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-20 19:56:44 +00:00
Damien Neil
d4803f5a4f cmd/protoc-gen-go: add "packed" encoding tag to fields
Change-Id: I423039d6c9edb50a07781a6e76336ed364f21866
Reviewed-on: https://go-review.googlesource.com/136359
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-20 17:08:44 +00:00
Damien Neil
55fe1c0178 cmd/protoc-gen-go: generate deprecation comments
Generate deprecation comments on packages, enums, enum values, messages,
and fields.

Change-Id: I8a94aff535078d33d1cc6104cff17e062cbfe94f
Reviewed-on: https://go-review.googlesource.com/136355
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-19 22:46:31 +00:00
Damien Neil
81d6d83a09 cmd/protoc-gen-go: additional conflict resolution for oneof field types
This is purely for consistent output with the previous protoc-gen-go.

Consider this message:

  message M {
    oneof union { string conflict = 1; }
    message Conflict {}
  }

The type for the wrapper of M.conflict will have the same name as the
embedded message type for M.Conflict.

The previous protoc-gen-go performs a disambiguation step where it adds
_s to the names of oneof field types until they have no conflicts with
nested messages or enums in the same message as the field.

There are a number of ways in which this can fail, of course. Preserve
the behavior for now.

Change-Id: I78a1c6588b577324e003b8bc337b75bb363432ba
Reviewed-on: https://go-review.googlesource.com/136357
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-19 21:15:24 +00:00
Damien Neil
2e0c3da5a4 cmd/protoc-gen-go: generate blank imports for unused proto dependencies
Generate Go imports for all packages imported by the .proto source file,
even if they are not referenced.

Change-Id: I116bdf460ab441d205b42606b2b05b315ed68954
Reviewed-on: https://go-review.googlesource.com/136358
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-19 21:12:48 +00:00
Damien Neil
73ac885dab cmd/protoc-gen-go: generate public imports
Generate forwarding declarations for all types, consts, and vars in
publicly imported files.

Change-Id: I2f1041fa91b0ae18b1e123935951618b8d594f84
Reviewed-on: https://go-review.googlesource.com/136175
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-19 15:37:56 +00:00
Damien Neil
993c04dcf2 cmd/protoc-gen-go: generate extensions
Generate everything related to extensions: extension descriptors,
XXX_InternalExtensions fields, etc.

Tweak the order in which we generate code for descriptors to ensure
consistent output with the previous protoc-gen-go.

Change Field.ContainingType to Field.ParentMessage, since we need to get
at the parent of both message fields and extensions (and the "containing
type" of an extension field is the extended message, under existing
terminology).

Change-Id: I5d045ca80536436e7c987bca3d8fb8c1e1521e55
Reviewed-on: https://go-review.googlesource.com/136155
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-18 22:49:53 +00:00
Damien Neil
1fa78d8a35 cmd/protoc-gen-go: generate oneofs
Generate everything related to oneofs: Message struct fields, wrapper
types, XXX_OneofFuncs.

Change-Id: I409040e0deb5716afabf59186eeaae21757d29f1
Reviewed-on: https://go-review.googlesource.com/135535
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-18 21:10:04 +00:00
Damien Neil
1ec3315873 protoc-gen-go: generate imports consistent with previous protoc-gen-go
Include references to the proto, fmt, and math packages, consistent with
the ones in the previous generator:

	// Reference imports to suppress errors if they are not otherwise used.
	var _ = proto.Marshal
	var _ = fmt.Errorf
	var _ = math.Inf

Sort imports with ast.SortImports to ensure gofmt-compatible output.

Copy the protoc-gen-go/testdata/imports test files from
github.com/golang/protobuf.

Change-Id: I263e2ac750d03596ae9cb23609049d5ef1016205
Reviewed-on: https://go-review.googlesource.com/135361
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-14 22:40:55 +00:00
Damien Neil
ce36f8d1b4 cmd/protoc-gen-go: register messages and map field types
Move generation of the init function that registers all the types in a
file into a single function.

Take some care to generate the registrations in the same order as the
previous protoc-gen-go, to make it easier to catch unintended
differences in output.

For the same reason, adjust the order of generation to generate all
enums before all messages (matches previous behavior).

Change-Id: Ie0d574004d01a16f8d7b10be3882719a3c41676e
Reviewed-on: https://go-review.googlesource.com/135359
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-14 22:26:09 +00:00
Damien Neil
0bd5a38474 cmd/protoc-gen-go: add support for map fields
Generate the proper map[key]value type for map fields.
Include the protobuf_key and protobuf_val field tags.
Do not generate the map entry structs.

Fix an initialization order bug in protogen: While proto files cannot
contain circular dependencies, a single file can contain dependency
cycles. First create types for all the descriptors in a file, then add
in references (currently just field->message and field->enum) in a
second pass.

Change-Id: Ifedfa657d8dbb00413ba493adee1119b19c1b773
Reviewed-on: https://go-review.googlesource.com/135355
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-13 22:20:09 +00:00
Damien Neil
77f82fe6bc cmd/protoc-gen-go: generate getters
Refactor fieldGoType to return the non-pointer type for scalar types
(i.e., "int", not "*int"), and a bool indicating whether the struct
field is a pointer to that type.

Change-Id: Ic80220e92f0b190e41ead847440d57af5c6cc919
Reviewed-on: https://go-review.googlesource.com/135336
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-13 20:51:57 +00:00
Damien Neil
ebc699d099 cmd/protoc-gen-go: generate def= protobuf field tag, default constants
Change-Id: Id5cc34f0c1a5eb72f19e648844a5480827bcbda3
Reviewed-on: https://go-review.googlesource.com/135256
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-13 19:34:28 +00:00
Damien Neil
a1c6abc906 cmd/protoc-gen-go: generate common message methods
Generate the standard Reset, String, ProtoMessage, and Descriptor
methods for each message struct.

Generate the table-driven support methods (XXX_Marshal, et al.).

Change-Id: I8a5ed8af1433b481e2458ad534456692d6f74c1f
Reviewed-on: https://go-review.googlesource.com/134996
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-12 22:18:32 +00:00
Damien Neil
d412792d34 cmd/protoc-gen-go: reference proto.ProtoPackageIsVersionX
Include the compile-time assertion that the generated file is compatible
with the proto package it is being compiled against.

Change-Id: Iefa27ee3a99a6669a93303b6674b0033794ba0be
Reviewed-on: https://go-review.googlesource.com/134995
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-12 20:08:45 +00:00
Damien Neil
658051ba78 cmd/protoc-gen-go: generate message fields
This produces exactly the same output as github.com/golang/protobuf.

Change-Id: I01aacc9277c5cb5b4cc295f5ee8af12b4a524781
Reviewed-on: https://go-review.googlesource.com/134955
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-12 18:08:39 +00:00
Damien Neil
3cf6e62f69 protogen: support passing command-line parameters to plugins
Add a protogen.Options struct for future expansion. Include a FlagSet
which will be populated with parameters passed to the plugin.

Change-Id: I26a13bbde7ce011135b9c151edd160f3b51b7f9a
Reviewed-on: https://go-review.googlesource.com/134696
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-11 22:07:12 +00:00
Damien Neil
46abb57549 cmd/protoc-gen-go: generate enums
This produces exactly the same output (to the best of my ability to
determine) as github.com/golang/protobuf.

Change-Id: Ib60e7a836efb1eb0e5167b30458049ec239e7903
Reviewed-on: https://go-review.googlesource.com/134695
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-11 21:42:46 +00:00
Damien Neil
cab8dfee90 protogen: include source comments in generated code
Add initial handling of location paths (arrays of integers identifying
an entity in a .proto source file).

Expose path info in protogen; each descriptor has a Path field containing
its location path.

Format comments in protoc-gen-go. This contains one change from
github.com/golang/protobuf: Package comments are now included before the
package statement (but not attached to it) and use // comment syntax
instead of /* */. e.g.,

Before:

	package test

	/*
	This package contains interesting messages.
	*/

After:

	// This package contains interesting messages.

	package test

Change-Id: Ieee13ae77b3584f7562183100554d3df732348aa
Reviewed-on: https://go-review.googlesource.com/133915
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-11 17:19:32 +00:00
Damien Neil
7779e05b0a cmd/protoc-gen-go: register FileDescriptorProto
Generate the gzipped FileDescriptorProto var, and register it with
proto.RegisterFile at init time.

Change-Id: Ie232f20412ca9cd7bde91aaba7127dc181e9758c
Reviewed-on: https://go-review.googlesource.com/134118
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-10 20:31:17 +00:00
Damien Neil
082ce923d3 protogen: compute package names, import paths, generated filenames
Copy/duplicate the logic in github.com/golang/protobuf for computing
package names and import paths and the names of generated files.

This is all sufficiently complicated that the code is the best
documentation. In practice, users should always set a go_package option
containing an import path in every file and pass the
paths=source_relative generator flag to get reasonable behavior.

Change-Id: I34ae38fcc8db6909a4b25b16c73b982a7bad0463
Reviewed-on: https://go-review.googlesource.com/133876
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-07 17:58:10 +00:00
Joe Tsai
c84688961e cmd/protoc-gen-go: assume type alias support in golden_test
The v2 API already assumes that type aliases are available elsewhere.
Thus, we can drop this dynamic check for type alias support.

Change-Id: Ia4deab8f22384a982642da4a9aa5166870822b7d
Reviewed-on: https://go-review.googlesource.com/133877
Reviewed-by: Damien Neil <dneil@google.com>
2018-09-06 20:08:07 +00:00
Damien Neil
abc6fc1ff9 protogen: use protoreflect descriptors
Change the protogen types wrapping FileDescriptorProto et al. to use
protoreflect descriptors instead.

Change-Id: I99fe83b995a0a6f4fc88f03a6e4b827109a2ec80
Reviewed-on: https://go-review.googlesource.com/133815
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-06 17:45:34 +00:00
Damien Neil
d901677135 protogen: automatic handling of imports
The GoIdent type is now a tuple of import path and name. Generated files
have an associated import path. Writing a GoIdent to a generated file
qualifies the name if the identifier is from a different package.
All necessary imports are automatically added to generated Go files.

Change-Id: I839e0b7aa8ec967ce178aea4ffb960b62779cf74
Reviewed-on: https://go-review.googlesource.com/133635
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-05 23:14:28 +00:00
Damien Neil
c7d07d9ba5 protogen: generate message skeletons
Copy generator.CamelCase for camel-casing names, with one change: Convert
'.' in names to '_'. This removes the need for the CamelCaseSlice function
which operates on a []string representing a name split along '.'s.

Add protogen.Message.

Reformat generated code.

Add regenerate.bash, largely copied from regenerate.sh.

Change-Id: Iecf0bfc43b552f53e458499a328b933b0c9c5f82
Reviewed-on: https://go-review.googlesource.com/130915
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-08-23 22:10:13 +00:00
Damien Neil
220c20246b protogen, cmd/protoc-gen-go: initial commit
Package protogen provides support for writing protoc plugins.
A "plugin" in this case is a program run by protoc to generate output.

The protoc-gen-go command is a protoc plugin to generate Go code.

cmd/protoc-gen-go/golden_test.go is mostly a straight copy from
the golden test in github.com/golang/protobuf.

Change-Id: I332d0df1e4b60bb8cd926320b8721e16b99a4b71
Reviewed-on: https://go-review.googlesource.com/130175
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-08-22 17:08:04 +00:00