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

Allow multi-service client in clients init #111

Merged
merged 8 commits into from
May 30, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ EXAMPLE_SERVICES = $(sort $(dir $(wildcard $(EXAMPLE_SERVICES_DIR)*/)))

.PHONY: check-licence
check-licence:
ls ./node_modules/.bin/uber-licence >/dev/null 2>&1 || npm i uber-licence
./node_modules/.bin/uber-licence --dry --file '*.go' --dir '!vendor' --dir '!examples' --dir '!.tmp_gen' --dir '!template_bundle'
@echo "Checking uber-licence..."
@ls ./node_modules/.bin/uber-licence >/dev/null 2>&1 || npm i uber-licence
@./node_modules/.bin/uber-licence --dry --file '*.go' --dir '!vendor' --dir '!examples' --dir '!.tmp_gen' --dir '!template_bundle'

.PHONY: fix-licence
fix-licence:
ls ./node_modules/.bin/uber-licence >/dev/null 2>&1 || npm i uber-licence
@ls ./node_modules/.bin/uber-licence >/dev/null 2>&1 || npm i uber-licence
./node_modules/.bin/uber-licence --file '*.go' --dir '!vendor' --dir '!examples' --dir '!.tmp_gen' --dir '!template_bundle'

.PHONY: install
Expand All @@ -39,12 +40,13 @@ install:

.PHONY: eclint-check
eclint-check:
ls ./node_modules/.bin/eclint >/dev/null 2>&1 || npm i eclint@v1.1.5
./node_modules/.bin/eclint check "./{codegen,examples}/**/*.{json,tmpl}"
@echo "Checking eclint..."
@ls ./node_modules/.bin/eclint >/dev/null 2>&1 || npm i eclint@v1.1.5
@./node_modules/.bin/eclint check "./{codegen,examples}/**/*.{json,tmpl}"

.PHONY: eclint-fix
eclint-fix:
ls ./node_modules/.bin/eclint >/dev/null 2>&1 || npm i eclint@v1.1.5
@ls ./node_modules/.bin/eclint >/dev/null 2>&1 || npm i eclint@v1.1.5
./node_modules/.bin/eclint fix "./{codegen,examples}/**/*.{json,tmpl}"

.PHONY: lint
Expand Down
8 changes: 3 additions & 5 deletions codegen/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ func (ms *MethodSpec) setRequestType(curThriftFile string, funcSpec *compile.Fun
var err error
if isRequestBoxed(funcSpec) {
ms.RequestBoxed = true
ms.RequestType, err = packageHelper.TypeFullName(
curThriftFile, funcSpec.ArgsSpec[0].Type,
)
ms.RequestType, err = packageHelper.TypeFullName(funcSpec.ArgsSpec[0].Type)
if err == nil && isStructType(funcSpec.ArgsSpec[0].Type) {
ms.RequestType = "*" + ms.RequestType
}
Expand Down Expand Up @@ -227,7 +225,7 @@ func (ms *MethodSpec) setResponseType(curThriftFile string, respSpec *compile.Re
ms.ResponseType = ""
return nil
}
typeName, err := packageHelper.TypeFullName(curThriftFile, respSpec.ReturnType)
typeName, err := packageHelper.TypeFullName(respSpec.ReturnType)
if isStructType(respSpec.ReturnType) {
typeName = "*" + typeName
}
Expand Down Expand Up @@ -279,7 +277,7 @@ func (ms *MethodSpec) setExceptions(
)

for i, e := range resultSpec.Exceptions {
typeName, err := h.TypeFullName(curThriftFile, e.Type)
typeName, err := h.TypeFullName(e.Type)
if err != nil {
return errors.Wrapf(
err,
Expand Down
192 changes: 85 additions & 107 deletions codegen/module_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,6 @@ func (g *ClientsInitGenerator) Generate(
continue
}

if len(module.Services) != 1 {
return nil, errors.Errorf(
"Cannot import client with multiple services: %s",
module.PackageName,
)
}

clientInfo = append(clientInfo, ClientInfoMeta{
IsPointerType: true,
FieldName: strings.Title(clients[i].ClientName),
Expand Down Expand Up @@ -739,87 +732,6 @@ func (g *EndpointGenerator) generateEndpointFile(
return nil
}

/*
* Gateway Service Generator
*/

// GatewayServiceGenerator generates an entry point for a single service as
// a main.go that bootstraps the service and its dependencies
type GatewayServiceGenerator struct {
templates *Template
packageHelper *PackageHelper
}

// Generate returns the gateway service generated files as a map of relative
// file path (relative to the target buid directory) to file bytes.
func (generator *GatewayServiceGenerator) Generate(
instance *ModuleInstance,
) (map[string][]byte, error) {
// zanzibar-defaults.json is copied from ../config/production.json
configSrcFileName := path.Join(
getDirName(), "..", "config", "production.json",
)
productionConfig, err := ioutil.ReadFile(configSrcFileName)
if err != nil {
return nil, errors.Wrap(
err,
"Could not read config/production.json while generating main file",
)
}

// main.go and main_test.go shared meta
meta := &MainMeta{
IncludedPackages: []GoPackageImport{
{
PackageName: generator.packageHelper.GoGatewayPackageName() +
"/clients",
AliasName: "",
},
{
PackageName: generator.packageHelper.GoGatewayPackageName() +
"/endpoints",
AliasName: "",
},
},
GatewayName: instance.InstanceName,
RelativePathToAppConfig: filepath.Join("..", "..", ".."),
}

// generate main.go
main, err := generator.templates.execTemplate(
"main.tmpl",
meta,
generator.packageHelper,
)
if err != nil {
return nil, errors.Wrapf(
err,
"Error generating service main.go for %s",
instance.InstanceName,
)
}

// generate main_test.go
mainTest, err := generator.templates.execTemplate(
"main_test.tmpl",
meta,
generator.packageHelper,
)
if err != nil {
return nil, errors.Wrapf(
err,
"Error generating service main_test.go for %s",
instance.InstanceName,
)
}

return map[string][]byte{
"zanzibar-defaults.json": productionConfig,
"main.go": main,
"main_test.go": mainTest,
}, nil
}

func (g *EndpointGenerator) generateEndpointTestFile(
e *EndpointSpec, instance *ModuleInstance, out map[string][]byte,
) error {
Expand Down Expand Up @@ -941,25 +853,10 @@ func (g *EndpointGenerator) generateEndpointTestFile(
tempName := "endpoint_test.tmpl"
if e.WorkflowType == "tchannelClient" {
meta.ClientName = e.ClientName

genCodeClientPkgName, err := g.packageHelper.TypeImportPath(method.Downstream.ThriftFile)
if err != nil {
return errors.Wrap(err, "could not run endpoint_test template")
}
genCodeClientAliasName, err := g.packageHelper.TypePackageName(method.Downstream.ThriftFile)
if err != nil {
return errors.Wrap(err, "could not run endpoint_test template")
}
meta.IncludedPackages = []GoPackageImport{
{
AliasName: method.Downstream.PackageName,
PackageName: method.Downstream.GoPackage,
},
{
AliasName: genCodeClientAliasName,
PackageName: genCodeClientPkgName,
},
}
meta.IncludedPackages = append(method.Downstream.IncludedPackages, GoPackageImport{
AliasName: method.Downstream.PackageName,
PackageName: method.Downstream.GoPackage,
})
tempName = "endpoint_test_tchannel_client.tmpl"
}

Expand All @@ -981,3 +878,84 @@ func (g *EndpointGenerator) generateEndpointTestFile(

return nil
}

/*
* Gateway Service Generator
*/

// GatewayServiceGenerator generates an entry point for a single service as
// a main.go that bootstraps the service and its dependencies
type GatewayServiceGenerator struct {
templates *Template
packageHelper *PackageHelper
}

// Generate returns the gateway service generated files as a map of relative
// file path (relative to the target buid directory) to file bytes.
func (generator *GatewayServiceGenerator) Generate(
instance *ModuleInstance,
) (map[string][]byte, error) {
// zanzibar-defaults.json is copied from ../config/production.json
configSrcFileName := path.Join(
getDirName(), "..", "config", "production.json",
)
productionConfig, err := ioutil.ReadFile(configSrcFileName)
if err != nil {
return nil, errors.Wrap(
err,
"Could not read config/production.json while generating main file",
)
}

// main.go and main_test.go shared meta
meta := &MainMeta{
IncludedPackages: []GoPackageImport{
{
PackageName: generator.packageHelper.GoGatewayPackageName() +
"/clients",
AliasName: "",
},
{
PackageName: generator.packageHelper.GoGatewayPackageName() +
"/endpoints",
AliasName: "",
},
},
GatewayName: instance.InstanceName,
RelativePathToAppConfig: filepath.Join("..", "..", ".."),
}

// generate main.go
main, err := generator.templates.execTemplate(
"main.tmpl",
meta,
generator.packageHelper,
)
if err != nil {
return nil, errors.Wrapf(
err,
"Error generating service main.go for %s",
instance.InstanceName,
)
}

// generate main_test.go
mainTest, err := generator.templates.execTemplate(
"main_test.tmpl",
meta,
generator.packageHelper,
)
if err != nil {
return nil, errors.Wrapf(
err,
"Error generating service main_test.go for %s",
instance.InstanceName,
)
}

return map[string][]byte{
"zanzibar-defaults.json": productionConfig,
"main.go": main,
"main_test.go": mainTest,
}, nil
}
4 changes: 2 additions & 2 deletions codegen/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func (p PackageHelper) EndpointTestConfigPath(
return path.Join(p.testConfigsRootDir, strings.ToLower(serviceName), fileName)
}

// TypeFullName returns the referred Go type name in generated code from curThriftFile.
func (p PackageHelper) TypeFullName(curThriftFile string, typeSpec compile.TypeSpec) (string, error) {
// TypeFullName returns the referred Go type name in generated code.
func (p PackageHelper) TypeFullName(typeSpec compile.TypeSpec) (string, error) {
if typeSpec == nil {
return "", nil
}
Expand Down
14 changes: 10 additions & 4 deletions codegen/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,16 @@ func (ms *ModuleSpec) SetDownstream(
if method.Downstream != nil {
var downstreamMethod *MethodSpec

for _, dsMethod := range method.Downstream.Services[0].Methods {
if method.Name == dsMethod.Name {
downstreamMethod = dsMethod
break
// TODO: once all client configs have "exposedMethods" field, we can find the exact
// service, instead of loop over for service looking for the first matching method,
// which could totally be wrong method
loop:
for _, s := range method.Downstream.Services {
for _, dsMethod := range s.Methods {
if method.Name == dsMethod.Name {
downstreamMethod = dsMethod
break loop
}
}
}
if downstreamMethod == nil {
Expand Down
Loading