-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdix.go
More file actions
80 lines (61 loc) · 1.67 KB
/
dix.go
File metadata and controls
80 lines (61 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package dix
import (
"context"
_ "embed"
"log/slog"
"reflect"
"time"
"github.com/pubgo/dix/v2/dixinternal"
)
//go:embed .version/VERSION
var version string
func Version() string { return version }
type (
Option = dixinternal.Option
Options = dixinternal.Options
Dix = dixinternal.Dix
)
func SetLog(log slog.Handler) { dixinternal.SetLog(log) }
func WithValuesNull() Option { return dixinternal.WithValuesNull() }
func WithProviderTimeout(timeout time.Duration) Option {
return dixinternal.WithProviderTimeout(timeout)
}
func WithSlowProviderThreshold(threshold time.Duration) Option {
return dixinternal.WithSlowProviderThreshold(threshold)
}
func New(opts ...Option) *Dix { return dixinternal.New(opts...) }
func Inject[T any](di *Dix, data T, opts ...Option) T {
vp := reflect.ValueOf(data)
if vp.Kind() == reflect.Struct {
_ = di.Inject(&data, opts...)
} else {
_ = di.Inject(data, opts...)
}
return data
}
func InjectContext[T any](ctx context.Context, di *Dix, data T, opts ...Option) T {
vp := reflect.ValueOf(data)
if vp.Kind() == reflect.Struct {
_ = di.InjectContext(ctx, &data, opts...)
} else {
_ = di.InjectContext(ctx, data, opts...)
}
return data
}
func InjectT[T any](di *Dix, opts ...Option) T {
var data T
if reflect.TypeOf(data).Kind() != reflect.Struct {
panic("<T> type kind is not struct")
}
_ = di.Inject(&data, opts...)
return data
}
func InjectTContext[T any](ctx context.Context, di *Dix, opts ...Option) T {
var data T
if reflect.TypeOf(data).Kind() != reflect.Struct {
panic("<T> type kind is not struct")
}
_ = di.InjectContext(ctx, &data, opts...)
return data
}
func Provide(di *Dix, data any) { di.Provide(data) }