@@ -4,98 +4,8 @@ import (
4
4
"fmt"
5
5
"net/http"
6
6
"reflect"
7
-
8
- "github.com/nicksnyder/go-i18n/v2/i18n"
9
-
10
- "github.com/iota-uz/iota-sdk/modules/core/domain/aggregates/user"
11
- "github.com/iota-uz/iota-sdk/pkg/application"
12
- "github.com/iota-uz/iota-sdk/pkg/composables"
13
- "github.com/iota-uz/iota-sdk/pkg/types"
14
7
)
15
8
16
- var builtinProviders = []Provider {
17
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
18
- pageCtxType := reflect .TypeOf ((* types .PageContext )(nil ))
19
- if t == pageCtxType {
20
- return reflect .ValueOf (composables .UsePageCtx (r .Context ())), true , nil
21
- }
22
- return reflect.Value {}, false , nil
23
- },
24
-
25
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
26
- writerType := reflect .TypeOf ((* http .ResponseWriter )(nil )).Elem ()
27
- if t .Implements (writerType ) {
28
- return reflect .ValueOf (w ), true , nil
29
- }
30
- return reflect.Value {}, false , nil
31
- },
32
-
33
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
34
- requestType := reflect .TypeOf ((* http .Request )(nil ))
35
- if t == requestType {
36
- return reflect .ValueOf (r ), true , nil
37
- }
38
- return reflect.Value {}, false , nil
39
- },
40
-
41
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
42
- localizerType := reflect .TypeOf ((* i18n .Localizer )(nil ))
43
- if t == localizerType {
44
- localizer , ok := composables .UseLocalizer (r .Context ())
45
- if ! ok {
46
- return reflect.Value {}, true , fmt .Errorf ("localizer not found in request context" )
47
- }
48
- return reflect .ValueOf (localizer ), true , nil
49
- }
50
- return reflect.Value {}, false , nil
51
- },
52
-
53
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
54
- userType := reflect .TypeOf ((* user .User )(nil )).Elem ()
55
- if t .Implements (userType ) {
56
- u , err := composables .UseUser (r .Context ())
57
- if err != nil {
58
- return reflect.Value {}, true , fmt .Errorf ("user not found in request context" )
59
- }
60
- return reflect .ValueOf (u ), true , nil
61
- }
62
- return reflect.Value {}, false , nil
63
- },
64
-
65
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
66
- appType := reflect .TypeOf ((* application .Application )(nil )).Elem ()
67
- if t .Implements (appType ) {
68
- app , err := composables .UseApp (r .Context ())
69
- if err != nil {
70
- return reflect.Value {}, true , err
71
- }
72
- return reflect .ValueOf (app ), true , nil
73
- }
74
- return reflect.Value {}, false , nil
75
- },
76
-
77
- func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error ) {
78
- app , err := composables .UseApp (r .Context ())
79
- if err != nil {
80
- return reflect.Value {}, false , err
81
- }
82
-
83
- services := app .Services ()
84
- if service , exists := services [t .Elem ()]; exists {
85
- return reflect .ValueOf (service ), true , nil
86
- }
87
-
88
- return reflect.Value {}, false , nil
89
- },
90
- }
91
-
92
- // Provider is a function that can provide a value for a given type
93
- // It returns:
94
- // - The value (if it can provide it)
95
- // - A boolean indicating whether this provider can handle the requested type
96
- // - Any error that occurred during resolution
97
- type Provider func (t reflect.Type , w http.ResponseWriter , r * http.Request ) (reflect.Value , bool , error )
98
-
99
9
func NewDIHandler (handler interface {}, customProviders ... Provider ) * DIHandler {
100
10
return & DIHandler {
101
11
value : reflect .ValueOf (handler ),
@@ -113,39 +23,44 @@ func (d *DIHandler) Handler() http.HandlerFunc {
113
23
typeOf := d .value .Type ()
114
24
numArgs := typeOf .NumIn ()
115
25
116
- // Precompute argument types and their resolvers
117
26
argTypes := make ([]reflect.Type , numArgs )
118
27
for i := 0 ; i < numArgs ; i ++ {
119
28
argTypes [i ] = typeOf .In (i )
120
29
}
121
30
122
31
// All providers to try in order (custom first, then built-in)
123
- allProviders := append (d .customProviders , builtinProviders ... )
32
+ allProviders := append (d .customProviders , BuiltinProviders ()... )
33
+
34
+ matchedProviders := make ([]Provider , numArgs )
35
+ for i , argType := range argTypes {
36
+ for _ , provider := range allProviders {
37
+ if provider .Ok (argType ) {
38
+ matchedProviders [i ] = provider
39
+ break
40
+ }
41
+ }
42
+
43
+ if matchedProviders [i ] == nil {
44
+ // Return a handler that will return an error for this specific type
45
+ errorMsg := fmt .Sprintf ("No provider found for type: %v" , argType )
46
+ return func (w http.ResponseWriter , r * http.Request ) {
47
+ http .Error (w , errorMsg , http .StatusInternalServerError )
48
+ }
49
+ }
50
+ }
124
51
125
52
return func (w http.ResponseWriter , r * http.Request ) {
126
53
args := make ([]reflect.Value , numArgs )
127
54
128
- // Resolve each argument
55
+ // Resolve each argument using precomputed matched providers
129
56
for i , argType := range argTypes {
130
- var resolved bool
131
- var err error
132
-
133
- // Try each provider in order
134
- for _ , provider := range allProviders {
135
- args [i ], resolved , err = provider (argType , w , r )
136
- if err != nil {
137
- http .Error (w , err .Error (), http .StatusInternalServerError )
138
- return
139
- }
140
- if resolved {
141
- break
142
- }
143
- }
144
-
145
- if ! resolved {
146
- http .Error (w , fmt .Sprintf ("No provider found for type: %v" , argType ), http .StatusInternalServerError )
57
+ provider := matchedProviders [i ]
58
+ value , err := provider .Provide (argType , w , r )
59
+ if err != nil {
60
+ http .Error (w , err .Error (), http .StatusInternalServerError )
147
61
return
148
62
}
63
+ args [i ] = value
149
64
}
150
65
151
66
d .value .Call (args )
0 commit comments