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

support generics #197

Merged
merged 1 commit into from
Apr 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
8 changes: 7 additions & 1 deletion cmd/internal/svc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/iancoleman/strcase"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/v2/toolkit/astutils"
"github.com/unionj-cloud/go-doudou/v2/toolkit/constants"
Expand Down Expand Up @@ -540,7 +541,12 @@ func ExprStringP(expr ast.Expr) string {
case *ast.ChanType:
panic("not support channel as struct field type in vo and dto package and as parameter in method signature in svc.go file")
case *ast.IndexExpr:
return ExprStringP(_expr.X)
return ExprStringP(_expr.X) + "[" + ExprStringP(_expr.Index) + "]"
case *ast.IndexListExpr:
typeParams := lo.Map[ast.Expr, string](_expr.Indices, func(item ast.Expr, index int) string {
return ExprStringP(item)
})
return ExprStringP(_expr.X) + "[" + strings.Join(typeParams, ", ") + "]"
default:
logrus.Infof("not support expression: %+v\n", expr)
logrus.Infof("not support expression: %+v\n", reflect.TypeOf(expr))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ require (
github.com/containerd/cgroups v1.0.3 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand Down
8 changes: 7 additions & 1 deletion toolkit/astutils/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/v2/toolkit/constants"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
Expand Down Expand Up @@ -328,7 +329,12 @@ func ExprString(expr ast.Expr) string {
}
panic(fmt.Sprintf("invalid ellipsis expression: %+v\n", expr))
case *ast.IndexExpr:
return ExprString(_expr.X)
return ExprString(_expr.X) + "[" + ExprString(_expr.Index) + "]"
case *ast.IndexListExpr:
typeParams := lo.Map[ast.Expr, string](_expr.Indices, func(item ast.Expr, index int) string {
return ExprString(item)
})
return ExprString(_expr.X) + "[" + strings.Join(typeParams, ", ") + "]"
default:
logrus.Infof("not support expression: %+v\n", expr)
logrus.Infof("not support expression: %+v\n", reflect.TypeOf(expr))
Expand Down
2 changes: 2 additions & 0 deletions toolkit/astutils/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package astutils

import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -318,6 +319,7 @@ func TestNewMethodMeta(t *testing.T) {
}
sc := NewStructCollector(ExprString)
ast.Walk(sc, root)
spew.Dump(root)
}

func TestGetImportStatements(t *testing.T) {
Expand Down
30 changes: 29 additions & 1 deletion toolkit/astutils/testdata/cat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package main

import "fmt"
import (
"context"
"fmt"
)

type User struct {
Name string `json:"name"`
}

type Cat struct {
Hobbies map[string]interface{}
Expand All @@ -17,3 +24,24 @@ func (c *Cat) eat(food string) (
fmt.Println("eat " + food)
return true, "happy"
}

func (c *Cat) PostSelectVersionPage(ctx context.Context, body PageDTO[VersionDTO, Cat, User]) (code int, message string, data PageDTO[VersionDTO, Cat, User], err error) {
return 0, "", PageDTO[VersionDTO, Cat, User]{}, err
}

type VersionDTO struct {
VersionName string `json:"versionName"`
VersionId interface{} `json:"versionId"`
}

type PageDTO[T any, R any, K any] struct {
TotalRow int `json:"totalRow"`
PageNumber int `json:"pageNumber"`
TotalPage int `json:"totalPage"`
PageSize int `json:"pageSize"`
ReturnMsg string `json:"return_msg"`
List []T `json:"list"`
Item []R `json:"item"`
Many []K `json:"many"`
ReturnCode string `json:"return_code"`
}
4 changes: 0 additions & 4 deletions toolkit/gormgen/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,6 @@ func (p *Param) astGetParamType(param *ast.Field) {
case *ast.StarExpr:
p.IsPointer = true
p.astGetEltType(v.X)
case *ast.IndexExpr:
p.astGetEltType(v.X)
default:
log.Fatalf("unknow param type: %+v", v)
}
Expand All @@ -298,8 +296,6 @@ func (p *Param) astGetEltType(expr ast.Expr) {
case *ast.ArrayType:
p.astGetEltType(v.Elt)
p.Type = "[]" + p.Type
case *ast.IndexExpr:
p.astGetEltType(v.X)
default:
log.Fatalf("unknow param type: %+v", v)
}
Expand Down
Loading