Skip to content

Commit

Permalink
Merge pull request #16 from vlorc/develop
Browse files Browse the repository at this point in the history
HavingFile support
  • Loading branch information
vlorc committed Feb 20, 2021
2 parents 948527e + 06b1d35 commit bcfaea8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
6 changes: 5 additions & 1 deletion examples/module/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
)

var TLSModule = NewModuleFactory(
Condition(And(HavingBean(types.StringType, "certFile"), HavingBean(types.StringType, "keyFile")),
Condition(
And(
HavingFile(types.StringType, "certFile"),
HavingFile(types.StringType, "keyFile"),
),
Declare(
Method(func(param struct {
config *tls.Config `inject:"'default' optional"`
Expand Down
26 changes: 22 additions & 4 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,28 @@ func NewSliceFactory(typ reflect.Type, name ...types.StringFactory) types.BeanFa
return &resolveAnyFactory{typ: typ}
}

f := &resolveNamesFactory{typ: typ}
f.name = make([]types.StringFactory, len(name))
copy(f.name, name)
return f
dump := make([]types.StringFactory, len(name))
copy(dump, name)

if types.BeanFactoryType == typ {
return &resolveGeneralFactory{
typ: typ,
append: func(value reflect.Value, factory types.GeneralFactory) reflect.Value {
return reflect.Append(value, reflect.ValueOf(factory.(types.BeanFactory)))
},
}
}

if types.BeanFactoryType == typ {
return &resolveGeneralFactory{
typ: typ,
append: func(value reflect.Value, factory types.GeneralFactory) reflect.Value {
return reflect.Append(value, reflect.ValueOf(factory))
},
}
}

return &resolveNamesFactory{typ: typ, name: dump}
}

func NewLazyFactory(typ reflect.Type, factory types.BeanFactory) types.BeanFactory {
Expand Down
17 changes: 17 additions & 0 deletions factory/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ func (f *resolveNamesFactory) Instance(provider types.Provider) (interface{}, er

return val.Interface(), nil
}

func (f *resolveGeneralFactory) Instance(provider types.Provider) (interface{}, error) {
val := reflect.MakeSlice(reflect.SliceOf(f.typ), 0, len(f.name))

for _, b := range f.name {
id, err := b.Instance(provider)
if nil != err {
// add check error
continue
}
if v := provider.Factory(nil, id, -1); nil != v {
val = f.append(val, v)
}
}

return val.Interface(), nil
}
6 changes: 6 additions & 0 deletions factory/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@ type makeChanFactory struct {
typ reflect.Type
length int
}

type resolveGeneralFactory struct {
typ reflect.Type
name []types.StringFactory
append func(reflect.Value, types.GeneralFactory) reflect.Value
}
30 changes: 25 additions & 5 deletions module/operation/condition.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package operation

import (
"fmt"
"github.com/vlorc/gioc/module"
"github.com/vlorc/gioc/types"
"github.com/vlorc/gioc/utils"
"os"
"reflect"
"strings"
)
Expand All @@ -27,8 +29,8 @@ func havingValue(eq func(types.BeanFactory, types.Provider) bool, typ reflect.Ty
return false
}
for _, v := range names {
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "") {
if str, err := types.NewNameFactory(v).Instance(ctx.Container().AsProvider()); nil != err {
if strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}") {
if str, err := types.NewNameFactory(v[2 : len(v)-1]).Instance(ctx.Container().AsProvider()); nil != err {
// add check error
continue
} else {
Expand All @@ -45,15 +47,17 @@ func havingValue(eq func(types.BeanFactory, types.Provider) bool, typ reflect.Ty
}

func HavingBean(impType interface{}, names ...string) module.ModuleCondHandle {
return havingValue(func(factory types.BeanFactory, provider types.Provider) bool {
return nil != factory
}, utils.TypeOf(impType), names...)
return havingValue(havingBean, utils.TypeOf(impType), names...)
}

func HavingValue(eq func(types.BeanFactory, types.Provider) bool, impType interface{}, names ...string) module.ModuleCondHandle {
return havingValue(eq, utils.TypeOf(impType), names...)
}

func HavingFile(impType interface{}, names ...string) module.ModuleCondHandle {
return havingValue(havingFile, utils.TypeOf(impType), names...)
}

func Not(cond ...module.ModuleCondHandle) module.ModuleCondHandle {
return func(c *module.ModuleInitContext) bool {
for _, v := range cond {
Expand Down Expand Up @@ -96,3 +100,19 @@ func Equal(val interface{}) func(types.BeanFactory, types.Provider) bool {
return reflect.DeepEqual(val, instance)
}
}

func havingBean(factory types.BeanFactory, provider types.Provider) bool {
return nil != factory
}

func havingFile(factory types.BeanFactory, provider types.Provider) bool {
instance, err := factory.Instance(provider)
if nil != err {
return false
}

file := fmt.Sprint(instance)
_, err = os.Stat(file)

return nil == err || !os.IsNotExist(err)
}
2 changes: 1 addition & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *coreProvider) load(receive interface{}, typ reflect.Type, name string)
return
}

func (p *coreProvider) Factory(typ reflect.Type, name string, deep int) types.BeanFactory {
func (p *coreProvider) Factory(typ reflect.Type, name string, deep int) types.GeneralFactory {
if factory := p.selector.Get(typ, name); nil != factory {
return factory
}
Expand Down
4 changes: 3 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Provider interface {

Load(receive interface{}, name ...string) error

Factory(typ reflect.Type, name string, deep int) BeanFactory
Factory(typ reflect.Type, name string, deep int) GeneralFactory

Range(callback func(GeneralFactory) bool, types ...reflect.Type) bool

Expand Down Expand Up @@ -230,3 +230,5 @@ var Type = reflect.TypeOf((*reflect.Type)(nil)).Elem()
var StringType = reflect.TypeOf((*string)(nil)).Elem()
var ErrorType = reflect.TypeOf((*error)(nil)).Elem()
var ProviderType = reflect.TypeOf((*Provider)(nil)).Elem()
var BeanFactoryType = reflect.TypeOf((*BeanFactory)(nil)).Elem()
var GeneralFactoryType = reflect.TypeOf((*GeneralFactory)(nil)).Elem()

0 comments on commit bcfaea8

Please sign in to comment.