-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
168 lines (144 loc) · 4.12 KB
/
container.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2024 Outreach Corporation. All Rights Reserved.
// Description: This file contains root dependency container
package plumber
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"github.com/samber/lo"
)
// Container represents a root dependency container
type Container struct {
cleanup []func(context.Context) error
}
// Cleanup registers a cleanup function
func (c *Container) Cleanup(fn func(context.Context)) {
c.cleanup = append(c.cleanup, func(ctx context.Context) error {
fn(ctx)
return nil
})
}
// CleanupError registers a cleanup function returning an error
func (c *Container) CleanupError(fn func(context.Context) error) {
c.cleanup = append(c.cleanup, fn)
}
// Close calls all cleanup functions
func (c *Container) Close(ctx context.Context) error {
errs := []error{}
for _, cleanup := range c.cleanup {
if err := cleanup(ctx); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// DefineContainers defines supplied containers using given config and root container
func DefineContainers[C, CF any](ctx context.Context, cfg CF, definers []func(context.Context, CF, C), root C, containers ...interface {
Define(context.Context, CF, C)
}) C {
for _, d := range definers {
d(ctx, cfg, root)
}
for _, d := range containers {
d.Define(ctx, cfg, root)
}
return root
}
// ContainerResolved checks if the container can be resolved.
// It checks as well each instance in the container separately to ensure that all required dependencies are resolved
// and are actually used with resolution function.
func ContainerResolved[C any](containerFunc func() C) error {
root := containerFunc()
return containerDependecyResolved(root, containerFunc, []string{})
}
// containerDependecyResolved checks if the particular level in container graph can be resolved.
func containerDependecyResolved[C any](root any, containerFunc func() C, path []string) error {
errs := []error{}
v := reflect.ValueOf(root)
v = reflect.Indirect(v)
t := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if !field.CanInterface() {
continue
}
var (
name = t.Field(i).Name
actualPath = append(append([]string{}, path...), name)
)
tag := t.Field(i).Tag.Get("plumber")
if tag != "" {
parts := strings.Split(tag, ",")
if len(parts) > 1 {
if parts[1] == "ignore" {
continue
}
}
}
it := field.Interface()
if _, ok := it.(dependencyErrorer); ok {
if err := evaluateDependencyByPath(containerFunc, actualPath); err != nil {
errs = append(errs, err)
continue
}
} else {
ptr := reflect.New(field.Type())
ptr.Elem().Set(field)
it := ptr.Interface()
if _, ok := it.(dependencyErrorer); ok {
if err := evaluateDependencyByPath(containerFunc, actualPath); err != nil {
errs = append(errs, err)
continue
}
}
}
field = reflect.Indirect(field)
if field.Kind() == reflect.Struct {
if err := containerDependecyResolved(field.Interface(), containerFunc, actualPath); err != nil {
errs = append(errs, err)
continue
}
}
}
return errors.Join(errs...)
}
// evaluateDependencyByPath evaluates dependency by path
func evaluateDependencyByPath[C any](containerFunc func() C, path []string) error {
errs := []error{}
dep := ReflectValueByPath(containerFunc, path)
var errorer dependencyErrorer
if errr, ok := dep.Interface().(dependencyErrorer); ok {
errorer = errr
} else {
vl := dep
if vl.CanAddr() {
vl = vl.Addr()
dp := vl.Interface()
if errr, ok := dp.(dependencyErrorer); ok {
errorer = errr
}
}
}
if errorer != nil {
path := strings.Join(path, ".")
errs = append(errs, lo.Map(errorer.dependencyErrors(), func(e error, _ int) error {
return fmt.Errorf("errors on \"%s\": %w", path, e)
})...)
}
return errors.Join(errs...)
}
// ReflectValueByPath returns dependency by path
func ReflectValueByPath[C any](containerFunc func() C, path []string) reflect.Value {
elem := containerFunc()
if len(path) == 0 {
return reflect.ValueOf(elem)
}
field := reflect.ValueOf(elem)
for _, name := range path {
field = reflect.Indirect(field)
field = field.FieldByName(name)
}
return field
}