Skip to content

Commit

Permalink
add docs, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed May 29, 2021
1 parent f0c8283 commit d30a7e1
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions core/module/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ type node struct {
// its security policy. Access to dependencies provided by this provider can optionally
// be restricted to certain scopes based on SecurityCheckers.
type Provider struct {
Constructor func(deps []reflect.Value) ([]reflect.Value, error)
Needs []Key
Provides []Key
Scope Scope
// Constructor provides the dependencies
Constructor func(deps []reflect.Value) ([]reflect.Value, error)

// Needs are the keys for dependencies the constructor needs
Needs []Key

// Needs are the keys for dependencies the constructor provides
Provides []Key

// Scope is the scope within which the constructor runs
Scope Scope

// SecurityCheckers are optional security checker functions for the dependencies provided
// by the constructor.
SecurityCheckers []SecurityChecker
}

Expand All @@ -69,10 +79,18 @@ type scopeNode struct {
// can provide a dependency for any valid scope passed to it, although it can return an error
// to deny access.
type ScopedProvider struct {

// Constructor provides dependencies for the provided scope
Constructor func(scope Scope, deps []reflect.Value) ([]reflect.Value, error)
Needs []Key
Provides []Key
Scope Scope

// Needs are the keys for dependencies the constructor needs
Needs []Key

// Needs are the keys for dependencies the constructor provides
Provides []Key

// Scope is the scope within which the constructor runs
Scope Scope
}

type secureValue struct {
Expand Down Expand Up @@ -180,14 +198,12 @@ func (c *Container) resolve(scope Scope, key Key, stack map[interface{}]bool) (r
}
}

if val, ok := c.values[key]; ok {
if val.securityChecker != nil {
if err := val.securityChecker(scope); err != nil {
return reflect.Value{}, err
}
if val, ok, err := c.getValue(scope, key); ok {
if err != nil {
return reflect.Value{}, err
}

return val.value, nil
return val, nil
}

if provider, ok := c.providers[key]; ok {
Expand All @@ -204,18 +220,12 @@ func (c *Container) resolve(scope Scope, key Key, stack map[interface{}]bool) (r
return reflect.Value{}, err
}

val, ok := c.values[key]
val, ok, err := c.getValue(scope, key)
if !ok {
return reflect.Value{}, fmt.Errorf("internal error: bug")
}

if val.securityChecker != nil {
if err := val.securityChecker(scope); err != nil {
return reflect.Value{}, err
}
}

return val.value, nil
return val, err
}

return reflect.Value{}, fmt.Errorf("no provider")
Expand Down Expand Up @@ -268,6 +278,20 @@ func (c *Container) execNode(provider *node, stack map[interface{}]bool) error {
return nil
}

func (c *Container) getValue(scope Scope, key Key) (reflect.Value, bool, error) {
if val, ok := c.values[key]; ok {
if val.securityChecker != nil {
if err := val.securityChecker(scope); err != nil {
return reflect.Value{}, true, err
}
}

return val.value, true, nil
}

return reflect.Value{}, false, nil
}

func (c *Container) Resolve(scope Scope, key Key) (reflect.Value, error) {
val, err := c.resolve(scope, key, map[interface{}]bool{})
if err != nil {
Expand Down

0 comments on commit d30a7e1

Please sign in to comment.