Skip to content

Commit

Permalink
Merge pull request #102 from uber/me.packageinfo
Browse files Browse the repository at this point in the history
Add package info to module configuration
  • Loading branch information
Matt-Esch committed May 22, 2017
2 parents 51262c8 + 938a824 commit ab97b38
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 128 deletions.
327 changes: 262 additions & 65 deletions module/module.go → codegen/module.go

Large diffs are not rendered by default.

37 changes: 18 additions & 19 deletions codegen/module_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/uber/zanzibar/module"
)

// EndpointMeta saves meta data used to render an endpoint.
Expand Down Expand Up @@ -93,18 +92,18 @@ type ClientStub struct {
// module system (clients, endpoints)
func NewDefaultModuleSystem(
h *PackageHelper,
) (*module.System, error) {
system := module.NewSystem()
) (*ModuleSystem, error) {
system := NewModuleSystem()
tmpl, err := NewTemplate()

if err != nil {
return nil, err
}

// Register client module class and type generators
if err := system.RegisterClass("client", module.Class{
if err := system.RegisterClass("client", ModuleClass{
Directory: "clients",
ClassType: module.MultiModule,
ClassType: MultiModule,
ClassDependencies: []string{},
}); err != nil {
return nil, errors.Wrapf(err, "Error registering client class")
Expand Down Expand Up @@ -144,9 +143,9 @@ func NewDefaultModuleSystem(
)
}

if err := system.RegisterClass("clients", module.Class{
if err := system.RegisterClass("clients", ModuleClass{
Directory: "clients",
ClassType: module.SingleModule,
ClassType: SingleModule,
ClassDependencies: []string{},
}); err != nil {
return nil, errors.Wrapf(err, "Error registering clientInit class")
Expand All @@ -162,9 +161,9 @@ func NewDefaultModuleSystem(
)
}

if err := system.RegisterClass("service", module.Class{
if err := system.RegisterClass("service", ModuleClass{
Directory: "services",
ClassType: module.MultiModule,
ClassType: MultiModule,
ClassDependencies: []string{"client"},
}); err != nil {
return nil, errors.Wrapf(
Expand All @@ -184,9 +183,9 @@ func NewDefaultModuleSystem(
}

// Register endpoint module class and type generators
if err := system.RegisterClass("endpoint", module.Class{
if err := system.RegisterClass("endpoint", ModuleClass{
Directory: "endpoints",
ClassType: module.MultiModule,
ClassType: MultiModule,
ClassDependencies: []string{"client"},
}); err != nil {
return nil, errors.Wrapf(err, "Error registering endpoint class")
Expand Down Expand Up @@ -254,7 +253,7 @@ type HTTPClientGenerator struct {
// Generate returns the HTTP client generated files as a map of relative file
// path (relative to the target build directory) to file bytes.
func (g *HTTPClientGenerator) Generate(
instance *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
// Parse the client config from the endpoint JSON file
clientConfig, err := readClientConfig(instance.JSONFileRaw)
Expand Down Expand Up @@ -335,7 +334,7 @@ type TChannelClientGenerator struct {
// Generate returns the TChannel client generated files as a map of relative file
// path (relative to the target build directory) to file bytes.
func (g *TChannelClientGenerator) Generate(
instance *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
// Parse the client config from the endpoint JSON file
clientConfig, err := readClientConfig(instance.JSONFileRaw)
Expand Down Expand Up @@ -430,7 +429,7 @@ type CustomClientGenerator struct {

// Generate does NOT generate any file, it collects the client spec
func (g *CustomClientGenerator) Generate(
instance *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
// Parse the client config from the endpoint JSON file
clientConfig, err := readClientConfig(instance.JSONFileRaw)
Expand Down Expand Up @@ -477,7 +476,7 @@ type ClientsInitGenerator struct {
// Generate returns the client init file as a map of relative file
// path (relative to the target build directory) to file bytes.
func (g *ClientsInitGenerator) Generate(
instance *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
clients := []*ClientSpec{}
for _, v := range g.clientSpecs {
Expand Down Expand Up @@ -576,7 +575,7 @@ type EndpointGenerator struct {
// Generate returns the endpoint generated files as a map of relative file
// path (relative to the target build directory) to file bytes.
func (g *EndpointGenerator) Generate(
instance *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
ret := map[string][]byte{}
endpointJsons := []string{}
Expand Down Expand Up @@ -636,7 +635,7 @@ func (g *EndpointGenerator) Generate(
}

func (g *EndpointGenerator) generateEndpointFile(
e *EndpointSpec, instance *module.Instance, out map[string][]byte,
e *EndpointSpec, instance *ModuleInstance, out map[string][]byte,
) error {
m := e.ModuleSpec
methodName := e.ThriftMethodName
Expand Down Expand Up @@ -744,7 +743,7 @@ type GatewayServiceGenerator struct {
// 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 *module.Instance,
instance *ModuleInstance,
) (map[string][]byte, error) {
// zanzibar-defaults.json is copied from ../config/production.json
configSrcFileName := path.Join(
Expand Down Expand Up @@ -812,7 +811,7 @@ func (generator *GatewayServiceGenerator) Generate(
}

func (g *EndpointGenerator) generateEndpointTestFile(
e *EndpointSpec, instance *module.Instance, out map[string][]byte,
e *EndpointSpec, instance *ModuleInstance, out map[string][]byte,
) error {
m := e.ModuleSpec
methodName := e.ThriftMethodName
Expand Down
45 changes: 23 additions & 22 deletions module/module_test.go → codegen/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package module
package codegen

import (
"os"
Expand All @@ -35,35 +35,35 @@ var staticHandler = handler{}
var variableHandler = handler{}
var splatHandler = handler{}

type HTTPClientGenerator struct{}
type TestHTTPClientGenerator struct{}

func (*HTTPClientGenerator) Generate(
instance *Instance,
func (*TestHTTPClientGenerator) Generate(
instance *ModuleInstance,
) (map[string][]byte, error) {
return nil, nil
}

type TChannelClientGenerator struct{}
type TestTChannelClientGenerator struct{}

func (*TChannelClientGenerator) Generate(
instance *Instance,
func (*TestTChannelClientGenerator) Generate(
instance *ModuleInstance,
) (map[string][]byte, error) {
return nil, nil
}

type HTTPEndpointGenerator struct{}
type TestHTTPEndpointGenerator struct{}

func (*HTTPEndpointGenerator) Generate(
instance *Instance,
func (*TestHTTPEndpointGenerator) Generate(
instance *ModuleInstance,
) (map[string][]byte, error) {
return nil, nil
}

func TestExampleService(t *testing.T) {
moduleSystem := NewSystem()
moduleSystem := NewModuleSystem()
var err error

err = moduleSystem.RegisterClass("client", Class{
err = moduleSystem.RegisterClass("client", ModuleClass{
ClassType: MultiModule,
Directory: "clients",
})
Expand All @@ -74,7 +74,7 @@ func TestExampleService(t *testing.T) {
err = moduleSystem.RegisterClassType(
"client",
"http",
&HTTPClientGenerator{},
&TestHTTPClientGenerator{},
)
if err != nil {
t.Errorf("Unexpected error registering http client class type: %s", err)
Expand All @@ -83,13 +83,13 @@ func TestExampleService(t *testing.T) {
err = moduleSystem.RegisterClassType(
"client",
"tchannel",
&TChannelClientGenerator{},
&TestTChannelClientGenerator{},
)
if err != nil {
t.Errorf("Unexpected error registering tchannel client class type: %s", err)
}

err = moduleSystem.RegisterClass("endpoint", Class{
err = moduleSystem.RegisterClass("endpoint", ModuleClass{
ClassType: MultiModule,
ClassDependencies: []string{"client"},
Directory: "endpoints",
Expand All @@ -101,7 +101,7 @@ func TestExampleService(t *testing.T) {
err = moduleSystem.RegisterClassType(
"endpoint",
"http",
&HTTPEndpointGenerator{},
&TestHTTPEndpointGenerator{},
)
if err != nil {
t.Errorf("Unexpected error registering http client class type: %s", err)
Expand All @@ -110,42 +110,43 @@ func TestExampleService(t *testing.T) {
err = moduleSystem.RegisterClassType(
"endpoint",
"http",
&HTTPEndpointGenerator{},
&TestHTTPEndpointGenerator{},
)
if err == nil {
t.Errorf("Expected double creation of http endpoint to error")
}

err = moduleSystem.RegisterClass("client", Class{
err = moduleSystem.RegisterClass("client", ModuleClass{
ClassType: MultiModule,
Directory: "clients",
})
if err == nil {
t.Errorf("Expected double definition of client class to error")
}

err = moduleSystem.RegisterClass("newclient", Class{
err = moduleSystem.RegisterClass("newclient", ModuleClass{
ClassType: MultiModule,
Directory: "./clients/",
})
if err == nil {
t.Errorf("Expected registering a module in the same directory to throw")
}

err = moduleSystem.RegisterClass("newclient", Class{
err = moduleSystem.RegisterClass("newclient", ModuleClass{
ClassType: MultiModule,
Directory: "./clients/../../../foo",
})
if err == nil {
t.Errorf("Expected registering a module in an external directory to throw")
}

currentDir := getDirName()
currentDir := getTestDirName()
testServiceDir := path.Join(currentDir, "test-service")

// TODO: this doesn't yet generate the build to a dir
// TODO: this should return a collection of errors if they occur
err = moduleSystem.GenerateBuild(
"github.com/uber/zanzibar/module/test-service",
testServiceDir,
path.Join(testServiceDir, "build"),
)
Expand All @@ -154,7 +155,7 @@ func TestExampleService(t *testing.T) {
}
}

func getDirName() string {
func getTestDirName() string {
_, file, _, _ := runtime.Caller(0)
dirname := filepath.Dir(file)
// Strip _obj dirs generated by test -cover ...
Expand Down
9 changes: 9 additions & 0 deletions codegen/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

// PackageHelper manages the mapping from thrift file to generated type code and service code.
type PackageHelper struct {
// The project package name
packageRoot string
// The root directory containing thrift files.
thriftRootDir string
// Namespace under thrift folder
Expand All @@ -49,6 +51,7 @@ type PackageHelper struct {

// NewPackageHelper creates a package helper.
func NewPackageHelper(
packageRoot string,
configDirName string,
middlewareConfig string,
thriftRootDir string,
Expand Down Expand Up @@ -87,6 +90,7 @@ func NewPackageHelper(
}

p := &PackageHelper{
packageRoot: packageRoot,
thriftRootDir: path.Clean(thriftRootDir),
genCodePackage: genCodePackage,
gatewayNamespace: gatewayNamespace,
Expand All @@ -97,6 +101,11 @@ func NewPackageHelper(
return p, nil
}

// PackageRoot returns the service's root package name
func (p PackageHelper) PackageRoot() string {
return p.packageRoot
}

// MiddlewareSpecs returns a map of middlewares available
func (p PackageHelper) MiddlewareSpecs() map[string]*MiddlewareSpec {
return p.middlewareSpecs
Expand Down
1 change: 1 addition & 0 deletions codegen/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func newPackageHelper(t *testing.T) *codegen.PackageHelper {
}

h, err := codegen.NewPackageHelper(
"github.com/uber/zanzibar/examples/example-gateway",
filepath.Join(absGatewayPath),
"middlewares/middleware-config.json",
filepath.Join(absGatewayPath, "idl"),
Expand Down
2 changes: 2 additions & 0 deletions codegen/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func main() {
}

packageHelper, err := codegen.NewPackageHelper(
config.MustGetString("packageRoot"),
configDirName,
config.MustGetString("middlewareConfig"),
filepath.Join(configDirName, config.MustGetString("thriftRootDir")),
Expand All @@ -101,6 +102,7 @@ func main() {
// TODO: All components should be generated by the module system
fmt.Printf("Generating module system components:\n")
err = moduleSystem.GenerateBuild(
packageHelper.PackageRoot(),
configDirName,
packageHelper.CodeGenTargetPath(),
)
Expand Down
2 changes: 2 additions & 0 deletions codegen/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestGenerateBar(t *testing.T) {
}

packageHelper, err := codegen.NewPackageHelper(
"github.com/uber/zanzibar/examples/example-gateway",
filepath.Join(absGatewayPath),
"middlewares/middleware-config.json",
filepath.Join(absGatewayPath, "idl"),
Expand Down Expand Up @@ -89,6 +90,7 @@ func TestGenerateBar(t *testing.T) {
}

err = moduleSystem.GenerateBuild(
"github.com/uber/zanzibar/examples/example-gateway",
absGatewayPath,
packageHelper.CodeGenTargetPath(),
)
Expand Down
10 changes: 10 additions & 0 deletions codegen/test-service/clients/example/client-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example",
"type": "http",
"config": {
"headers": {
"x-service-name": "example"
}
},
"dependencies": {}
}
12 changes: 12 additions & 0 deletions codegen/test-service/endpoints/health/endpoint-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "health",
"type": "http",
"config": {
"rateLimit": 100
},
"dependencies": {
"client": [
"example"
]
}
}
1 change: 1 addition & 0 deletions examples/example-gateway/gateway.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"gatewayName": "example-gateway",
"packageRoot": "github.com/uber/zanzibar/examples/example-gateway",
"thriftRootDir": "./idl",
"gatewayNamespace": "github.com/uber/zanzibar",
"genCodePackage": "github.com/uber/zanzibar/examples/example-gateway/build/gen-code",
Expand Down
Loading

0 comments on commit ab97b38

Please sign in to comment.