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

新增 internal 层级 #16

Merged
merged 2 commits into from
May 27, 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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
- 为了简化实现,不区分空值(默认值)与未传入的区别,所以在使用时需要注意对空值的处理
- 所有的数据表的类型名都需要以 `Model` 为开头,例如 `ModelUser`,`ModelOrder` 等
- 除非用户主动开启,否则不会自动执行以下操作
- 数据表默认使用 `id` 作为主键,如果存在字段名为 `id` 的字段,那么将会使用该字段作为主键,并且主动为其添加
相关 `gorm` 标签
- 数据表默认使用 `id` 作为主键,如果存在字段名为 `id` 的字段,那么将会使用该字段作为主键,并且主动为其添加相关 `gorm` 标签
- 数据表默认使用 `created_at`、`updated_at`、`deleted_at`
作为时间字段,如果存在字段名为 `created_at`、`updated_at`、`deleted_at` 的字段,那么将会使用该字段作为时间字段,并且主动为其添加
相关 `gorm` 标签
Expand Down Expand Up @@ -93,6 +92,12 @@ protogen_go_path: "<protoc-gen-go的绝对路径>"
output_path: "<生成的代码的目录>"
go_module_prefix: "<go module的前缀,在生成时会拼接proto中的 go_package 字段当做包名>"

# 不在 指定 proto 文件所在目录的其它被引用的包路径
# 支持绝对路径、相对生成路径 (相对于 output_path)
proto_files:
- "./core/"
- "~/proto/"

# 自定义模版文件,要求 .gtpl 格式
template:
# .editorconfig 模板文件路径,用于 IDE 的默认格式化
Expand Down
69 changes: 28 additions & 41 deletions codegen/generate_add_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
action.Roles = append(action.Roles, p.DefaultRole)
}

action.Roles = candy.Unique(action.Roles)

Check failure on line 58 in codegen/generate_add_rpc.go

View workflow job for this annotation

GitHub Actions / lint

undefined: candy.Unique (typecheck)
}
}

Expand Down Expand Up @@ -100,59 +100,48 @@
// 找到最后一个 service 里面 rpc 的位置
var lastRpcPos int
{
rpc := candy.Last(pb.RPCs())
if rpc != nil {
// 找到这一个 rpc 的结尾
s := rpc.RPC().Position.Offset
foundEnd := func(s int, next bool) {
e := s
poolCnt := 0

for e <= len(pb.ProtoBuffer) {
for e < len(pb.ProtoBuffer) &&
pb.ProtoBuffer[e] != '\n' {
e = s
for e < len(pb.ProtoBuffer) {
e++
switch pb.ProtoBuffer[e-1] {
case '\n':
goto END
case '{':
poolCnt++
case '}':
poolCnt--
}
}

line := pb.ProtoBuffer[s:e]

line = strings.ReplaceAll(line, " ", "")
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\t", "")
if line == "};" || line == "}" {
lastRpcPos = e // 当前行的换行符
END:
if poolCnt == 0 {
if next {
lastRpcPos = e
} else {
lastRpcPos = s - 1
}
break
}

s = e + 1
e = s
s = e
}
}
}

if lastRpcPos == 0 {
service := candy.Last(pb.Services())
if service != nil {
rpc := candy.Last(pb.RPCs())
if rpc != nil {
// 找到这一个 rpc 的结尾
s := service.Service().Position.Offset
e := s

for e <= len(pb.ProtoBuffer) {
for e < len(pb.ProtoBuffer) &&
pb.ProtoBuffer[e] != '\n' {
e++
}

line := pb.ProtoBuffer[s:e]

line = strings.ReplaceAll(line, " ", "")
line = strings.ReplaceAll(line, "\r", "")
line = strings.ReplaceAll(line, "\t", "")
if line == "}" {
lastRpcPos = s - 1
break
}
foundEnd(rpc.RPC().Position.Offset, true)
}

s = e + 1
e = s
if lastRpcPos == 0 {
service := candy.Last(pb.Services())
if service != nil {
foundEnd(service.Service().Position.Offset, false)
}
}
}
Expand Down Expand Up @@ -252,8 +241,6 @@
return err
}

log.Info(args["ListOptions"])

var b bytes.Buffer
err = tpl.Execute(&b, args)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions codegen/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
PathTypeOrm
PathTypeTableName

PathTypeInternal
PathTypeState
PathTypeStateTable
PathTypeStateConf
Expand All @@ -35,20 +36,23 @@ func GetPath(t PathType, pb *PbPackage) string {
case PathTypeTableName:
return filepath.Join(pb.ProjectRoot(), "table_name.gen.go")

case PathTypeInternal:
return filepath.Join(pb.ProjectRoot(), "internal")

case PathTypeState:
return filepath.Join(pb.ProjectRoot(), "state")
return filepath.Join(GetPath(PathTypeInternal, pb), "state")

case PathTypeStateTable:
return filepath.Join(pb.ProjectRoot(), "state", "table.go")
return filepath.Join(GetPath(PathTypeState, pb), "table.go")

case PathTypeStateConf:
return filepath.Join(pb.ProjectRoot(), "state", "config.go")
return filepath.Join(GetPath(PathTypeState, pb), "config.go")

case PathTypeStateCache:
return filepath.Join(pb.ProjectRoot(), "state", "cache.go")
return filepath.Join(GetPath(PathTypeState, pb), "cache.go")

case PathTypeStateState:
return filepath.Join(pb.ProjectRoot(), "state", "state.go")
return filepath.Join(GetPath(PathTypeState, pb), "state.go")

default:
panic("unsupported path type")
Expand Down
4 changes: 2 additions & 2 deletions codegen/template/state/table.gtpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package state
import (
{{ .PB.GoPackage }}
"github.com/lazygophers/log"
"github.com/lazygophers/utils/common"
"github.com/lazygophers/lrpc/middleware/xerror"
"github.com/lazygophers/utils/db"
)

Expand All @@ -23,7 +23,7 @@ func ConnectDatebase() (err error) {
return err
}

{{ range $key, $value := .Models}} {{TrimPrefix $value "Model"}} = db.NewModel[{{ $.PB.GoPackageName }}.{{ $value }}](Db()).SetNotFound(common.NewError({{ $.PB.GoPackageName }}.ErrCode_{{TrimPrefix $value "Model"}}NotFound))
{{ range $key, $value := .Models}} {{TrimPrefix $value "Model"}} = db.NewModel[{{ $.PB.GoPackageName }}.{{ $value }}](Db()).SetNotFound(xerror.NewError({{ $.PB.GoPackageName }}.ErrCode_{{TrimPrefix $value "Model"}}NotFound))
{{ end }}
log.Info("connect mysql successfully")

Expand Down
6 changes: 4 additions & 2 deletions example.codegen.cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ protogen_go_path: "<protoc-gen-go的绝对路径>"
output_path: "<生成的代码的目录>"
go_module_prefix: "<go module的前缀,在生成时会拼接proto中的 go_package 字段当做包名>"

# 不在 指定 proto 文件所在目录的其它被引用的包路径
# 支持绝对路径、相对生成路径 (相对于 output_path)
proto_files:
- ~/proto/
- ~/core/
- "./core/"
- "~/proto/"

# 自定义模版文件,要求 .gtpl 格式
template:
Expand Down
Loading