Skip to content

Commit

Permalink
Merge pull request #74 from osspkg/fix-di
Browse files Browse the repository at this point in the history
Fix di
  • Loading branch information
markus621 authored Jan 4, 2024
2 parents d6620c6 + e02a189 commit 0aac0ef
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 95 deletions.
77 changes: 30 additions & 47 deletions app/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ func (v *container) Invoke(obj interface{}) error {
if v.srv.IsOff() {
return errDepNotRunning
}
item, err := v.toStoreItem(obj)
if err != nil {
return err
}
_, err = v.callArgs(item)
item, _, err := v.callArgs(obj)
if err != nil {
return err
}
Expand All @@ -153,29 +149,16 @@ func (v *container) toStoreItem(obj interface{}) (*objectStorageItem, error) {
return item, nil
}
ref := reflect.TypeOf(obj)
address, ok := getReflectAddress(ref, obj)
if !ok {
return nil, fmt.Errorf("dependency [%s] is not supported", address)
}
serviceStatus := itNotService
if isService(obj) {
serviceStatus = itDownService
}
item = &objectStorageItem{
Address: address,
RelationType: 0,
ReflectType: ref,
Kind: ref.Kind(),
Value: obj,
Service: serviceStatus,
if err := v.store.Add(ref, obj, asTypeNew); err != nil {
return nil, err
}
return item, nil
return v.store.GetByReflect(ref, obj)
}

func (v *container) callArgs(obj interface{}) ([]reflect.Value, error) {
func (v *container) callArgs(obj interface{}) (*objectStorageItem, []reflect.Value, error) {
item, err := v.toStoreItem(obj)
if err != nil {
return nil, err
return nil, nil, err
}

switch item.Kind {
Expand All @@ -186,21 +169,21 @@ func (v *container) callArgs(obj interface{}) ([]reflect.Value, error) {
inRefType := item.ReflectType.In(i)
inAddress, ok := getReflectAddress(inRefType, item.Value)
if !ok {
return nil, fmt.Errorf("dependency [%s] is not supported", inAddress)
return nil, nil, fmt.Errorf("dependency [%s] is not supported", inAddress)
}
dep, err := v.store.Get(inAddress)
dep, err := v.store.GetByAddress(inAddress)
if err != nil {
return nil, err
return nil, nil, err
}
args = append(args, reflect.ValueOf(dep.Value))
}
args = reflect.ValueOf(item.Value).Call(args)
for _, arg := range args {
if err, ok := arg.Interface().(error); ok && err != nil {
return nil, err
return nil, nil, err
}
}
return args, nil
return item, args, nil

case reflect.Struct:
value := reflect.New(item.ReflectType)
Expand All @@ -209,20 +192,20 @@ func (v *container) callArgs(obj interface{}) ([]reflect.Value, error) {
inRefType := item.ReflectType.Field(i)
inAddress, ok := getReflectAddress(inRefType.Type, nil)
if !ok {
return nil, fmt.Errorf("dependency [%s] is not supported", inAddress)
return nil, nil, fmt.Errorf("dependency [%s] is not supported", inAddress)
}
dep, err := v.store.Get(inAddress)
dep, err := v.store.GetByAddress(inAddress)
if err != nil {
return nil, err
return nil, nil, err
}
value.Elem().FieldByName(inRefType.Name).Set(reflect.ValueOf(dep.Value))
}
return append(args, value.Elem()), nil
return item, append(args, value.Elem()), nil

default:
}

return []reflect.Value{reflect.ValueOf(item.Value)}, nil
return item, []reflect.Value{reflect.ValueOf(item.Value)}, nil
}

// nolint: gocyclo
Expand All @@ -239,7 +222,7 @@ func (v *container) run() error {
if name == root {
continue
}
item, err := v.store.Get(name)
item, err := v.store.GetByAddress(name)
if err != nil {
return err
}
Expand All @@ -253,29 +236,29 @@ func (v *container) run() error {
delete(names, name)
continue
}
args, err := v.callArgs(item)
_, args, err := v.callArgs(item)
if err != nil {
return errors.Wrapf(err, "initialize error [%s]", name)
}
delete(names, name)
for _, arg := range args {
address, ok := getReflectAddress(arg.Type(), arg.Interface())
if !ok {
if address == "error" {
if err = v.store.Add(arg.Type(), arg.Interface(), asTypeExist); err != nil {
return errors.Wrapf(err, "initialize error")
}
if item, err = v.store.GetByReflect(arg.Type(), arg.Interface()); err != nil {
if errors.Is(err, errIsTypeError) {
continue
}
return fmt.Errorf("dependency [%s] is not supported form [%s]", address, item.Address)
return errors.Wrapf(err, "initialize error")
}
if isService(arg.Interface()) {
if err = v.srv.AddAndUp(arg.Interface()); err != nil {
return errors.Wrapf(err, "service initialization error [%s]", address)
delete(names, item.Address)
if item.Service == itDownService {
if err = v.srv.AddAndUp(item.Value); err != nil {
return errors.Wrapf(err, "service initialization error [%s]", item.Address)
}
}
delete(names, address)
if err = v.store.Add(arg.Type(), arg.Interface(), asTypeExist); err != nil {
return errors.Wrapf(err, "initialize error [%s]", address)
item.Service = itUpedService
}
}
delete(names, name)
}

v.srv.IterateOver()
Expand Down
18 changes: 9 additions & 9 deletions app/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ func TestUnit_DI_Default(t *testing.T) {
wantErrString: "is up err",
},
{
name: "Case15",
name: "Case16",
register: []interface{}{
&SimpleDI1_Service{ErrDown: "is down err"},
},
wantErr: true,
wantErrString: "is down err",
},
{
name: "Case16",
name: "Case17",
register: []interface{}{
func() *SimpleDI1_Service {
return &SimpleDI1_Service{ErrUp: "is up err"}
Expand All @@ -273,27 +273,27 @@ func TestUnit_DI_Default(t *testing.T) {
wantErrString: "is up err",
},
{
name: "Case17",
name: "Case18",
register: []interface{}{
func() (int, error) {
return 0, nil
},
},
wantErr: true,
wantErrString: "dependency [int] is not supported form [",
wantErrString: "dependency [int] is not supported",
},
{
name: "Case17",
name: "Case19",
register: []interface{}{
func() (error, int) {
return nil, 0
},
},
wantErr: true,
wantErrString: "dependency [int] is not supported form [",
wantErrString: "dependency [int] is not supported",
},
{
name: "Case18",
name: "Case20",
register: []interface{}{},
invoke: func() (int, error) {
return 0, nil
Expand All @@ -302,7 +302,7 @@ func TestUnit_DI_Default(t *testing.T) {
wantErrString: "",
},
{
name: "Case19",
name: "Case21",
register: []interface{}{},
invoke: func(_ error) int {
return 0
Expand All @@ -311,7 +311,7 @@ func TestUnit_DI_Default(t *testing.T) {
wantErrString: "dependency [error] is not supported",
},
{
name: "Case20",
name: "Case22",
register: []interface{}{
&SimpleDI1_ServiceEmpty{},
&SimpleDI1_ServiceEmptyXC{},
Expand Down
1 change: 1 addition & 0 deletions app/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
errDepAlreadyRunned = errors.New("dependencies are already running")
errDepNotRunning = errors.New("dependencies are not running yet")
errServiceUnknown = errors.New("unknown service")
errIsTypeError = errors.New("ERROR")
)
6 changes: 3 additions & 3 deletions app/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ type (
)

func isService(v interface{}) bool {
if _, ok := v.(ServiceInterface); ok {
return true
}
if _, ok := v.(ServiceContextInterface); ok {
return true
}
if _, ok := v.(ServiceXContextInterface); ok {
return true
}
if _, ok := v.(ServiceInterface); ok {
return true
}
return false
}

Expand Down
16 changes: 15 additions & 1 deletion app/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newObjectStorage() *objectStorage {
}
}

func (v *objectStorage) Get(address string) (*objectStorageItem, error) {
func (v *objectStorage) GetByAddress(address string) (*objectStorageItem, error) {
v.mux.RLock()
defer v.mux.RUnlock()

Expand All @@ -58,6 +58,20 @@ func (v *objectStorage) Get(address string) (*objectStorageItem, error) {
return nil, fmt.Errorf("dependency [%s] not initiated", address)
}

func (v *objectStorage) GetByReflect(ref reflect.Type, obj interface{}) (*objectStorageItem, error) {
v.mux.RLock()
defer v.mux.RUnlock()

address, ok := getReflectAddress(ref, obj)
if !ok {
if address == "error" {
return nil, errIsTypeError
}
return nil, fmt.Errorf("dependency [%s] is not supported", address)
}
return v.GetByAddress(address)
}

func (v *objectStorage) Add(ref reflect.Type, obj interface{}, relationType objectRelationType) error {
v.mux.Lock()
defer v.mux.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion auth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
go.osspkg.com/goppy/ioutil v0.1.1
go.osspkg.com/goppy/plugins v0.1.1
go.osspkg.com/goppy/random v0.1.1
go.osspkg.com/goppy/web v0.1.8
go.osspkg.com/goppy/web v0.1.9
go.osspkg.com/goppy/xtest v0.1.3
golang.org/x/oauth2 v0.13.0
)
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ replace (

require (
go.osspkg.com/goppy v0.15.4
go.osspkg.com/goppy/app v0.1.7
go.osspkg.com/goppy/app v0.1.8
go.osspkg.com/goppy/auth v0.1.0
go.osspkg.com/goppy/console v0.1.0
go.osspkg.com/goppy/geoip v0.1.0
Expand Down
2 changes: 1 addition & 1 deletion geoip/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ replace (
require (
github.com/oschwald/geoip2-golang v1.9.0
go.osspkg.com/goppy/plugins v0.1.1
go.osspkg.com/goppy/web v0.1.8
go.osspkg.com/goppy/web v0.1.9
)

require (
Expand Down
5 changes: 2 additions & 3 deletions geoip/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ func (v *MaxMindConfig) Default() {
func WithMaxMindGeoIP() plugins.Plugin {
return plugins.Plugin{
Config: &MaxMindConfig{},
Inject: func(conf *MaxMindConfig) (*maxmind, GeoIP) {
mmdb := newMMDB(conf)
return mmdb, mmdb
Inject: func(conf *MaxMindConfig) GeoIP {
return newMMDB(conf)
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ replace (
)

require (
go.osspkg.com/goppy/app v0.1.8
go.osspkg.com/goppy/app v0.1.9
go.osspkg.com/goppy/console v0.1.0
go.osspkg.com/goppy/errors v0.1.0
go.osspkg.com/goppy/iofile v0.1.4
Expand Down
5 changes: 2 additions & 3 deletions ormmysql/plugin_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func (v *ConfigMysql) Default() {
func WithMySQL() plugins.Plugin {
return plugins.Plugin{
Config: &ConfigMysql{},
Inject: func(c *ConfigMysql, l xlog.Logger) (*mysqlProvider, MySQL) {
Inject: func(c *ConfigMysql, l xlog.Logger) MySQL {
conn := New(c)
o := orm.New(conn, orm.UsePluginLogger(l))
m := orm.NewMigrate(o, c.Migrate, l)
p := &mysqlProvider{
return &mysqlProvider{
conn: conn,
orm: o,
migrate: m,
Expand All @@ -86,7 +86,6 @@ func WithMySQL() plugins.Plugin {
list: make(map[string]orm.Stmt),
active: false,
}
return p, p
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions ormpgsql/plugin_pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (v *ConfigPgsql) Default() {
func WithPostgreSQL() plugins.Plugin {
return plugins.Plugin{
Config: &ConfigPgsql{},
Inject: func(c *ConfigPgsql, l xlog.Logger) (*pgsqlProvider, PgSQL) {
Inject: func(c *ConfigPgsql, l xlog.Logger) PgSQL {
conn := New(c)
o := orm.New(conn, orm.UsePluginLogger(l))
m := orm.NewMigrate(o, c.Migrate, l)
p := &pgsqlProvider{
return &pgsqlProvider{
conn: conn,
orm: o,
migrate: m,
Expand All @@ -82,7 +82,6 @@ func WithPostgreSQL() plugins.Plugin {
list: make(map[string]orm.Stmt),
active: false,
}
return p, p
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions ormsqlite/plugin_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func (v *ConfigSqlite) List() (list []sqlcommon.ItemInterface) {
func WithSQLite() plugins.Plugin {
return plugins.Plugin{
Config: &ConfigSqlite{},
Inject: func(c *ConfigSqlite, l xlog.Logger) (*sqliteProvider, SQLite) {
Inject: func(c *ConfigSqlite, l xlog.Logger) SQLite {
conn := New(c)
o := orm.New(conn, orm.UsePluginLogger(l))
m := orm.NewMigrate(o, c.Migrate, l)
p := &sqliteProvider{
return &sqliteProvider{
conn: conn,
orm: o,
migrate: m,
Expand All @@ -76,7 +76,6 @@ func WithSQLite() plugins.Plugin {
list: make(map[string]orm.Stmt),
active: false,
}
return p, p
},
}
}
Expand Down
Loading

0 comments on commit 0aac0ef

Please sign in to comment.