Skip to content

Commit

Permalink
Zanzibar now supports custom interface names in clients, added change…
Browse files Browse the repository at this point in the history
…s in template, dependencies to check for custom client interface names
  • Loading branch information
tejaswiagarwal committed Sep 19, 2019
1 parent 80765c3 commit 6802493
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 93 deletions.
14 changes: 8 additions & 6 deletions codegen/mockgen.go
Expand Up @@ -22,14 +22,15 @@ package codegen

import (
"bytes"
"github.com/golang/mock/mockgen/model"
"github.com/pkg/errors"
"go/token"
"os/exec"
"path"
"sort"
"strconv"
"strings"

"github.com/golang/mock/mockgen/model"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -77,7 +78,7 @@ func (b byMethodName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byMethodName) Less(i, j int) bool { return b[i].Name < b[j].Name }

// AugmentMockWithFixture generates mocks with fixture for the interface in the given package
func (m MockgenBin) AugmentMockWithFixture(pkg *model.Package, f *Fixture) ([]byte, []byte, error) {
func (m MockgenBin) AugmentMockWithFixture(pkg *model.Package, f *Fixture, intf string) ([]byte, []byte, error) {
methodsMap := make(map[string]*model.Method, len(pkg.Interfaces[0].Methods))
validationMap := make(map[string]interface{}, len(pkg.Interfaces[0].Methods))
for _, m := range pkg.Interfaces[0].Methods {
Expand Down Expand Up @@ -128,9 +129,10 @@ func (m MockgenBin) AugmentMockWithFixture(pkg *model.Package, f *Fixture) ([]by
}

data := map[string]interface{}{
"Imports": pkgPathToAlias,
"Methods": methods,
"Fixture": f,
"Imports": pkgPathToAlias,
"Methods": methods,
"Fixture": f,
"ClientInterface": intf,
}
types, err := m.tmpl.ExecTemplate("fixture_types.tmpl", data, m.pkgHelper)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions codegen/module_system.go
Expand Up @@ -1497,6 +1497,10 @@ func GenerateDependencyStruct(
packageHelper *PackageHelper,
template *Template,
) ([]byte, error) {
genCustom, _ := instance.Config["customInterface"].(string)
if genCustom != "" {
instance.PackageInfo.ExportType = instance.Config["customInterface"].(string)
}
return template.ExecTemplate(
"dependency_struct.tmpl",
instance,
Expand Down
28 changes: 12 additions & 16 deletions codegen/post_gen_hooks.go
Expand Up @@ -36,8 +36,8 @@ import (
)

const (
defaulClientInterface = "Client"
custom = "custom"
defaultClientInterface = "Client"
custom = "custom"
)

type mockableClient struct {
Expand Down Expand Up @@ -79,7 +79,7 @@ func ClientMockGenHook(h *PackageHelper, t *Template) (PostGenHook, error) {

importPathMap := make(map[string]string, mockCount)
fixtureMap := make(map[string]*Fixture, mockCount)
customInterfaceMap := make(map[string]string)
clientInterfaceMap := make(map[string]string)
pathSymbolMap := make(map[string]string)
for _, instance := range clientInstances {
key := instance.ClassType + instance.InstanceName
Expand All @@ -98,13 +98,18 @@ func ClientMockGenHook(h *PackageHelper, t *Template) (PostGenHook, error) {
importPath = client.Config.CustomImportPath
}

clientInterface := defaultClientInterface
// if an interfaces name is provided use that, else use "Client"
if customInterface != "" {
clientInterface = customInterface
}
importPathMap[key] = importPath
customInterfaceMap[key] = customInterface
clientInterfaceMap[key] = clientInterface

// gather all modules that need to generate fixture types
f := client.Config.Fixture
if f != nil && f.Scenarios != nil {
pathSymbolMap[importPath] = defaulClientInterface
pathSymbolMap[importPath] = clientInterface
fixtureMap[key] = f
}
}
Expand Down Expand Up @@ -144,18 +149,9 @@ func ClientMockGenHook(h *PackageHelper, t *Template) (PostGenHook, error) {
genDir := filepath.Join(buildDir, instance.Directory, "mock-client")

importPath := importPathMap[key]
customInterface := customInterfaceMap[key]

var mock []byte
var err error
var clientInterface = "Client"
// generate mock client, this starts a sub process.
// if an interfaces name is provided use that, else use "Client"
if customInterface != "" {
clientInterface = customInterface
}

mock, err = bin.GenMock(importPath, "clientmock", clientInterface)
mock, err := bin.GenMock(importPath, "clientmock", clientInterfaceMap[key])
if err != nil {
ec <- errors.Wrapf(
err,
Expand All @@ -168,7 +164,7 @@ func ClientMockGenHook(h *PackageHelper, t *Template) (PostGenHook, error) {

// generate fixture types and augmented mock client
if f, ok := fixtureMap[key]; ok {
types, augMock, err := bin.AugmentMockWithFixture(pkgs[importPath], f)
types, augMock, err := bin.AugmentMockWithFixture(pkgs[importPath], f, clientInterfaceMap[key])
if err != nil {
ec <- errors.Wrapf(
err,
Expand Down
24 changes: 13 additions & 11 deletions codegen/template_bundle/template_files.go

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

22 changes: 12 additions & 10 deletions codegen/templates/augmented_mock.tmpl
Expand Up @@ -2,15 +2,17 @@
{{- $methods := .Methods}}
{{- $fixturePkg := .Fixture.ImportPath -}}
{{- $scenarios := .Fixture.Scenarios -}}
{{- $clientinterface := .ClientInterface}}

package clientmock

import (
"github.com/golang/mock/gomock"
)

// MockClientWithFixture is a mock of Client interface with preset fixture
type MockClientWithFixture struct {
*MockClient
// Mock{{$clientinterface}}WithFixture is a mock of Client interface with preset fixture
type Mock{{$clientinterface}}WithFixture struct {
*Mock{{$clientinterface}}
fixture *ClientFixture

{{range $method := $methods}}
Expand All @@ -37,16 +39,16 @@ func (c Call) MinTimes(max int) {
}

// New creates a new mock instance
func New(ctrl *gomock.Controller, fixture *ClientFixture) *MockClientWithFixture {
return &MockClientWithFixture{
MockClient: NewMockClient(ctrl),
func New(ctrl *gomock.Controller, fixture *ClientFixture) *Mock{{$clientinterface}}WithFixture {
return &Mock{{$clientinterface}}WithFixture{
Mock{{$clientinterface}}: NewMock{{$clientinterface}}(ctrl),
fixture: fixture,
}
}

// EXPECT shadows the EXPECT method on the underlying mock client.
// It should not be called directly.
func (m *MockClientWithFixture) EXPECT() {
func (m *Mock{{$clientinterface}}WithFixture) EXPECT() {
panic("should not call EXPECT directly.")
}

Expand All @@ -59,15 +61,15 @@ func (m *MockClientWithFixture) EXPECT() {
// {{$methodMockType}} mocks the {{$methodName}} method
type {{$methodMockType}} struct {
scenarios *{{$methodName}}Scenarios
mockClient *MockClient
mockClient *Mock{{$clientinterface}}
}
{{$methodMockMethod := printf "Expect%s" $methodName -}}
// {{$methodMockMethod}} returns an object that allows the caller to choose expected scenario for {{$methodName}}
func (m *MockClientWithFixture) {{$methodMockMethod}}() *{{$methodMockType}} {
func (m *Mock{{$clientinterface}}WithFixture) {{$methodMockMethod}}() *{{$methodMockType}} {
if m.{{$methodMockField}} == nil {
m.{{$methodMockField}} = &{{$methodMockType}}{
scenarios: m.fixture.{{$methodName}},
mockClient: m.MockClient,
mockClient: m.Mock{{$clientinterface}},
}
}
return m.{{$methodMockField}}
Expand Down
1 change: 1 addition & 0 deletions coverage.tmp
@@ -0,0 +1 @@
mode: set
3 changes: 0 additions & 3 deletions examples/example-gateway/build.yaml
Expand Up @@ -4,9 +4,6 @@ copyrightHeader: ./copyright_header.txt
endpointConfig: ./endpoints
genCodePackage: github.com/uber/zanzibar/examples/example-gateway/build/gen-code
genMock: true
clientInterfacesMock:
github.com/uber/zanzibar/examples/example-gateway/build/clients/contacts:
- Client
middlewareConfig: ./middlewares
defaultMiddlewareConfig: ./middlewares/default
packageRoot: github.com/uber/zanzibar/examples/example-gateway
Expand Down

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

0 comments on commit 6802493

Please sign in to comment.