-
Notifications
You must be signed in to change notification settings - Fork 0
/
definition.go
108 lines (85 loc) · 2.14 KB
/
definition.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
// Copyright 2022 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package di
import (
"reflect"
"github.com/gozix/di/internal/compiler"
"github.com/gozix/di/internal/runtime"
)
type (
// definition is container item representation.
definition struct {
id int
aliases []any
compiler compiler.Compiler
constraints constraints
frame runtime.Frame
tags Tags
unshared bool
definitions definitions
}
// definitions are list of definitions.
definitions map[reflect.Type][]definition
)
// definition implements the Definition interface.
var _ Definition = (*definition)(nil)
func (d *definition) Dependencies() []Dependency {
var deps []Dependency
for _, dep := range d.compiler.Dependencies() {
var (
constr = d.constraints.choose(dep.Index, dep.Name, dep.Type)
defs = make([]Definition, 0, 2)
)
for _, def := range d.definitions.find(dep.Type, constr.modifiers) {
def.definitions = d.definitions
defs = append(defs, Definition(&def))
}
deps = append(deps, Dependency{
Type: dep.Type,
Optional: constr.optional,
Definitions: defs,
})
}
return deps
}
func (d *definition) ID() int {
return d.id
}
func (d *definition) Type() reflect.Type {
return d.compiler.Type()
}
func (d *definition) Tags() Tags {
var tags = make(Tags, len(d.tags))
copy(tags, d.tags)
return tags
}
func (d *definition) Unshared() bool {
return d.unshared
}
func (d *definition) applyAddOptions(options ...AddOption) {
for _, o := range options {
o.applyAddOption(d)
}
}
func (d *definition) applyProvideOptions(options ...ProvideOption) {
for _, o := range options {
o.applyProvideOption(d)
}
}
func (d definitions) find(typ reflect.Type, modifiers []Modifier) (founded []definition) {
var defs = make([]Definition, 0, 4)
for i := range d[typ] {
defs = append(defs, &d[typ][i])
}
for _, mod := range modifiers {
defs = mod(defs)
}
for _, def := range defs {
if def == nil {
continue
}
founded = append(founded, *def.(*definition))
}
return founded
}