Skip to content

Commit

Permalink
Merge pull request #38 from lazygophers/luoxin
Browse files Browse the repository at this point in the history
修复部分 bug
  • Loading branch information
Luoxin authored Jan 2, 2025
2 parents 6059a1e + 3eb0e01 commit 0209145
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
6 changes: 5 additions & 1 deletion cli/gen/gen_add_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ var addRpcCmd = &cobra.Command{
}

var msg *codegen.PbMessage
if v, err := cmd.Flags().GetString("model"); err == nil && v != "" {
if v, err := cmd.Flags().GetString("model"); err == nil && (v != "" || opt.GenTo != "") {
if v == "" {
v = opt.GenTo
}

// 先看一下加了 Model 的 时候存在
{
model := stringx.ToSnake(v)
Expand Down
10 changes: 7 additions & 3 deletions codegen/generate_add_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ type AddRpcOption struct {
func (p *AddRpcOption) ParseActions(s string) {
candy.Each(strings.Split(s, ";"), func(item string) {
idx := strings.Index(item, ":")
if idx < 0 {
if idx < 0 && p.Action[item] != nil {
p.Action[item] = &AddRpcOptionAction{}
return
}

action := item[:idx]
p.Action[action] = &AddRpcOptionAction{}
if p.Action[action] == nil {
p.Action[action] = &AddRpcOptionAction{}
}

candy.Each(strings.Split(item[idx+1:], ","), func(item string) {
// TODO: 更多的格式解析
Expand All @@ -58,6 +60,8 @@ func (p *AddRpcOption) ParseActions(s string) {

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

log.Info()
}

func (p *AddRpcOption) ParseListOption(s string, msg *PbMessage) {
Expand Down Expand Up @@ -154,7 +158,7 @@ func GenerateAddRpc(pb *PbPackage, msg *PbMessage, opt *AddRpcOption) (err error

// NOTE: 寻找主键
pkField := msg.PrimaryField()

var rpcBlock string

for action, actionOpt := range opt.Action {
Expand Down
59 changes: 52 additions & 7 deletions codegen/pbparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type PbEnum struct {
fields []*PbEnumField
}

func (p *PbEnum) FieldList() []*PbEnumField {
return p.fields
}

func (p *PbEnum) Enum() *proto.Enum {
return p.enum
}
Expand Down Expand Up @@ -212,6 +216,22 @@ func (p *PbRPC) ResponsePackage() string {
return p.responsePackage
}

func (p *PbRPC) Method() string {
if p.genOption.Method != "" {
return p.genOption.Method
}

return "POST"
}

func (p *PbRPC) Path() string {
if p.genOption.Path != "" {
return p.genOption.Path
}

return "/" + p.Name
}

func (p *PbRPC) walk() {
for _, option := range p.rpc.Options {
p.options[option.Name] = make(map[string]string, len(option.AggregatedConstants))
Expand Down Expand Up @@ -269,7 +289,7 @@ func (p *PbRPC) walk() {
}

if gen.Path != nil {
p.genOption.Path = *gen.Path
p.genOption.Path = "/" + strings.TrimPrefix(*gen.Path, "/")
}
}

Expand Down Expand Up @@ -308,24 +328,21 @@ func (p *PbRPC) walk() {
}
if idx := strings.LastIndex(text, "."); idx > 0 {
p.requestPackage = text[idx+1:]
} else {
p.requestPackage = text
text = text[:idx]
}
}

if strings.Contains(p.rpc.ReturnsType, ".") {
text := p.rpc.ReturnsType
if idx := strings.LastIndex(text, "."); idx > 0 {
p.responsePackage = text[idx+1:]
p.returnsType = text[idx+1:]
text = text[:idx]
}
if idx := strings.LastIndex(text, "."); idx > 0 {
p.responsePackage = text[idx+1:]
} else {
p.responsePackage = text
text = text[:idx]
}
}

}

func NewPbRPC(rpc *proto.RPC) *PbRPC {
Expand Down Expand Up @@ -475,6 +492,32 @@ type PbEnumField struct {
field *proto.EnumField
comment *PbComment
FullName string
Value int32
}

func (p *PbEnumField) Desc() string {
// 先尝试找 @desc 标记
if p.comment != nil {
value, ok := p.comment.tags["desc"]
if ok && len(value.Lines()) > 0 {
return strings.Join(value.Lines(), "")
}
}

// 尝试找后面接着的注释
if p.field.InlineComment != nil && len(p.field.InlineComment.Lines) > 0 {
return strings.Join(p.field.InlineComment.Lines, "")
}

// 找空行标记
if p.comment != nil {
value, ok := p.comment.tags[""]
if ok && len(value.Lines()) > 0 {
return strings.Join(value.Lines(), "")
}
}

return ""
}

func (p *PbEnumField) FieldName() string {
Expand All @@ -496,6 +539,8 @@ func (p *PbEnumField) walk() {
if p.field.Comment != nil {
p.comment = NewPbComment(p.field.Comment)
}

p.Value = int32(p.field.Integer)
}

func NewPbEnumField(f *proto.EnumField) *PbEnumField {
Expand Down
4 changes: 2 additions & 2 deletions codegen/template/client.call.gtpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func {{ .RPC.RpcName }}(ctx *lrpc.Ctx, req *{{ .RequestType }}) (*{{ .ResponseType }}, error) {
var rsp {{ .ResponseType }}
func {{ .RPC.RpcName }}(ctx *lrpc.Ctx, req *{{ .RequestPackage }}.{{ .RequestType }}) (*{{ .ResponsePackage }}.{{ .ResponseType }}, error) {
var rsp {{ .ResponsePackage }}.{{ .ResponseType }}
return &rsp, lrpc.Call(ctx, &core.ServiceDiscoveryClient{
ServiceName: ServerName,
ServicePath: RpcPath{{ .RPC.RpcName }},
Expand Down
2 changes: 1 addition & 1 deletion state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (p *Cfg) apply() (err error) {
addStringType := func(typ string) {
typ = "@" + typ
if _, ok := p.DefaultTag["gorm"][typ]; !p.Tables.DisableFieldId && !ok {
p.DefaultTag["gorm"][typ] = "type:varchar(255);not null"
p.DefaultTag["gorm"][typ] = "type:varchar(255);not null;default:''"
} else if p.Tables.DisableFieldType && ok {
delete(p.DefaultTag["gorm"], typ)
}
Expand Down

0 comments on commit 0209145

Please sign in to comment.