Skip to content

Commit

Permalink
feat: use third model
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Feb 17, 2023
1 parent 6f37d9d commit dd06c30
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func Init() *cli.App {
handlerDirFlag := cli.StringFlag{Name: "handler_dir", Usage: "Specify the handler relative path (based on \"out_dir\").", Destination: &globalArgs.HandlerDir}
modelDirFlag := cli.StringFlag{Name: "model_dir", Usage: "Specify the model relative path (based on \"out_dir\").", Destination: &globalArgs.ModelDir}
routerDirFlag := cli.StringFlag{Name: "router_dir", Usage: "Specify the router relative path (based on \"out_dir\").", Destination: &globalArgs.RouterDir}
useFlag := cli.StringFlag{Name: "use", Usage: "Specify the model package to import for handler.", Destination: &globalArgs.Use}
baseDomainFlag := cli.StringFlag{Name: "base_domain", Usage: "Specify the request domain.", Destination: &globalArgs.BaseDomain}
clientDirFlag := cli.StringFlag{Name: "client_dir", Usage: "Specify the client path. If not specified, IDL generated path is used for 'client' command; no client code is generated for 'new' command", Destination: &globalArgs.ClientDir}

Expand Down Expand Up @@ -209,6 +210,7 @@ func Init() *cli.App {
&modelDirFlag,
&routerDirFlag,
&clientDirFlag,
&useFlag,

&includesFlag,
&thriftOptionsFlag,
Expand Down Expand Up @@ -267,6 +269,7 @@ func Init() *cli.App {
&moduleFlag,
&outDirFlag,
&modelDirFlag,
&useFlag,

&includesFlag,
&thriftOptionsFlag,
Expand All @@ -290,6 +293,7 @@ func Init() *cli.App {
&baseDomainFlag,
&modelDirFlag,
&clientDirFlag,
&useFlag,

&includesFlag,
&thriftOptionsFlag,
Expand Down Expand Up @@ -397,7 +401,10 @@ func TriggerPlugin(args *config.Argument) error {
logs.Debugf("begin to trigger plugin, compiler: %s, idl_paths: %v", compiler, args.IdlPaths)
buf, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("plugin %s_gen_hertz returns error: %v, cause:\n%v", compiler, err, string(buf))
out := strings.TrimSpace(string(buf))
if !strings.HasSuffix(out, meta.TheUseOptionMessage) {
return fmt.Errorf("plugin %s_gen_hertz returns error: %v, cause:\n%v", compiler, err, string(buf))
}
}

// If len(buf) != 0, the plugin returned the log.
Expand Down
1 change: 1 addition & 0 deletions cmd/hz/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Argument struct {
Gomod string
Gopkg string // $GOPATH/src/{{gopkg}}
ServiceName string // service name
Use string

JSONEnumStr bool
UnsetOmitempty bool
Expand Down
10 changes: 10 additions & 0 deletions cmd/hz/generator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package generator

import (
"path/filepath"
"strings"

"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
Expand Down Expand Up @@ -78,6 +79,15 @@ func (pkgGen *HttpPackageGenerator) genClient(pkg *HttpPackage, clientDir string
client.Imports[mm.PackageName] = mm
}
}
if len(pkgGen.UseDir) != 0 {
oldModelDir := filepath.Clean(filepath.Join(pkgGen.ProjPackage, pkgGen.ModelDir))
newModelDir := filepath.Clean(pkgGen.UseDir)
for _, m := range client.ClientMethods {
for _, mm := range m.Models {
mm.Package = strings.Replace(mm.Package, oldModelDir, newModelDir, 1)
}
}
}
err = pkgGen.TemplateGenerator.Generate(client, idlClientName, client.FilePath, false)
if err != nil {
return err
Expand Down
10 changes: 10 additions & 0 deletions cmd/hz/generator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ func (pkgGen *HttpPackageGenerator) processHandler(handler *Handler, root *Route
}
}

if len(pkgGen.UseDir) != 0 {
oldModelDir := filepath.Clean(filepath.Join(pkgGen.ProjPackage, pkgGen.ModelDir))
newModelDir := filepath.Clean(pkgGen.UseDir)
for _, m := range handler.Methods {
for _, mm := range m.Models {
mm.Package = strings.Replace(mm.Package, oldModelDir, newModelDir, 1)
}
}
}

handler.Format()
return nil
}
Expand Down
1 change: 1 addition & 0 deletions cmd/hz/generator/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type HttpPackageGenerator struct {
HandlerDir string
RouterDir string
ModelDir string
UseDir string
ClientDir string
IdlClientDir string
NeedModel bool
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ const (
const (
SetBodyParam = "setBodyParam(req).\n"
)

// TheUseOptionMessage indicates that the generating of 'model code' is aborted due to the -use option for thrift IDL.
const TheUseOptionMessage = "'model code' is not generated due to the '-use' option"
8 changes: 7 additions & 1 deletion cmd/hz/protobuf/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Plugin struct {
Recursive bool
OutDir string
ModelDir string
UseDir string
IdlClientDir string
PkgMap map[string]string
logger *logs.StdLogger
Expand Down Expand Up @@ -165,6 +166,7 @@ func (plugin *Plugin) parseArgs(param string) (*config.Argument, error) {
}
plugin.OutDir = args.OutDir
plugin.PkgMap = args.OptPkgMap
plugin.UseDir = args.Use
return args, nil
}

Expand Down Expand Up @@ -349,7 +351,10 @@ func (plugin *Plugin) GenerateFile(gen *protogen.Plugin, f *protogen.File) error
}
f.GeneratedFilenamePrefix = filepath.Join(util.ImportToPath(impt, ""), util.BaseName(f.Proto.GetName(), ".proto"))
f.Generate = true

// if use third-party model, no model code is generated within the project
if len(plugin.UseDir) != 0 {
return nil
}
file, err := generateFile(gen, f)
if err != nil || file == nil {
return fmt.Errorf("generate file %s failed: %s", f.Proto.GetName(), err.Error())
Expand Down Expand Up @@ -586,6 +591,7 @@ func (plugin *Plugin) genHttpPackage(ast *descriptorpb.FileDescriptorProto, deps
HandlerDir: handlerDir,
RouterDir: routerDir,
ModelDir: modelDir,
UseDir: args.Use,
ClientDir: clientDir,
TemplateGenerator: generator.TemplateGenerator{
OutputDir: args.OutDir,
Expand Down
18 changes: 17 additions & 1 deletion cmd/hz/thrift/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package thrift

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -128,6 +129,7 @@ func (plugin *Plugin) Run() int {
HandlerDir: handlerDir,
RouterDir: routerDir,
ModelDir: modelDir,
UseDir: args.Use,
ClientDir: clientDir,
TemplateGenerator: generator.TemplateGenerator{
OutputDir: args.OutDir,
Expand All @@ -149,6 +151,20 @@ func (plugin *Plugin) Run() int {
logs.Errorf("generate package failed: %s", err.Error())
return meta.PluginError
}
if len(args.Use) != 0 {
err = sg.Persist()
if err != nil {
logs.Errorf("persist file failed within '-use' option: %s", err.Error())
return meta.PluginError
}
res := thriftgo_plugin.BuildErrorResponse(errors.New(meta.TheUseOptionMessage).Error())
err = plugin.response(res)
if err != nil {
logs.Errorf("response failed: %s", err.Error())
return meta.PluginError
}
return 0
}
files, err := sg.GetFormatAndExcludedFiles()
if err != nil {
logs.Errorf("format file failed: %s", err.Error())
Expand All @@ -159,7 +175,7 @@ func (plugin *Plugin) Run() int {
logs.Errorf("get response failed: %s", err.Error())
return meta.PluginError
}
plugin.response(res)
err = plugin.response(res)
if err != nil {
logs.Errorf("response failed: %s", err.Error())
return meta.PluginError
Expand Down

0 comments on commit dd06c30

Please sign in to comment.