-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Patrin <[email protected]>
- Loading branch information
1 parent
8c12138
commit b1bbf95
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package gontainer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// Resolver defines service resolver interface. | ||
type Resolver interface { | ||
// Resolve returns specified dependency. | ||
Resolve(any) error | ||
} | ||
|
||
// resolver implements resolver interface. | ||
type resolver struct { | ||
ctx context.Context | ||
registry *registry | ||
} | ||
|
||
// Resolve returns specified dependency. | ||
func (r *resolver) Resolve(varPtr any) error { | ||
value := reflect.ValueOf(varPtr).Elem() | ||
ins := []reflect.Type{value.Type()} | ||
outs, err := r.registry.getFactoryInValues(r.ctx, ins) | ||
if err != nil { | ||
return fmt.Errorf("failed to get service: %s", err) | ||
} | ||
value.Set(outs[0]) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package gontainer | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestResolverService tests resolver service. | ||
func TestResolverService(t *testing.T) { | ||
container, err := New( | ||
NewFactory(func() string { return "string" }), | ||
NewFactory(func(resolver Resolver) { | ||
var depExists string | ||
equal(t, resolver.Resolve(&depExists), nil) | ||
equal(t, depExists, "string") | ||
|
||
var depNotExists int | ||
equal(t, resolver.Resolve(&depNotExists) != nil, true) | ||
equal(t, depNotExists, 0) | ||
}), | ||
) | ||
equal(t, err, nil) | ||
equal(t, container == nil, false) | ||
|
||
// Start all factories in the container. | ||
equal(t, container.Start(), nil) | ||
|
||
// Let async service function launch. | ||
time.Sleep(time.Millisecond) | ||
|
||
// Close all factories in the container. | ||
equal(t, container.Close(), nil) | ||
|
||
<-container.Done() | ||
} |