-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust.go
70 lines (61 loc) · 2.09 KB
/
must.go
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
package dino
// must panics with the passed error, if it is not nil.
func must(err error) {
if err != nil {
panic(err)
}
}
// MustAdd registers a service of type T as a singleton in the provided container.
//
// If the operation fails, this method will panic.
func MustAdd[T any, TImpl any](c *Container) {
must(Add[T, TImpl](c))
}
// MustAddNamed registers a service of type T as a singleton in the provided container.
//
// If the operation fails, this method will panic.
func MustAddNamed[T any, TImpl any](c *Container, name string) {
must(AddNamed[T, TImpl](c, name))
}
// MustAddTransient registers a service of type T as a transient in the provided container.
//
// If the operation fails, this method will panic.
func MustAddTransient[T any, TImpl any](c *Container) {
must(AddTransient[T, TImpl](c))
}
// MustAddTransientNamed registers a service of type T as a transient in the provided container.
//
// If the operation fails, this method will panic.
func MustAddTransientNamed[T any, TImpl any](c *Container, name string) {
must(AddTransientNamed[T, TImpl](c, name))
}
// AddInstance registers an object of type TImpl as a service of type T
// in the container under a global namespace.
//
// If the operation fails, this method will panic.
func MustAddInstance[T any, TImpl any](c *Container, instance TImpl) {
must(AddInstance[T](c, instance))
}
// AddInstanceNamed registers an object of type TImpl as a service of type T
// in the container under a provided namespace.
//
// If the operation fails, this method will panic.
func MustAddInstanceNamed[T any, TImpl any](c *Container, name string, instance TImpl) {
must(AddInstanceNamed[T](c, name, instance))
}
// MustGet tries to create, retrieve or inject an object of type T.
//
// If the operation fails, this method will panic.
func MustGet[T any](c *Container) T {
svc, err := Get[T](c)
must(err)
return svc
}
// MustGetNamed tries to create, retrieve or inject an object of type T.
//
// If the operation fails, this method will panic.
func MustGetNamed[T any](c *Container, name string) T {
svc, err := GetNamed[T](c, name)
must(err)
return svc
}