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

feat(hz): hz generate client #471

Merged
merged 13 commits into from
Dec 21, 2022
39 changes: 39 additions & 0 deletions cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ func Model(c *cli.Context) error {
return nil
}

func Client(c *cli.Context) error {
args, err := globalArgs.Parse(c, meta.CmdClient)
if err != nil {
return cli.Exit(err, meta.LoadError)
}
setLogVerbose(args.Verbose)
logs.Debugf("Args: %#v\n", args)

err = TriggerPlugin(args)
if err != nil {
return cli.Exit(err, meta.PluginError)
}

return nil
}

func PluginMode() {
pluginName := filepath.Base(os.Args[0])
if util.IsWindows() {
Expand Down Expand Up @@ -240,6 +256,29 @@ func Init() *cli.App {
},
Action: Model,
},
{
Name: meta.CmdClient,
Usage: "Generate hertz client based IDL",
welkeyever marked this conversation as resolved.
Show resolved Hide resolved
Flags: []cli.Flag{
&idlFlag,
&moduleFlag,
&modelDirFlag,

&includesFlag,
&thriftOptionsFlag,
&protoOptionsFlag,
&noRecurseFlag,

&jsonEnumStrFlag,
&unsetOmitemptyFlag,
&protoCamelJSONTag,
&snakeNameFlag,
&excludeFilesFlag,
&protoPluginsFlag,
&thriftPluginsFlag,
},
Action: Client,
},
}
return app
}
Expand Down
81 changes: 81 additions & 0 deletions cmd/hz/generator/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package generator
FGYFFFF marked this conversation as resolved.
Show resolved Hide resolved

import (
"path/filepath"

"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
)

type ClientMethod struct {
*HttpMethod
BodyParamsCode string
QueryParamsCode string
PathParamsCode string
HeaderParamsCode string
FormValueCode string
FormFileCode string
}

type ClientFile struct {
FilePath string
ServiceName string
BaseDomain string
Imports map[string]*model.Model
ClientMethods []*ClientMethod
}

func (pkgGen *HttpPackageGenerator) genClient(pkg *HttpPackage, clientDir string) error {
for _, s := range pkg.Services {
hertzClientPath := filepath.Join(clientDir, "hertz_client.go")
isExist, err := util.PathExist(hertzClientPath)
if err != nil {
return err
}
if !isExist {
err := pkgGen.TemplateGenerator.Generate(nil, hertzClientTplName, hertzClientPath, false)
if err != nil {
return err
}
}
var client ClientFile
client = ClientFile{
welkeyever marked this conversation as resolved.
Show resolved Hide resolved
FilePath: filepath.Join(clientDir, util.ToSnakeCase(s.Name)+".go"),
ServiceName: util.ToSnakeCase(s.Name),
ClientMethods: s.ClientMethods,
BaseDomain: s.BaseDomain,
}
client.Imports = make(map[string]*model.Model, len(client.ClientMethods))
for _, m := range client.ClientMethods {
// Iterate over the request and return parameters of the method to get import path.
for key, mm := range m.Models {
if v, ok := client.Imports[mm.PackageName]; ok && v.Package != mm.Package {
client.Imports[key] = mm
continue
}
client.Imports[mm.PackageName] = mm
}
}
err = pkgGen.TemplateGenerator.Generate(client, serviceClientName, client.FilePath, false)
if err != nil {
return err
}
}
return nil
}
19 changes: 16 additions & 3 deletions cmd/hz/generator/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ type HttpPackage struct {
}

type Service struct {
Name string
Methods []*HttpMethod
Models []*model.Model // all dependency models
Name string
Methods []*HttpMethod
ClientMethods []*ClientMethod
Models []*model.Model // all dependency models
BaseDomain string // base domain for client code
}

type HttpPackageGenerator struct {
ConfigPath string
Backend meta.Backend
Options []Option
CmdType string
ProjPackage string
HandlerDir string
RouterDir string
ModelDir string
ClientDir string
IdlClientDir string
NeedModel bool
HandlerByMethod bool

Expand Down Expand Up @@ -136,6 +140,15 @@ func (pkgGen *HttpPackageGenerator) Generate(pkg *HttpPackage) error {
}
}

if pkgGen.CmdType == meta.CmdClient {
clientDir := pkgGen.IdlClientDir
clientDir = util.SubDir(clientDir, "hertz")
if err := pkgGen.genClient(pkg, clientDir); err != nil {
return err
}
return nil
}

// this is for handler_by_service, the handler_dir is {$HANDLER_DIR}/{$PKG}
handlerDir := util.SubDir(pkgGen.HandlerDir, pkg.Package)
if pkgGen.HandlerByMethod {
Expand Down
Loading