Skip to content

Commit

Permalink
Merge pull request #3 from twitchtv/handle-parsing-struct-type
Browse files Browse the repository at this point in the history
Handle parsing imports for struct literals (struct{})
  • Loading branch information
kollektiv authored Aug 30, 2019
2 parents 67db94d + 26b845c commit 6db49d8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
22 changes: 11 additions & 11 deletions internal/circuitgentest/circuittest/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"testing"
"time"

"github.com/twitchtv/circuitgen/internal/circuitgentest"
"github.com/twitchtv/circuitgen/internal/circuitgentest/model"
"github.com/twitchtv/circuitgen/internal/circuitgentest/rep"
"github.com/cep21/circuit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/twitchtv/circuitgen/internal/circuitgentest"
"github.com/twitchtv/circuitgen/internal/circuitgentest/model"
"github.com/twitchtv/circuitgen/internal/circuitgentest/rep"
)

// Test generated clients
Expand All @@ -50,7 +50,7 @@ func TestPublisherInterface(t *testing.T) {

opt := rep.PublishOption{Sample: 0.1}
opt2 := rep.PublishOption{Sample: 0.2}
m.On("Publish", mock.Anything, grants, topics, opt, opt2).Return(nil).Once()
m.On("Publish", mock.Anything, grants, topics, opt, opt2).Return(nil, nil).Once()

m.On("Close", mock.Anything).Return(nil).Once()

Expand Down Expand Up @@ -87,7 +87,7 @@ func TestPublisherInterface(t *testing.T) {
require.Equal(t, 1*time.Second, manager.GetCircuit("Publisher.PublishWithResult").Config().Execution.Timeout)
require.Equal(t, 2*time.Second, manager.GetCircuit("Publisher.Publish").Config().Execution.Timeout)

err = publisher.Publish(ctx, grants, topics, opt, opt2)
_, err = publisher.Publish(ctx, grants, topics, opt, opt2)
require.NoError(t, err)

res, err := publisher.PublishWithResult(ctx, publishInput)
Expand All @@ -109,7 +109,7 @@ func TestPublisherInterfaceErrors(t *testing.T) {

testError := errors.New("test error")
m := &circuitgentest.MockPublisher{}
m.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(testError).Once()
m.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil, testError).Once()

publishCounter := &runMetricsCounter{}
publisher, err := NewCircuitWrapperPublisher(manager, m, CircuitWrapperPublisherConfig{
Expand All @@ -123,7 +123,7 @@ func TestPublisherInterfaceErrors(t *testing.T) {
require.NotNil(t, publisher)

ctx := context.Background()
err = publisher.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
_, err = publisher.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
require.Equal(t, testError, err)

// Check embedded called
Expand All @@ -138,7 +138,7 @@ func TestPublisherInterfaceBadRequest(t *testing.T) {

testError := errors.New("bad request error")
m := &circuitgentest.MockPublisher{}
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(testError).Once()
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, testError).Once()

publishCounter := &runMetricsCounter{}
publisher, err := NewCircuitWrapperPublisher(manager, m, CircuitWrapperPublisherConfig{
Expand All @@ -155,7 +155,7 @@ func TestPublisherInterfaceBadRequest(t *testing.T) {
require.NotNil(t, publisher)

ctx := context.Background()
err = publisher.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
_, err = publisher.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
require.Equal(t, testError, err)

// Check embedded called
Expand All @@ -171,7 +171,7 @@ func TestPubsubInterface(t *testing.T) {

m := &circuitgentest.MockPublisher{}
m.On("PublishWithResult", mock.Anything, mock.Anything).Return(nil, nil).Once()
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil).Once()
m.On("Close", mock.Anything).Return(nil).Once()

pubsub, err := NewCircuitWrapperPubsub(manager, m, CircuitWrapperPubsubConfig{})
Expand All @@ -184,7 +184,7 @@ func TestPubsubInterface(t *testing.T) {
require.Contains(t, names, "Pubsub.Publish")

ctx := context.Background()
err = pubsub.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
_, err = pubsub.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
require.NoError(t, err)

_, err = pubsub.PublishWithResult(ctx, rep.PublishInput{})
Expand Down
8 changes: 5 additions & 3 deletions internal/circuitgentest/circuittest/publisher.gen.go

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

8 changes: 5 additions & 3 deletions internal/circuitgentest/circuittest/pubsub.gen.go

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

8 changes: 5 additions & 3 deletions internal/circuitgentest/publisher.gen.go

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

18 changes: 14 additions & 4 deletions internal/circuitgentest/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package circuitgentest
import (
"context"

"github.com/stretchr/testify/mock"
"github.com/twitchtv/circuitgen/internal/circuitgentest/model"
"github.com/twitchtv/circuitgen/internal/circuitgentest/rep"
"github.com/stretchr/testify/mock"
)

// TopicsList is a test struct
Expand All @@ -40,7 +40,7 @@ type Publisher interface {
// PublishWithResult is a test method and should be wrapped
PublishWithResult(context.Context, rep.PublishInput) (*model.Result, error)
// PublishWithResult is a test method and should be wrapped
Publish(context.Context, map[Seed][][]Grant, TopicsList, ...rep.PublishOption) error
Publish(context.Context, map[Seed][][]Grant, TopicsList, ...rep.PublishOption) (map[string]struct{}, error)
// PublishWithResult is a test method and should not be wrapped
Close() error
}
Expand All @@ -65,7 +65,7 @@ func (m *MockPublisher) PublishWithResult(ctx context.Context, input rep.Publish
}

// Publish mocks the method
func (m *MockPublisher) Publish(ctx context.Context, g map[Seed][][]Grant, s TopicsList, opts ...rep.PublishOption) error {
func (m *MockPublisher) Publish(ctx context.Context, g map[Seed][][]Grant, s TopicsList, opts ...rep.PublishOption) (map[string]struct{}, error) {
var ca []interface{}
ca = append(ca, ctx, g, s)
va := make([]interface{}, len(opts))
Expand All @@ -74,7 +74,17 @@ func (m *MockPublisher) Publish(ctx context.Context, g map[Seed][][]Grant, s Top
}
ca = append(ca, va...)
args := m.Called(ca...)
return args.Error(0)

var r0 map[string]struct{}
if args.Get(0) != nil {
var ok bool
r0, ok = args.Get(0).(map[string]struct{})
if !ok {
panic("args.Get(0) is not a map[string]struct{}")
}
}

return r0, args.Error(1)
}

// Close mocks the method
Expand Down
1 change: 1 addition & 0 deletions parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func resolvePkgPaths(p types.Type) ([]string, error) {
}
case *types.Basic:
case *types.Interface:
case *types.Struct: // struct{}
// Break out of the switch and return below
default:
return nil, fmt.Errorf("resolvePkgPaths: invalid type: %v", t)
Expand Down

0 comments on commit 6db49d8

Please sign in to comment.