Skip to content

Commit

Permalink
refactor: http inject and test
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Jun 5, 2024
1 parent 114c982 commit 2b3323f
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 351 deletions.
63 changes: 63 additions & 0 deletions cemetery.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,66 @@ func (c *cemetery) GetTomById(id GonerId) Tomb {
func (c *cemetery) GetTomByType(t reflect.Type) (tombs []Tomb) {
return Tombs(c.tombs).GetTomByType(t)
}

func (c *cemetery) InjectFuncParameters(fn any, injectBefore func(pt reflect.Type, i int) any, injectAfter func(pt reflect.Type, i int, obj any)) (args []any, err error) {
ft := reflect.TypeOf(fn)
if ft.Kind() != reflect.Func {
return nil, NewInnerError("fn must be a function", NotCompatible)
}

in := ft.NumIn()

getOnlyOne := func(pt reflect.Type, i int) Goner {
tombs := c.GetTomByType(pt)
if len(tombs) > 0 {
var container Tomb

for _, t := range tombs {
if t.IsDefault() {
container = t
break
}
}
if container == nil {
container = tombs[0]
if len(tombs) > 1 {
c.Warnf(fmt.Sprintf("injected function %s %d parameter more than one goner was found and no default, used the first!", ft.Name(), i))
}
}
return container.GetGoner()
}
return nil
}

for i := 0; i < in; i++ {
pt := ft.In(0)
x := injectBefore(pt, i)
if x != nil {
args = append(args, x)
continue
}

x = getOnlyOne(pt, i+1)
if x != nil {
args = append(args, x)
continue
}

if pt.Kind() != reflect.Struct {
err = NewInnerError(fmt.Sprintf("injected function %s %d parameter must be a struct", ft.Name(), i), NotCompatible)
return
}

parameter := reflect.New(pt)
goner := parameter.Interface()
_, err = c.ReviveOne(goner)
if err != nil {
return
}
x = parameter.Elem().Interface()

args = append(args, x)
injectAfter(pt, i, x)
}
return
}
4 changes: 3 additions & 1 deletion example/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomodule/redigo v1.8.9 // indirect
Expand Down Expand Up @@ -58,6 +58,8 @@ require (
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
Expand Down
15 changes: 12 additions & 3 deletions example/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ type controller struct {

// Mount use for mounting the router of gin framework
func (ctr *controller) Mount() gin.MountError {
ctr.router.GET("/ping", func(c *gin.Context) (any, error) {
return "hello", nil
})
//ctr.router.GET("/ping", func(c *gin.Context) (any, error) {
// return "hello", nil
//})
ctr.router.GET("/hello", ctr.hello)
return nil
}

func (ctr *controller) hello(in struct {
name string `gone:"http,query"`
}) (any, error) {
defer gone.TimeStat("hello")()

return "hello, " + in.name, nil
}

func NewController() gone.Goner {
return &controller{}
}
Expand Down
Loading

0 comments on commit 2b3323f

Please sign in to comment.