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

通过 shell 生成针对 model 的 rpc 结构定义 #14

Merged
merged 5 commits into from
May 25, 2024
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ lint: ## lint go install github.com/golangci/golangci-lint/cmd/golangci-lint@lat

help: ## Show this help
@egrep '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | sed 's/Makefile://' | awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-z0-9A-Z_-]+:.*?##/ { printf " \033[36m%-30s\033[0m %s\n", $$1, $$2 }'

# make build && ./dist/cli_darwin_arm64/codegen gen -i ../example/example.proto -d add-rpc -m user -a add:,admin
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
- go.mod
- go.sum
- .editorconfig
- orm_gen.go(gorm 配套的接口协议的生成)
- table_gen.go(表名的生成)
- state
- table.go(数据库相关的内容)
- cache.go(缓存相关的内容)
Expand All @@ -59,7 +61,7 @@

4. 执行命令
```bash
codegen gen pb -i <proto文件路径>
codegen gen pb <action> -i <proto文件路径>
```

### 注意事项
Expand Down
94 changes: 94 additions & 0 deletions cli/gen_add_rpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cli

import (
"errors"
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/log"
"github.com/lazygophers/utils/stringx"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"strings"
)

var genAddRpcCmd = &cobra.Command{
Use: "add-rpc",
Short: "add rpc to proto with model",
RunE: func(cmd *cobra.Command, args []string) (err error) {
opt := codegen.NewAddRpcOption()

if v, err := cmd.Flags().GetString("default-role"); err == nil {
opt.DefaultRole = v
}

if v, err := cmd.Flags().GetString("gen-to"); err == nil {
opt.GenTo = v
}

var msg *codegen.PbMessage
if v, err := cmd.Flags().GetString("model"); err == nil && v != "" {
// 先看一下加了 Model 的 时候存在
{
model := stringx.ToSnake(v)

Check failure on line 31 in cli/gen_add_rpc.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stringx.ToSnake (typecheck)
model = strings.TrimPrefix(model, "model_")
model = "model_" + model
model = stringx.ToCamel(model)

Check failure on line 34 in cli/gen_add_rpc.go

View workflow job for this annotation

GitHub Actions / lint

undefined: stringx.ToCamel (typecheck)

msg = pb.GetMessage(model)
if msg != nil {
opt.Model = model
}
}

// 直接找名字的
if msg == nil {
msg = pb.GetMessage(v)
if msg != nil {
opt.Model = v
}
}

if msg == nil {
log.Errorf("not found model:%v", v)
pterm.Error.Printfln("not found model:%v", v)
return errors.New("not found model")
}
} else {
log.Errorf("missed argument `model`")
pterm.Error.Printfln("missing argument `model`")
return errors.New("missing argument `model`")
}

if v, err := cmd.Flags().GetString("action"); err == nil && v != "" {
opt.ParseActions(v)
} else {
log.Errorf("missed argument `action`")
pterm.Error.Printfln("missing argument `action`")
return errors.New("missing argument `action`")
}

if v, err := cmd.Flags().GetString("list-option"); err == nil && v != "" {
opt.ParseListOption(v, msg)
}

err = codegen.GenerateAddRpc(pb, msg, opt)
if err != nil {
log.Errorf("err:%v", err)
return err
}

return err
},
}

func init() {
genAddRpcCmd.Flags().StringP("model", "m", "", "model name,must be specified")
genAddRpcCmd.Flags().StringP("gen-to", "t", "", "generate go source code path, will be used in gen go source code")

genAddRpcCmd.Flags().StringP("action", "a", "", "action for adding, segmente by ';',\nsupports: add/set/get/list/del/update")

genAddRpcCmd.Flags().StringP("list-option", "l", "", "list options, segmente by ';'")

genAddRpcCmd.Flags().String("default-role", "", "default role")

genCmd.AddCommand(genAddRpcCmd)
}
2 changes: 1 addition & 1 deletion cli/gen_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func runGenState(cmd *cobra.Command, args []string) (err error) {
}

func init() {
rootCmd.AddCommand(genStateCmd)
genCmd.AddCommand(genStateCmd)
}
12 changes: 12 additions & 0 deletions cli/gen_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ var genTableCmd = &cobra.Command{
}

func runGenTable(cmd *cobra.Command, args []string) (err error) {
err = codegen.GenerateOrm(pb)
if err != nil {
log.Errorf("err:%v", err)
return err
}

err = codegen.GenerateTableName(pb)
if err != nil {
log.Errorf("err:%v", err)
return err
}

err = codegen.GenerateTable(pb)
if err != nil {
log.Errorf("err:%v", err)
Expand Down
Loading
Loading