@@ -5,15 +5,14 @@ mod loader;
5
5
6
6
use environment_definition:: { TargetEnvironment , TargetWorld , TriggerType } ;
7
7
pub use loader:: ResolutionContext ;
8
- use loader:: { component_source , ComponentSourceLoader , ComponentToValidate } ;
8
+ use loader:: { load_and_resolve_all , ComponentToValidate } ;
9
9
10
10
pub async fn validate_application_against_environment_ids (
11
11
env_ids : impl Iterator < Item = & str > ,
12
12
app : & spin_manifest:: schema:: v2:: AppManifest ,
13
13
resolution_context : & ResolutionContext ,
14
14
) -> anyhow:: Result < ( ) > {
15
- let envs = futures:: future:: join_all ( env_ids. map ( resolve_environment_id) ) . await ;
16
- let envs: Vec < _ > = envs. into_iter ( ) . collect :: < Result < _ , _ > > ( ) ?;
15
+ let envs = join_all_result ( env_ids. map ( resolve_environment_id) ) . await ?;
17
16
validate_application_against_environments ( & envs, app, resolution_context) . await
18
17
}
19
18
@@ -54,6 +53,8 @@ pub async fn validate_application_against_environments(
54
53
app : & spin_manifest:: schema:: v2:: AppManifest ,
55
54
resolution_context : & ResolutionContext ,
56
55
) -> anyhow:: Result < ( ) > {
56
+ use futures:: FutureExt ;
57
+
57
58
for trigger_type in app. triggers . keys ( ) {
58
59
if let Some ( env) = envs
59
60
. iter ( )
@@ -66,26 +67,15 @@ pub async fn validate_application_against_environments(
66
67
}
67
68
}
68
69
69
- let components_by_trigger_type = app
70
- . triggers
71
- . iter ( )
72
- . map ( |( ty, ts) | {
73
- ts. iter ( )
74
- . map ( |t| component_source ( app, t) )
75
- . collect :: < Result < Vec < _ > , _ > > ( )
76
- . map ( |css| ( ty, css) )
77
- } )
78
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
70
+ let components_by_trigger_type_futs = app. triggers . iter ( ) . map ( |( ty, ts) | {
71
+ load_and_resolve_all ( app, ts, resolution_context)
72
+ . map ( |css| css. map ( |css| ( ty. to_owned ( ) , css) ) )
73
+ } ) ;
74
+ let components_by_trigger_type = join_all_result ( components_by_trigger_type_futs) . await ?;
79
75
80
76
for ( trigger_type, component) in components_by_trigger_type {
81
77
for component in & component {
82
- validate_component_against_environments (
83
- envs,
84
- trigger_type,
85
- component,
86
- resolution_context,
87
- )
88
- . await ?;
78
+ validate_component_against_environments ( envs, & trigger_type, component) . await ?;
89
79
}
90
80
}
91
81
@@ -96,7 +86,6 @@ async fn validate_component_against_environments(
96
86
envs : & [ TargetEnvironment ] ,
97
87
trigger_type : & TriggerType ,
98
88
component : & ComponentToValidate < ' _ > ,
99
- resolution_context : & ResolutionContext ,
100
89
) -> anyhow:: Result < ( ) > {
101
90
let worlds = envs
102
91
. iter ( )
@@ -110,21 +99,16 @@ async fn validate_component_against_environments(
110
99
. map ( |w| ( e. name . as_str ( ) , w) )
111
100
} )
112
101
. collect :: < Result < std:: collections:: HashSet < _ > , _ > > ( ) ?;
113
- validate_component_against_worlds ( worlds. into_iter ( ) , component, resolution_context ) . await ?;
102
+ validate_component_against_worlds ( worlds. into_iter ( ) , component) . await ?;
114
103
Ok ( ( ) )
115
104
}
116
105
117
106
async fn validate_component_against_worlds (
118
107
target_worlds : impl Iterator < Item = ( & str , & TargetWorld ) > ,
119
108
component : & ComponentToValidate < ' _ > ,
120
- resolution_context : & ResolutionContext ,
121
109
) -> anyhow:: Result < ( ) > {
122
- let loader = ComponentSourceLoader :: new ( resolution_context. wasm_loader ( ) ) ;
123
- let wasm_bytes = spin_compose:: compose ( & loader, component) . await ?;
124
-
125
110
for ( env_name, target_world) in target_worlds {
126
- validate_wasm_against_any_world ( env_name, target_world, component, wasm_bytes. as_ref ( ) )
127
- . await ?;
111
+ validate_wasm_against_any_world ( env_name, target_world, component) . await ?;
128
112
}
129
113
130
114
tracing:: info!(
@@ -139,7 +123,6 @@ async fn validate_wasm_against_any_world(
139
123
env_name : & str ,
140
124
target_world : & TargetWorld ,
141
125
component : & ComponentToValidate < ' _ > ,
142
- wasm : & [ u8 ] ,
143
126
) -> anyhow:: Result < ( ) > {
144
127
let mut result = Ok ( ( ) ) ;
145
128
for target_str in target_world. versioned_names ( ) {
@@ -148,7 +131,7 @@ async fn validate_wasm_against_any_world(
148
131
component. id( ) ,
149
132
component. source_description( ) ,
150
133
) ;
151
- match validate_wasm_against_world ( env_name, & target_str, component, wasm ) . await {
134
+ match validate_wasm_against_world ( env_name, & target_str, component) . await {
152
135
Ok ( ( ) ) => {
153
136
tracing:: info!(
154
137
"Validated component {} {} against target world {target_str}" ,
@@ -175,7 +158,6 @@ async fn validate_wasm_against_world(
175
158
env_name : & str ,
176
159
target_str : & str ,
177
160
component : & ComponentToValidate < ' _ > ,
178
- wasm : & [ u8 ] ,
179
161
) -> anyhow:: Result < ( ) > {
180
162
let comp_name = "root:component" ;
181
163
@@ -200,7 +182,7 @@ async fn validate_wasm_against_world(
200
182
. await
201
183
. context ( "reg_resolver.resolve failed" ) ?;
202
184
203
- packages. insert ( compkey, wasm . to_vec ( ) ) ;
185
+ packages. insert ( compkey, component . wasm_bytes ( ) . to_vec ( ) ) ;
204
186
205
187
match doc. resolve ( packages) {
206
188
Ok ( _) => Ok ( ( ) ) ,
@@ -223,3 +205,15 @@ async fn validate_wasm_against_world(
223
205
} ,
224
206
}
225
207
}
208
+
209
+ /// Equivalent to futures::future::join_all, but specialised for iterators of
210
+ /// fallible futures. It returns a Result<Vec<...>> instead of a Vec<Result<...>> -
211
+ /// this just moves the transposition boilerplate out of the main flow.
212
+ async fn join_all_result < T , I > ( iter : I ) -> anyhow:: Result < Vec < T > >
213
+ where
214
+ I : IntoIterator ,
215
+ I :: Item : std:: future:: Future < Output = anyhow:: Result < T > > ,
216
+ {
217
+ let vec_result = futures:: future:: join_all ( iter) . await ;
218
+ vec_result. into_iter ( ) . collect ( )
219
+ }
0 commit comments