Skip to content

Commit

Permalink
Add compatibility markers to proto generated code.
Browse files Browse the repository at this point in the history
This asserts at build time that the generated code is compatible with
the proto package that it is linked to.
  • Loading branch information
dsymonds committed Jan 6, 2016
1 parent 4df416c commit 2402d76
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jsonpb/jsonpb_test_proto/more_test_objects.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proto/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,7 @@ func isProto3Zero(v reflect.Value) bool {
}
return false
}

// ProtoPackageIsVersion1 is referenced from generated protocol buffer files
// to assert that that code is compatible with this version of the proto package.
const ProtoPackageIsVersion1 = true
4 changes: 4 additions & 0 deletions proto/testdata/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protoc-gen-go/descriptor/descriptor.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions protoc-gen-go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ import (
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
)

// generatedCodeVersion indicates a version of the generated code.
// It is incremented whenever an incompatibility between the generated code and
// proto package is introduced; the generated code references
// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
const generatedCodeVersion = 1

// A Plugin provides functionality to add to the output during Go code generation,
// such as to produce RPC stubs.
type Plugin interface {
Expand Down Expand Up @@ -1122,6 +1128,14 @@ func (g *Generator) generate(file *FileDescriptor) {
g.file = g.FileOf(file.FileDescriptorProto)
g.usedPackages = make(map[string]bool)

if g.file.index == 0 {
// For one file in the package, assert version compatibility.
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the proto package it is being compiled against.")
g.P("const _ = ", g.Pkg["proto"], ".ProtoPackageIsVersion", generatedCodeVersion)
g.P()
}

for _, td := range g.file.imp {
g.generateImported(td)
}
Expand Down
4 changes: 4 additions & 0 deletions protoc-gen-go/plugin/plugin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protoc-gen-go/testdata/my_test/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions protoc-gen-go/testdata/my_test/test.pb.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
const _ = proto.ProtoPackageIsVersion1

type HatType int32

const (
Expand Down

0 comments on commit 2402d76

Please sign in to comment.