Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
Guiding Principles:

Changelogs are for humans, not machines.
There should be an entry for every single version.
The same types of changes should be grouped.
Versions and sections should be linkable.
The latest version comes first.
The release date of each version is displayed.
Mention whether you follow Semantic Versioning.

Usage:

Change log entries are to be added to the Unreleased section under the
appropriate stanza (see below). Each entry should ideally include a tag and
the Github issue reference in the following format:

* (<tag>) \#<issue-number> message

The issue numbers will later be link-ified during the release process so you do
not have to worry about including a link manually, but you can if you wish.

Types of changes (Stanzas):

"Features" for new features.
"Improvements" for changes in existing functionality.
"Deprecated" for soon-to-be removed features.
"Bug Fixes" for any bug fixes.
"Client Breaking" for breaking Protobuf, gRPC and REST routes used by end-users.
"CLI Breaking" for breaking CLI commands.
"API Breaking" for breaking exported APIs used by developers building on SDK.
Ref: https://keepachangelog.com/en/1.0.0/
-->

# Changelog

## [Unreleased]

### Features

* [#82](https://github.com/cosmos/cosmos-proto/pull/82) generated code in `internal` go packages is not registered with the global protobuf registry allowing modules to have internal protobuf generated types that are not exposed in public APIs and do not affect global protobuf registration.
17 changes: 17 additions & 0 deletions features/protoc/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package protoc

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIsInternalPackage(t *testing.T) {
require.True(t, isInternalPackage("internal"))
require.True(t, isInternalPackage("example/internal"))
require.True(t, isInternalPackage("example.com/internal"))
require.True(t, isInternalPackage("example.com/internal/foo"))
require.False(t, isInternalPackage("example"))
require.False(t, isInternalPackage("example.com"))
require.False(t, isInternalPackage("example.com/false"))
}
30 changes: 25 additions & 5 deletions features/protoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@ package protoc

import (
"fmt"
"github.com/cosmos/cosmos-proto/features/protoc/genid"
"github.com/cosmos/cosmos-proto/generator"
"go/ast"
"go/parser"
"go/token"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"
"math"
"strconv"
"strings"
"unicode"
"unicode/utf8"

"golang.org/x/exp/slices"

"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/proto"

"github.com/cosmos/cosmos-proto/features/protoc/genid"
"github.com/cosmos/cosmos-proto/generator"

pref "google.golang.org/protobuf/reflect/protoreflect"

"github.com/cosmos/cosmos-proto/features/protoc/version"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"

"github.com/cosmos/cosmos-proto/features/protoc/version"

"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
Expand Down Expand Up @@ -60,6 +65,7 @@ var (
protojsonPackage goImportPath = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
protoreflectPackage goImportPath = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoreflect")
protoregistryPackage goImportPath = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoregistry")
runtimePackage = protogen.GoImportPath("github.com/cosmos/cosmos-proto/runtime")
)

// GenerateFile generates the contents of a .pb.go file.
Expand Down Expand Up @@ -360,6 +366,8 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *generator.GeneratedFile,
}
}

isInternal := isInternalPackage(f.GoImportPath.String())

g.P("type x struct{}")
g.P("out := ", protoimplPackage.Ident("TypeBuilder"), "{")
g.P("File: ", protoimplPackage.Ident("DescBuilder"), "{")
Expand All @@ -369,6 +377,9 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *generator.GeneratedFile,
g.P("NumMessages: ", len(f.allMessages), ",")
g.P("NumExtensions: ", len(f.allExtensions), ",")
g.P("NumServices: ", len(f.Services), ",")
if isInternal {
g.P("FileRegistry: ", runtimePackage.Ident("NullRegistry"), "{},")
}
g.P("},")
g.P("GoTypes: ", goTypesVarName(f), ",")
g.P("DependencyIndexes: ", depIdxsVarName(f), ",")
Expand All @@ -381,6 +392,9 @@ func genReflectFileDescriptor(gen *protogen.Plugin, g *generator.GeneratedFile,
if len(f.allExtensions) > 0 {
g.P("ExtensionInfos: ", extensionTypesVarName(f), ",")
}
if isInternal {
g.P("TypeRegistry: ", runtimePackage.Ident("NullRegistry"), "{},")
}
g.P("}.Build()")
g.P(f.GoDescriptorIdent, " = out.File")

Expand Down Expand Up @@ -2287,3 +2301,9 @@ func newFileInfo(file *protogen.File) *fileInfo {

return f
}

// checks whether the package path is an internal package
func isInternalPackage(pkg string) bool {
pathParts := strings.Split(pkg, "/")
return slices.Contains(pathParts, "internal")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/google/go-cmp v0.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0
google.golang.org/protobuf v1.34.0
gotest.tools/v3 v3.5.1
pgregory.net/rapid v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0 h1:17k44ji3KFYG94XS5QEFC8pyuOlMh3IoR+vkmTZmJJs=
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
16 changes: 16 additions & 0 deletions internal/testprotos/test3/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test3

import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoregistry"
)

func TestInternalNotRegistered(t *testing.T) {
_, err := protoregistry.GlobalTypes.FindMessageByName((&TestAllTypes{}).ProtoReflect().Descriptor().FullName())
require.Error(t, err)

_, err = protoregistry.GlobalFiles.FindFileByPath("internal/testprotos/test3/test_import.proto")
require.Error(t, err)
}
2 changes: 2 additions & 0 deletions internal/testprotos/test3/test.pulsar.go

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

2 changes: 2 additions & 0 deletions internal/testprotos/test3/test_import.pulsar.go

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

2 changes: 2 additions & 0 deletions internal/testprotos/test3/test_nesting.pulsar.go

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

23 changes: 23 additions & 0 deletions runtime/nullregistry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package runtime

import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)

// NullRegistry is an implementation of the interfaces used to register generated
// types and file descriptors that does nothing. This is used to generated
// protobuf files in internal packages that are not registered with the global registry.
type NullRegistry struct{}

func (NullRegistry) RegisterMessage(protoreflect.MessageType) error { return nil }
func (NullRegistry) RegisterEnum(protoreflect.EnumType) error { return nil }
func (NullRegistry) RegisterExtension(protoreflect.ExtensionType) error { return nil }
func (NullRegistry) RegisterFile(protoreflect.FileDescriptor) error { return nil }

func (NullRegistry) FindFileByPath(path string) (protoreflect.FileDescriptor, error) {
return protoregistry.GlobalFiles.FindFileByPath(path)
}
func (NullRegistry) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
return protoregistry.GlobalFiles.FindDescriptorByName(name)
}