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

update #37

Merged
merged 16 commits into from
Dec 31, 2024
2 changes: 2 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Run() {
if err != nil {
pterm.Error.Printfln("load state error\n%s", err.Error())
log.Errorf("err:%v", err)
os.Exit(1)
return
}

Expand All @@ -59,6 +60,7 @@ func Run() {
err = rootCmd.Execute()
if err != nil {
log.Errorf("err:%v", err)
os.Exit(1)
return
}
}
5 changes: 3 additions & 2 deletions codegen/generate_add_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"bytes"
"fmt"
"github.com/lazygophers/codegen/state"
"github.com/lazygophers/log"
"github.com/lazygophers/utils/candy"
"github.com/pterm/pterm"
Expand Down Expand Up @@ -153,7 +154,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 Expand Up @@ -213,7 +214,7 @@ func GenerateAddRpc(pb *PbPackage, msg *PbMessage, opt *AddRpcOption) (err error
args["RpcName"] = rpcName
args["RequestType"] = rpcName + "Req"
args["ResponseType"] = rpcName + "Rsp"
args["Path"] = CoverageStyledName(rpcName, opt.Model)
args["Path"] = CoverageStyledNamePath(state.Config.Style.Path, rpcName, opt.Model)

// 处理 server.rpc
if rpc := pb.GetRPC(rpcName); rpc == nil {
Expand Down
26 changes: 13 additions & 13 deletions codegen/generate_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func generateImpl(pb *PbPackage, rpc *PbRPC) (err error) {
case "str", "string":
opt.FieldType = "string"

case "strs", "stringslice", "string-slice":
case "strs", "stringslice", "string-slice", "[]string":
opt.FieldType = "string-slice"

case "like":
Expand Down Expand Up @@ -189,40 +189,40 @@ func generateImpl(pb *PbPackage, rpc *PbRPC) (err error) {
case "b", "bool":
opt.FieldType = "bool"

case "is", "ints", "intslice", "int-slice":
case "is", "ints", "intslice", "int-slice", "[]int":
opt.FieldType = "int-slice"

case "i8s", "int8s", "int8slice", "int8-slice":
case "i8s", "int8s", "int8slice", "int8-slice", "[]int8":
opt.FieldType = "int8-slice"

case "i16s", "int16s", "int16slice", "int16-slice":
case "i16s", "int16s", "int16slice", "int16-slice", "[]int16":
opt.FieldType = "int16-slice"

case "i32s", "int32s", "int32slice", "int32-slice":
case "i32s", "int32s", "int32slice", "int32-slice", "[]int32":
opt.FieldType = "int32-slice"

case "i64s", "int64s", "int64slice", "int64-slice":
case "i64s", "int64s", "int64slice", "int64-slice", "[]int64":
opt.FieldType = "int64-slice"

case "us", "uints", "uintslice", "uint-slice":
case "us", "uints", "uintslice", "uint-slice", "[]uint":
opt.FieldType = "uint-slice"

case "u16s", "uint16s", "uint16slice", "uint16-slice":
case "u16s", "uint16s", "uint16slice", "uint16-slice", "[]uint16":
opt.FieldType = "uint16-slice"

case "u32s", "uint32s", "uint32slice", "uint32-slice":
case "u32s", "uint32s", "uint32slice", "uint32-slice", "[]uint32":
opt.FieldType = "uint32-slice"

case "u64s", "uint64s", "uint64slice", "uint64-slice":
case "u64s", "uint64s", "uint64slice", "uint64-slice", "[]uint64":
opt.FieldType = "uint64-slice"

case "f32s", "float32s", "float32slice", "float32-slice":
case "f32s", "float32s", "float32slice", "float32-slice", "[]float32":
opt.FieldType = "float32-slice"

case "f64s", "float64s", "float64slice", "float64-slice":
case "f64s", "float64s", "float64slice", "float64-slice", "[]float64":
opt.FieldType = "float64-slice"

case "bs", "bools", "boolslice", "bool-slice":
case "bs", "bools", "boolslice", "bool-slice", "[]bool":
opt.FieldType = "bool-slice"

}
Expand Down
85 changes: 75 additions & 10 deletions codegen/generate_struct_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,26 @@ func (p tagItems) override() tagItems {
v = candy.Filter(v, func(s string) bool {
return s != ""
})
v = candy.Unique(v)

// 校验如果存在 - ,则跳过
if candy.Contains(v, "-") {
overrided = append(overrided, tagItem{
key: k,
value: "-",
})
continue
}

switch k {
case "gorm":
if candy.Contains(v, "autoIncrement") {
// 自动的,不添加 default
v = candy.FilterNot(v, func(s string) bool {
return strings.Contains(s, "default:")
})
}

overrided = append(overrided, tagItem{
key: k,
value: strings.Join(v, ";"),
Expand Down Expand Up @@ -296,11 +313,11 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {
}

for _, value := range strings.Split(values, seq) {
idx := strings.Index(value, connect)
if idx < 0 {
before, _, found := strings.Cut(value, connect)
if !found {
delete(m, value)
} else {
delete(m, value[:idx])
delete(m, before)
}
}

Expand All @@ -321,13 +338,43 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {

// 先按照类型获取一下
var getFieldType func(xx ast.Expr) string
getFieldType = func(xx ast.Expr) string {
var getObjType func(xx *ast.Object) string

getObjType = func(xx *ast.Object) string {
if xx == nil {
return ""
}

if xx.Decl != nil {
switch x := xx.Decl.(type) {
case *ast.TypeSpec:
return getFieldType(x.Type)

default:
log.Panicf("unknown type %T", x)
}
}

return ""
}

getFieldType = func(xx ast.Expr) (name string) {
switch x := xx.(type) {
case *ast.Ident:
name = getObjType(x.Obj)
if name != "" {
return name
}

return x.Name

case *ast.StarExpr:
return "*" + getFieldType(x.X)
name = getFieldType(x.X)
if name == "" {
return ""
}

return "*" + name

case *ast.ArrayType:
return "array"
Expand All @@ -336,7 +383,11 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {
return "map"

case *ast.SelectorExpr:
return "*" + getFieldType(x.X)
// TODO
return ""

case *ast.StructType:
return "object"

default:
log.Panicf("unknown type %T", x)
Expand All @@ -347,16 +398,24 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {

fieldType := getFieldType(field.Type)

log.Infof("field type: %s", fieldType)
log.Infof("%s field type: %s", fieldName, fieldType)

tagsMap := make(map[string][]string)
tagsMap := map[string][]string{
"yaml": {
CoverageStyledBase(state.Config.Style.Yaml, fieldName),
"omitempty",
},
}

if tm := state.Config.DefaultTag[fieldType]; tm != nil {
if tm := state.Config.DefaultTag["@"+fieldType]; tm != nil {
for k, v := range tm {
tagsMap[k] = append(tagsMap[k], v)
}
}

switch fieldType {
case "":

case "int32", "int64", "uint32", "uint64", "sint32", "sint64":

case "float", "double", "float32", "float64":
Expand All @@ -365,8 +424,14 @@ func InjectTagParseFile(inputPath string) ([]textArea, error) {

case "bool":

case "array":

case "map":

case "object":

default:
if tm := state.Config.DefaultTag["object"]; tm != nil {
if tm := state.Config.DefaultTag["@object"]; tm != nil {
for k, v := range tm {
tagsMap[k] = append(tagsMap[k], v)
}
Expand Down
Loading
Loading