示例#1
0
文件: main.go 项目: yarpc/yarpc-go
func (generator) Generate(req *api.GenerateServiceRequest) (*api.GenerateServiceResponse, error) {
	files := make(map[string][]byte)
	for _, serviceID := range req.RootServices {
		service := req.Services[serviceID]
		module := req.Modules[service.ModuleID]

		var (
			parent       *api.Service
			parentModule *api.Module
		)
		if service.ParentID != nil {
			parent = req.Services[*service.ParentID]
			parentModule = req.Modules[parent.ModuleID]
		}

		templateData := struct {
			Module       *api.Module
			Service      *api.Service
			Parent       *api.Service
			ParentModule *api.Module
		}{
			Module:       module,
			Service:      service,
			Parent:       parent,
			ParentModule: parentModule,
		}

		// kv.thrift => .../kv/yarpc
		baseDir := filepath.Join(module.Directory, "yarpc")

		serverPackageName := strings.ToLower(service.Name) + "server"
		clientPackageName := strings.ToLower(service.Name) + "client"

		// kv.thrift =>
		//   .../yarpc/keyvalueserver/server.go
		//   .../yarpc/keyvalueclient/client.go
		serverFilePath := filepath.Join(baseDir, serverPackageName, "server.go")
		clientFilePath := filepath.Join(baseDir, clientPackageName, "client.go")

		serverContents, err := plugin.GoFileFromTemplate(
			serverFilePath, serverTemplate, templateData, templateOptions...)
		if err != nil {
			return nil, err
		}

		clientContents, err := plugin.GoFileFromTemplate(
			clientFilePath, clientTemplate, templateData, templateOptions...)
		if err != nil {
			return nil, err
		}

		files[serverFilePath] = serverContents
		files[clientFilePath] = clientContents
	}
	return &api.GenerateServiceResponse{Files: files}, nil
}
示例#2
0
func (sgen) Generate(req *api.GenerateServiceRequest) (*api.GenerateServiceResponse, error) {
	files := make(map[string][]byte)
	for _, serviceID := range req.RootServices {
		service := req.Services[serviceID]
		module := req.Modules[service.ModuleID]

		templateData := struct {
			Service *api.Service
			Request *api.GenerateServiceRequest
		}{Service: service, Request: req}

		var (
			err  error
			opts []plugin.TemplateOption
		)

		opts = append(opts, templateOptions...)
		opts = append(opts, plugin.GoFileImportPath(module.ImportPath))

		ifacePath := filepath.Join(module.Directory, strings.ToLower(service.Name)+".go")
		files[ifacePath], err = plugin.GoFileFromTemplate(
			ifacePath, interfaceTemplate, templateData, opts...)
		if err != nil {
			return nil, err
		}

		clientPath := filepath.Join(module.Directory, strings.ToLower(service.Name)+"_client.go")
		files[clientPath], err = plugin.GoFileFromTemplate(
			clientPath, clientTemplate, templateData, opts...)
		if err != nil {
			return nil, err
		}

		handlerPath := filepath.Join(module.Directory, strings.ToLower(service.Name)+"_handler.go")
		files[handlerPath], err = plugin.GoFileFromTemplate(
			handlerPath, handlerTemplate, templateData, opts...)
		if err != nil {
			return nil, err
		}
	}
	return &api.GenerateServiceResponse{Files: files}, nil
}