Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf v2 migration #113

Merged
merged 15 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
protoc-version: ['3.5.0', '3.6.0', '3.17.0']
env:
GOPATH: ${{ github.workspace }}
GOBIN: ${{ github.workspace }}/bin
defaults:
run:
working-directory: ${{ env.GOPATH }}/src/github.com/lyft/protoc-gen-star
name: protoc version ${{ matrix.protoc-version }}
name: protoc version 3.17.0
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -28,7 +25,7 @@ jobs:
with:
go-version: '1.17'
- run: mkdir -p $GOPATH/bin
- run: wget "https://github.com/protocolbuffers/protobuf/releases/download/v${{ matrix.protoc-version }}/protoc-${{ matrix.protoc-version }}-linux-x86_64.zip" -O /tmp/protoc.zip
- run: wget "https://github.com/protocolbuffers/protobuf/releases/download/v3.17.0/protoc-3.17.0-linux-x86_64.zip" -O /tmp/protoc.zip
- run: unzip /tmp/protoc.zip -d /tmp
- run: sudo mv /tmp/bin/protoc /usr/local/bin/protoc
- run: sudo mv /tmp/include/google /usr/local/include/google
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _PG* is a protoc plugin library for efficient proto-based code generation_
```go
package main

import "github.com/lyft/protoc-gen-star"
import "github.com/lyft/protoc-gen-star/v2"

func main() {
pgs.Init(pgs.DebugEnv("DEBUG")).
Expand Down Expand Up @@ -292,7 +292,7 @@ PG* permits mutating the `Parameters` via the `MutateParams` `InitOption`. By pa

While implemented in Go, PG* seeks to be language agnostic in what it can do. Therefore, beyond the pre-generated base descriptor types, PG* has no dependencies on the protoc-gen-go (PGG) package. However, there are many nuances that each language's protoc-plugin introduce that can be generalized. For instance, PGG package naming, import paths, and output paths are a complex interaction of the proto package name, the `go_package` file option, and parameters passed to protoc. While PG*'s core API should not be overloaded with many language-specific methods, subpackages can be provided that can operate on `Parameters` and `Entities` to derive the appropriate results.

PG* currently implements the [pgsgo](https://godoc.org/github.com/lyft/protoc-gen-star/lang/go/) subpackage to provide these utilities to plugins targeting the Go language. Future subpackages are planned to support a variety of languages.
PG* currently implements the [pgsgo](https://godoc.org/github.com/lyft/protoc-gen-star/v2/lang/go/) subpackage to provide these utilities to plugins targeting the Go language. Future subpackages are planned to support a variety of languages.

## PG* Development & Make Targets

Expand Down
4 changes: 2 additions & 2 deletions artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"path/filepath"
"strings"

"github.com/golang/protobuf/proto"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"google.golang.org/protobuf/proto"
plugin_go "google.golang.org/protobuf/types/pluginpb"
)

// An Artifact describes the output for a Module. Typically this is the creation
Expand Down
6 changes: 3 additions & 3 deletions ast.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pgs

import (
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
descriptor "google.golang.org/protobuf/types/descriptorpb"
plugin_go "google.golang.org/protobuf/types/pluginpb"
)

// AST encapsulates the entirety of the input CodeGeneratorRequest from protoc,
Expand Down Expand Up @@ -101,7 +101,7 @@ func ProcessCodeGeneratorRequestBidirectional(debug Debugger, req *plugin_go.Cod
// malformed or missing dependencies. To generate a self-contained
// FileDescriptorSet, run the following command:
//
// protoc -o path/to/fdset.bin --include_imports $PROTO_FILES
// protoc -o path/to/fdset.bin --include_imports $PROTO_FILES
//
// The emitted AST will have no values in the Targets map, but Packages will be
// populated. If used for testing purposes, the Targets map can be manually
Expand Down
6 changes: 3 additions & 3 deletions ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"path/filepath"
"testing"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
descriptor "google.golang.org/protobuf/types/descriptorpb"

"github.com/golang/protobuf/proto"
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
plugin_go "google.golang.org/protobuf/types/pluginpb"
)

func readCodeGenReq(t *testing.T, dir string) *plugin_go.CodeGeneratorRequest {
Expand Down
6 changes: 2 additions & 4 deletions entity.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pgs

import (
"github.com/golang/protobuf/proto"
)
import "google.golang.org/protobuf/runtime/protoimpl"

// Entity describes any member of the proto AST that is extensible via
// options. All components of a File are considered entities.
Expand Down Expand Up @@ -36,7 +34,7 @@ type Entity interface {
// will only be returned if there is a type mismatch between desc and ext.
// The ok value will be true if the extension was found. If the extension
// is NOT found, ok will be false and err will be nil.
Extension(desc *proto.ExtensionDesc, ext interface{}) (ok bool, err error)
Extension(desc *protoimpl.ExtensionInfo, ext interface{}) (ok bool, err error)

// BuildTarget identifies whether or not generation should be performed on
// this entity. Use this flag to determine if the file was targeted in the
Expand Down
6 changes: 3 additions & 3 deletions enum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pgs

import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/protobuf/runtime/protoimpl"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

// Enum describes an enumeration type. Its parent can be either a Message or a
Expand Down Expand Up @@ -68,7 +68,7 @@ func (e *enum) Dependents() []Message {
return messageSetToSlice("", e.dependentsCache)
}

func (e *enum) Extension(desc *proto.ExtensionDesc, ext interface{}) (bool, error) {
func (e *enum) Extension(desc *protoimpl.ExtensionInfo, ext interface{}) (bool, error) {
return extension(e.desc.GetOptions(), desc, &ext)
}

Expand Down
4 changes: 2 additions & 2 deletions enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

func TestEnum_Name(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions enum_value.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pgs

import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/protobuf/runtime/protoimpl"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

// An EnumValue describes a name-value pair for an entry in an enum.
Expand Down Expand Up @@ -41,7 +41,7 @@ func (ev *enumVal) Enum() Enum { return ev
func (ev *enumVal) Value() int32 { return ev.desc.GetNumber() }
func (ev *enumVal) Imports() []File { return nil }

func (ev *enumVal) Extension(desc *proto.ExtensionDesc, ext interface{}) (bool, error) {
func (ev *enumVal) Extension(desc *protoimpl.ExtensionInfo, ext interface{}) (bool, error) {
return extension(ev.desc.GetOptions(), desc, &ext)
}

Expand Down
4 changes: 2 additions & 2 deletions enum_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

func TestEnumVal_Name(t *testing.T) {
Expand Down
21 changes: 11 additions & 10 deletions extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"reflect"

"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/runtime/protoimpl"
)

// An Extension is a custom option annotation that can be applied to an Entity to provide additional
Expand Down Expand Up @@ -58,27 +59,27 @@ var extractor extExtractor
func init() { extractor = protoExtExtractor{} }

type extExtractor interface {
HasExtension(proto.Message, *proto.ExtensionDesc) bool
GetExtension(proto.Message, *proto.ExtensionDesc) (interface{}, error)
HasExtension(proto.Message, *protoimpl.ExtensionInfo) bool
GetExtension(proto.Message, *protoimpl.ExtensionInfo) interface{}
}

type protoExtExtractor struct{}

func (e protoExtExtractor) HasExtension(pb proto.Message, ext *proto.ExtensionDesc) bool {
func (e protoExtExtractor) HasExtension(pb proto.Message, ext *protoimpl.ExtensionInfo) bool {
return proto.HasExtension(pb, ext)
}

func (e protoExtExtractor) GetExtension(pb proto.Message, ext *proto.ExtensionDesc) (interface{}, error) {
func (e protoExtExtractor) GetExtension(pb proto.Message, ext *protoimpl.ExtensionInfo) interface{} {
return proto.GetExtension(pb, ext)
}

func extension(opts proto.Message, e *proto.ExtensionDesc, out interface{}) (bool, error) {
func extension(opts proto.Message, e *protoimpl.ExtensionInfo, out interface{}) (bool, error) {
if opts == nil || reflect.ValueOf(opts).IsNil() {
return false, nil
}

if e == nil {
return false, errors.New("nil *proto.ExtensionDesc parameter provided")
return false, errors.New("nil *protoimpl.ExtensionInfo parameter provided")
}

if out == nil {
Expand All @@ -94,9 +95,9 @@ func extension(opts proto.Message, e *proto.ExtensionDesc, out interface{}) (boo
return false, nil
}

val, err := extractor.GetExtension(opts, e)
if err != nil || val == nil {
return false, err
val := extractor.GetExtension(opts, e)
if val == nil {
return false, errors.New("extracted extension value is nil")
}

v := reflect.ValueOf(val)
Expand Down
36 changes: 11 additions & 25 deletions extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"errors"
"testing"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/runtime/protoimpl"
)

func TestExt_FullyQualifiedName(t *testing.T) {
Expand Down Expand Up @@ -101,13 +102,12 @@ func TestExt_Accept(t *testing.T) {
type mockExtractor struct {
has bool
get interface{}
err error
}

func (e *mockExtractor) HasExtension(proto.Message, *proto.ExtensionDesc) bool { return e.has }
func (e *mockExtractor) HasExtension(proto.Message, *protoimpl.ExtensionInfo) bool { return e.has }

func (e *mockExtractor) GetExtension(proto.Message, *proto.ExtensionDesc) (interface{}, error) {
return e.get, e.err
func (e *mockExtractor) GetExtension(proto.Message, *protoimpl.ExtensionInfo) interface{} {
return e.get
}

var testExtractor = &mockExtractor{}
Expand All @@ -116,7 +116,6 @@ func init() { extractor = testExtractor }

func TestExtension(t *testing.T) {
// cannot be parallel

defer func() { testExtractor.get = nil }()

found, err := extension(nil, nil, nil)
Expand All @@ -128,43 +127,30 @@ func TestExtension(t *testing.T) {
assert.NoError(t, err)

opts := &struct{ proto.Message }{}

found, err = extension(opts, nil, nil)
assert.False(t, found)
assert.Error(t, err)

desc := &proto.ExtensionDesc{}
assert.EqualError(t, err, "nil *protoimpl.ExtensionInfo parameter provided")

desc := &protoimpl.ExtensionInfo{}
found, err = extension(opts, desc, nil)
assert.False(t, found)
assert.Error(t, err)
assert.EqualError(t, err, "nil extension output parameter provided")

type myExt struct{ Name string }

found, err = extension(opts, desc, &myExt{})
assert.False(t, found)
assert.NoError(t, err)

testExtractor.has = true

found, err = extension(opts, desc, &myExt{})
assert.False(t, found)
assert.NoError(t, err)
assert.EqualError(t, err, "extracted extension value is nil")

testExtractor.err = errors.New("foo")

found, err = extension(opts, desc, &myExt{})
assert.False(t, found)
assert.Error(t, err)

testExtractor.err = nil
testExtractor.get = &myExt{"bar"}

out := myExt{}

found, err = extension(opts, desc, out)
assert.False(t, found)
assert.Error(t, err)
assert.EqualError(t, err, "out parameter must be a pointer type")

found, err = extension(opts, desc, &out)
assert.True(t, found)
Expand All @@ -185,7 +171,7 @@ func TestExtension(t *testing.T) {
func TestProtoExtExtractor(t *testing.T) {
e := protoExtExtractor{}
assert.NotPanics(t, func() { e.HasExtension(nil, nil) })
assert.NotPanics(t, func() { e.GetExtension(nil, nil) })
assert.Panics(t, func() { e.GetExtension(nil, nil) })
}

// needed to wrapped since there is a Extension method
Expand Down
6 changes: 3 additions & 3 deletions field.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pgs

import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/protobuf/runtime/protoimpl"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

// A Field describes a member of a Message. A field may also be a member of a
Expand Down Expand Up @@ -113,7 +113,7 @@ func (f *field) addType(t FieldType) {
f.typ = t
}

func (f *field) Extension(desc *proto.ExtensionDesc, ext interface{}) (ok bool, err error) {
func (f *field) Extension(desc *protoimpl.ExtensionInfo, ext interface{}) (ok bool, err error) {
return extension(f.desc.GetOptions(), desc, &ext)
}

Expand Down
4 changes: 2 additions & 2 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

func TestField_Name(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions field_type_elem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pgs
import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

func TestScalarE_ParentType(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pgs
import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)

func TestScalarT_Field(t *testing.T) {
Expand Down