Skip to content

Commit

Permalink
Merge pull request #32 from lazygophers/luoxin
Browse files Browse the repository at this point in the history
修复一些 bug
  • Loading branch information
Luoxin authored Jul 25, 2024
2 parents 5995842 + 05711fb commit 4fe9c5a
Show file tree
Hide file tree
Showing 155 changed files with 706 additions and 57 deletions.
17 changes: 17 additions & 0 deletions cli/gen/gen_impl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gen

import (
"github.com/lazygophers/codegen/cli/utils"
"github.com/lazygophers/codegen/codegen"
"github.com/lazygophers/codegen/state"
"github.com/lazygophers/log"
Expand All @@ -13,6 +14,8 @@ var implCmd = &cobra.Command{
}

var runGenImpl = func(cmd *cobra.Command, args []string) (err error) {
mergeImplFlage(cmd)

err = codegen.GenerateImpl(pb)
if err != nil {
log.Errorf("err:%v", err)
Expand Down Expand Up @@ -40,9 +43,23 @@ var runGenImpl = func(cmd *cobra.Command, args []string) (err error) {
return nil
}

func mergeImplFlage(cmd *cobra.Command) {
if utils.Changed("template-impl-route", cmd) {
state.Config.Template.Impl.Route = utils.GetString("template-impl-route", cmd)
}

if utils.Changed("template-impl-path", cmd) {
state.Config.Template.Impl.Path = utils.GetString("template-impl-path", cmd)
}
}

func initImpl() {
implCmd.Short = state.Localize(state.I18nTagCliGenImplShort)
implCmd.Long = state.Localize(state.I18nTagCliGenImplLong)

implCmd.Flags().String("template-impl-route", "", state.Localize(state.I18nTagCliGenImplFlagsTemplateImplRoute))

implCmd.Flags().String("template-impl-path", "", state.Localize(state.I18nTagCliGenImplFlagsTemplateImplPath))

genCmd.AddCommand(implCmd)
}
1 change: 1 addition & 0 deletions cli/i18n/tran.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var allLanguage = candy.Map([]string{
"pl",
"ps",
"pt",
"pt-br",
"qu",
"ro",
"ru",
Expand Down
7 changes: 7 additions & 0 deletions cli/utils/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package utils

import "github.com/spf13/cobra"

func Changed(key string, c *cobra.Command) bool {
if flag := c.Flag(key); flag != nil {
return flag.Changed
}
return false
}

func GetBool(key string, c *cobra.Command) (ok bool) {
ok, _ = c.Flags().GetBool(key)
return ok
Expand Down
32 changes: 32 additions & 0 deletions codegen/generate_i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"github.com/lazygophers/codegen/state"
"github.com/lazygophers/log"
"github.com/lazygophers/utils/anyx"
"github.com/lazygophers/utils/osx"
"github.com/pterm/pterm"
"io/fs"
Expand All @@ -28,6 +29,9 @@ func GenerateI18nConst(dstLocalize map[string]any, path string) (err error) {
case string:
localize[key] = x

case []byte:
localize[key] = string(x)

case map[string]any:
deepKeys(append(slices.Clone(ps), k), x)

Expand All @@ -38,6 +42,13 @@ func GenerateI18nConst(dstLocalize map[string]any, path string) (err error) {
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[string][]byte:
sub := make(map[string]any, len(x))
for k, v := range x {
sub[k] = string(v)
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[int64]any:
sub := make(map[string]any, len(x))
for k, v := range x {
Expand All @@ -52,6 +63,13 @@ func GenerateI18nConst(dstLocalize map[string]any, path string) (err error) {
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[int64][]byte:
sub := make(map[string]any, len(x))
for k, v := range x {
sub[strconv.FormatInt(k, 10)] = string(v)
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[float64]any:
sub := make(map[string]any, len(x))
for k, v := range x {
Expand All @@ -66,6 +84,20 @@ func GenerateI18nConst(dstLocalize map[string]any, path string) (err error) {
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[float64][]byte:
sub := make(map[string]any, len(x))
for k, v := range x {
sub[strconv.FormatFloat(k, 'f', -1, 64)] = string(v)
}
deepKeys(append(slices.Clone(ps), k), sub)

case map[any]any:
sub := make(map[string]any, len(x))
for k, v := range x {
sub[anyx.ToString(k)] = v
}
deepKeys(append(slices.Clone(ps), k), sub)

default:
log.Panicf("unsupported type:%T", x)
}
Expand Down
16 changes: 2 additions & 14 deletions codegen/generate_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,26 +560,14 @@ func GenerateClient(pb *PbPackage) (err error) {
}

matchFunc := func(rpc *PbRPC) bool {
for _, goFuncNode := range goPackage[pb.GoPackageName()].FuncMap {
goFunc := goFuncNode.goFunc

if goFunc.RecvType != "" {
continue
}

if goFunc.Name == rpc.Name {
pterm.Warning.Printfln("%s is exist, skip generate", rpc.Name)
return true
}
}

return false
return goPackage[pb.GoPackageName()].FuncMap[rpc.Name] != nil
}

for _, rpc := range pb.RPCs() {
//path := filepath.Join(GetPath(PathTypeImpl, pb), rpc.genOption.GenTo+".go")

if matchFunc(rpc) {
pterm.Warning.Printfln("%s is exist, skip generate client", rpc.Name)
continue
}

Expand Down
19 changes: 10 additions & 9 deletions codegen/generate_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func GenerateStateTable(pb *PbPackage) (err error) {
return
}

log.Infof("find table %s", message.Name)
log.Infof("find table %s", message.FullName)

models = append(models, message.Name)
models = append(models, message.FullName)
})

args["Models"] = models
Expand Down Expand Up @@ -79,13 +79,14 @@ func GenerateOrm(pb *PbPackage) (err error) {
{
var models []string
candy.Each(pb.Messages(), func(message *PbMessage) {
if !message.NeedOrm() {
return
}
// 先全部允许。实际使用的时候要考虑被 model 引用的场景
//if !message.NeedOrm() {
// return
//}

log.Infof("find orm object %s", message.Name)
log.Infof("find orm object %s", message.FullName)

models = append(models, message.Name)
models = append(models, message.FullName)
})

args["Models"] = models
Expand Down Expand Up @@ -134,9 +135,9 @@ func GenerateTableName(pb *PbPackage) (err error) {
return
}

log.Infof("find table %s", message.Name)
log.Infof("find table %s", message.FullName)

models = append(models, message.Name)
models = append(models, message.FullName)
})

args["Models"] = models
Expand Down
63 changes: 41 additions & 22 deletions codegen/pbparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func NewPbComment(c *proto.Comment) *PbComment {
type PbEnum struct {
enum *proto.Enum

Name string
FullName string

fields []*PbEnumField
}

Expand All @@ -129,11 +132,17 @@ func (p *PbEnum) Enum() *proto.Enum {
}

func (p *PbEnum) walk() {
p.Name = p.enum.Name
p.FullName = GetFullName(p.enum)

for _, element := range p.enum.Elements {
switch x := element.(type) {
case *proto.EnumField:
p.fields = append(p.fields, NewPbEnumField(x))

case *proto.Comment:
continue

default:
log.Panicf("unexpected enum field: %T", x)
}
Expand Down Expand Up @@ -186,7 +195,7 @@ func (p *PbRPC) walk() {
}

// 优先解析 options 的
if v, ok := p.options["lazygophers.lrpc.core.lazygen"]; ok {
if v, ok := p.options["(lazygophers.lrpc.core.lazygen)"]; ok {
var gen core.LazyGen
buffer, err := json.Marshal(v)
if err != nil {
Expand All @@ -200,16 +209,21 @@ func (p *PbRPC) walk() {
return
}

log.Info(v)
log.Info(gen)

if gen.Role != "" {
p.genOption.Role = gen.Role
}

if gen.SkipGenRoute {
p.genOption.SkipGenRoute = gen.SkipGenRoute
}
} else {
log.Info(p.options)
}

if v, ok := p.options["lazygophers.lrpc.core.http"]; ok {
if v, ok := p.options["(lazygophers.lrpc.core.http)"]; ok {
var gen core.Http
buffer, err := json.Marshal(v)
if err != nil {
Expand Down Expand Up @@ -391,9 +405,10 @@ func NewPbMapField(f *proto.MapField) *PbMapField {
}

type PbEnumField struct {
Name string
field *proto.EnumField
comment *PbComment
Name string
field *proto.EnumField
comment *PbComment
FullName string
}

func (p *PbEnumField) FieldName() string {
Expand All @@ -409,14 +424,16 @@ func (p *PbEnumField) IsSlice() bool {
}

func (p *PbEnumField) walk() {
p.Name = p.field.Name
p.FullName = GetFullName(p.field)

if p.field.Comment != nil {
p.comment = NewPbComment(p.field.Comment)
}
}

func NewPbEnumField(f *proto.EnumField) *PbEnumField {
p := &PbEnumField{
Name: f.Name,
field: f,
}

Expand All @@ -430,6 +447,7 @@ type PbMessage struct {
normalFields map[string]*PbNormalField
mapFields map[string]*PbMapField
enumFields map[string]*PbEnumField
FullName string
Name string
}

Expand Down Expand Up @@ -477,8 +495,8 @@ func (p *PbMessage) Message() *proto.Message {
}

func (p *PbMessage) IsTable() bool {
if strings.HasPrefix(p.Name, "Model") {
if !strings.Contains(p.Name, "_") {
if strings.HasPrefix(p.FullName, "Model") {
if !strings.Contains(p.FullName, "_") {
return true
}
}
Expand All @@ -489,7 +507,7 @@ func (p *PbMessage) IsTable() bool {
}

func (p *PbMessage) NeedOrm() bool {
if strings.HasPrefix(p.Name, "Model") {
if strings.HasPrefix(p.FullName, "Model") {
return true
}

Expand All @@ -499,6 +517,7 @@ func (p *PbMessage) NeedOrm() bool {
}

func (p *PbMessage) walk() {
p.FullName = GetFullName(p.message)
p.Name = p.message.Name

for _, element := range p.message.Elements {
Expand All @@ -507,17 +526,17 @@ func (p *PbMessage) walk() {
element.Accept(visitor)

for _, field := range visitor.mapFields {
pterm.Info.Printfln("find map field:%s in %s", field.Name, p.Name)
pterm.Info.Printfln("find map field:%s in %s", field.Name, p.FullName)
p.mapFields[field.Name] = NewPbMapField(field)
}

for _, field := range visitor.normalFields {
pterm.Info.Printfln("find normal field:%s in %s", field.Name, p.Name)
pterm.Info.Printfln("find normal field:%s in %s", field.Name, p.FullName)
p.normalFields[field.Name] = NewPbNormalField(field)
}

for _, field := range visitor.enumFields {
pterm.Info.Printfln("find enum field:%s in %s", field.Name, p.Name)
pterm.Info.Printfln("find enum field:%s in %s", field.Name, p.FullName)
p.enumFields[field.Name] = NewPbEnumField(field)
}
}
Expand Down Expand Up @@ -593,9 +612,9 @@ func (p *PbPackage) ProjectRoot() string {

func (p *PbPackage) GoPackage() string {
if state.Config.GoModulePrefix != "" {
return filepath.ToSlash(filepath.Join(state.Config.GoModulePrefix, p.RawGoPackage))
return strings.TrimPrefix(filepath.ToSlash(filepath.Join(state.Config.GoModulePrefix, p.RawGoPackage)), "/")
} else {
return filepath.ToSlash(p.RawGoPackage)
return strings.TrimPrefix(filepath.ToSlash(p.RawGoPackage), "/")
}
}

Expand Down Expand Up @@ -680,13 +699,13 @@ func GetFullName(e proto.Visitee) string {
func (p *PbPackage) Walk() {
proto.Walk(p.proto,
proto.WithMessage(func(m *proto.Message) {
m.Name = GetFullName(m)
name := GetFullName(m)

log.Infof("message:%v", m.Name)
pterm.Info.Printfln("find message:%s", m.Name)
pterm.Info.Printfln("find message:%s", name)

p.msgMap[m.Name] = NewPbMessage(m)
p.messages = append(p.messages, p.msgMap[m.Name])
p.msgMap[name] = NewPbMessage(m)
p.messages = append(p.messages, p.msgMap[name])
}),
proto.WithService(func(s *proto.Service) {
log.Infof("service:%v", s.Name)
Expand All @@ -703,13 +722,13 @@ func (p *PbPackage) Walk() {
p.rpcs = append(p.rpcs, p.rpcMap[r.Name])
}),
proto.WithEnum(func(e *proto.Enum) {
e.Name = GetFullName(e)
name := GetFullName(e)

log.Infof("enum:%v", e.Name)
pterm.Info.Printfln("find enum:%s", e.Name)
pterm.Info.Printfln("find enum:%s", name)

p.enumMap[e.Name] = NewPbEnum(e)
p.enums = append(p.enums, p.enumMap[e.Name])
p.enumMap[name] = NewPbEnum(e)
p.enums = append(p.enums, p.enumMap[name])
}),
proto.WithOption(func(option *proto.Option) {
log.Infof("option:%v", option.Name)
Expand Down
Loading

0 comments on commit 4fe9c5a

Please sign in to comment.