Skip to content

Commit

Permalink
Merge pull request #274 from twitchtv/version_constant_for_backwards_…
Browse files Browse the repository at this point in the history
…compatibility

Add version_constant and check from generated code
  • Loading branch information
marioizquierdo authored Sep 15, 2020
2 parents bc4f4e8 + 995f6b3 commit 01dd2f0
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

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

6 changes: 6 additions & 0 deletions example/service.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/empty_service/empty_service.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/gogo_compat/service.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/google_protobuf_imports/service.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/importable/importable.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/importer/importer.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/importer_local/importer_local.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/importmapping/x/x.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/multiple/multiple1.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/multiple/multiple2.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/no_package_name/no_package_name.twirp.go

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

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

6 changes: 6 additions & 0 deletions internal/twirptest/proto/proto.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/service.twirp.go

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

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

6 changes: 6 additions & 0 deletions internal/twirptest/snake_case_names/snake_case_names.twirp.go

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

6 changes: 6 additions & 0 deletions internal/twirptest/source_relative/source_relative.twirp.go

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

10 changes: 10 additions & 0 deletions protoc-gen-twirp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func (t *twirp) generate(file *descriptor.FileDescriptorProto) *plugin.CodeGener
t.generateUtilImports()
}

t.generateVersionCheck(file)

// For each service, generate client stubs and server
for i, service := range file.Service {
t.generateService(file, service, i)
Expand All @@ -243,6 +245,14 @@ func (t *twirp) generate(file *descriptor.FileDescriptorProto) *plugin.CodeGener
return resp
}

func (t *twirp) generateVersionCheck(file *descriptor.FileDescriptorProto) {
t.P(`// This is a compile-time assertion to ensure that this generated file`)
t.P(`// is compatible with the twirp package used in your project.`)
t.P(`// A compilation error at this line likely means your copy of the`)
t.P(`// twirp package needs to be updated.`)
t.P(`const _ = `, t.pkgs["twirp"], `.TwirpPackageIsVersion7`)
}

func (t *twirp) generateFileHeader(file *descriptor.FileDescriptorProto) {
t.P("// Code generated by protoc-gen-twirp ", gen.Version, ", DO NOT EDIT.")
t.P("// source: ", file.GetName())
Expand Down
19 changes: 19 additions & 0 deletions version_constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the License is
// located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package twirp

// TwirpPackageIsVersion7 is a constant referenced from generated code that
// requires features on this version of the twirp package. Older versions
// don't have this constant defined, causing a compile-time assetion error.
const TwirpPackageIsVersion7 = true

0 comments on commit 01dd2f0

Please sign in to comment.