Skip to content

Commit

Permalink
Update package doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Jul 22, 2024
1 parent 95497e6 commit 41ad1b0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
5 changes: 1 addition & 4 deletions module.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Package module provides a way to do dependency injection, with type-safe, without performance penalty.
// See examples for the basic usage.
// See [examples_test.go](./examples_test.go) for the basic usage.
package module

import (
"context"
"reflect"
)

// BuildFunc is the constructor of an Instance.
type BuildFunc[T any] func(context.Context) (T, error)

type moduleKey string

// Module provides a module to inject and retreive an instance with its type.
Expand Down
7 changes: 5 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ type Provider interface {
value(ctx context.Context) (any, error)
}

// BuildFunc is the constructor of an Instance.
type BuildFunc[T any] func(context.Context) (T, error)

type funcProvider[T any] struct {
moduleKey moduleKey
ctor BuildFunc[T]
}

// ProvideWithFunc returns a provider which provides instances creating from `ctor` function.
func (m *Module[T]) ProvideWithFunc(ctor BuildFunc[T]) Provider {
func (m Module[T]) ProvideWithFunc(ctor BuildFunc[T]) Provider {
return &funcProvider[T]{
moduleKey: m.moduleKey,
ctor: ctor,
}
}

// ProvideValue returns a provider which always provides given `value` as instances.
func (m *Module[T]) ProvideValue(value T) Provider {
func (m Module[T]) ProvideValue(value T) Provider {
return m.ProvideWithFunc(func(context.Context) (T, error) {
return value, nil
})
Expand Down

0 comments on commit 41ad1b0

Please sign in to comment.