Skip to content

Commit

Permalink
refactor (#14)
Browse files Browse the repository at this point in the history
* fix

* fix

* fix

* fix: 2024-03-27 12:14:13

* fix: 2024-03-27 14:22:29

* fix: 2024-03-31 23:52:34

* fix: 2024-04-02 12:29:03

* fix: 2024-04-18 23:44:15
  • Loading branch information
kooksee authored Apr 18, 2024
1 parent 47f74f5 commit 1ccd536
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 169 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
> dix是一个依赖注入框架
> 它参考了dig的设计, 但是它能够完成更加复杂的依赖注入管理和namespace依赖隔离
> https://github.com/uber-go/dig

## 功能描述
Expand Down
10 changes: 9 additions & 1 deletion dix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dix

import (
"reflect"

"github.com/pubgo/dix/internal/dix_inter"
)

Expand All @@ -24,7 +26,13 @@ func New(opts ...Option) *Dix {
}

func Inject[T any](di *Dix, data T, opts ...Option) T {
_ = di.Inject(data, opts...)
vp := reflect.ValueOf(data)
if vp.Kind() == reflect.Struct {
_ = di.Inject(&data, opts...)
} else {
_ = di.Inject(data, opts...)
}

return data
}

Expand Down
11 changes: 4 additions & 7 deletions example/cycle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ func main() {
}()

type (
A struct {
}
A struct{}

B struct {
}
B struct{}

C struct {
}
C struct{}
)

di.Provide(func(*B) *A {
Expand All @@ -35,7 +32,7 @@ func main() {
return new(B)
})

var err = try.Try(func() error {
err := try.Try(func() error {
di.Provide(func(*A) *C {
return new(C)
})
Expand Down
3 changes: 2 additions & 1 deletion example/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {

di.Provide(func(p struct {
L *log.Logger
}) *Redis {
},
) *Redis {
p.L.Println("init redis")
return &Redis{name: "hello"}
})
Expand Down
6 changes: 3 additions & 3 deletions example/inject_method/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"github.com/pubgo/funk/errors"
)

type handler struct {
}
type handler struct{}

func (h *handler) DixInjectA(err *errors.Err) {
fmt.Println("A: ", err.Msg)
}

func (h *handler) DixInjectD(p struct {
Err *errors.Err
}) {
},
) {
fmt.Println("D: ", p.Err.Msg)
}

Expand Down
2 changes: 1 addition & 1 deletion example/struct-in/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"fmt"
"github.com/pubgo/funk/recovery"

"github.com/pubgo/dix"
"github.com/pubgo/dix/di"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/recovery"
)

type a struct {
Expand Down
13 changes: 8 additions & 5 deletions example/struct-out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/pubgo/dix/di"
"github.com/pubgo/funk/pretty"
"github.com/pubgo/funk/recovery"
Expand All @@ -15,10 +16,12 @@ type D struct {
M C
}

type C interface{}
type C1 struct {
Name string
}
type (
C interface{}
C1 struct {
Name string
}
)

type Conf struct {
Inline
Expand Down Expand Up @@ -68,7 +71,7 @@ func main() {
},
},
D4: map[string][]*D{
"default4": []*D{
"default4": {
{
M: "hello d4",
},
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module github.com/pubgo/dix

go 1.19

require github.com/pubgo/funk v0.5.32-3
require github.com/pubgo/funk v0.5.40

require (
github.com/alecthomas/repr v0.2.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/k0kubun/pp/v3 v3.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/phuslu/goid v1.0.0 // indirect
github.com/rs/zerolog v1.30.0 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
27 changes: 11 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
Expand All @@ -23,31 +21,28 @@ github.com/phuslu/goid v1.0.0 h1:Cgcvd/R54UO1fCtyt+iKXAi+yZQ/KWlAm6MmZNizCLM=
github.com/phuslu/goid v1.0.0/go.mod h1:txc2fUIdrdnn+v9Vq+QpiPQ3dnrXEchjoVDgic+r+L0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pubgo/funk v0.5.32-3 h1:91saMP3Eq9GQDzEwK3w3kN45ABeqO7zP1RrPtr+MHMg=
github.com/pubgo/funk v0.5.32-3/go.mod h1:Z5Wp7OoxjmSlWH+6waFSbao5MSD942LVhkBSlUu4s4s=
github.com/pubgo/funk v0.5.40 h1:uLN637YY/ljykp5R+bmBGl47wMzsUENW/ZvYG1APNGE=
github.com/pubgo/funk v0.5.40/go.mod h1:gKCw72+MK7xPiUGY1Z/bdGJMrSfVi87r0x/7d1GtKU4=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c h1:QgY/XxIAIeccR+Ca/rDdKubLIU9rcJ3xfy1DC/Wd2Oo=
google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c/go.mod h1:CGI5F/G+E5bKwmfYo09AXuVN4dD894kIKUFmVbP2/Fo=
google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U=
google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8 changes: 7 additions & 1 deletion internal/dix_inter/aaa.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dix_inter

import "reflect"
import (
"reflect"

"github.com/pubgo/funk/log"
)

const (
// defaultKey 默认的 namespace
Expand All @@ -20,3 +24,5 @@ type Graph struct {
Objects string `json:"objects"`
Providers string `json:"providers"`
}

var logger = log.GetLogger("dix")

Check failure on line 28 in internal/dix_inter/aaa.go

View workflow job for this annotation

GitHub Actions / lint

var `logger` is unused (unused)
8 changes: 5 additions & 3 deletions internal/dix_inter/cycle-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (

// isCycle Check whether type circular dependency
func (x *Dix) isCycle() (string, bool) {
var types = make(map[reflect.Type]map[reflect.Type]bool)
types := make(map[reflect.Type]map[reflect.Type]bool)
for _, nodes := range x.providers {
for _, n := range nodes {
if types[n.output.typ] == nil {
types[n.output.typ] = make(map[reflect.Type]bool)
}

for i := range n.input {
types[n.output.typ][n.input[i].typ] = true
for _, v := range x.getAllProvideInput(n.input[i].typ) {
types[n.output.typ][v.typ] = true
}
}
}
}
Expand All @@ -37,7 +39,7 @@ func (x *Dix) isCycle() (string, bool) {
return false
}

var nodes = list.New()
nodes := list.New()
for root := range types {
nodes.PushBack(root)
if check(root, types[root], nodes) {
Expand Down
Loading

0 comments on commit 1ccd536

Please sign in to comment.