@@ -54,15 +54,14 @@ type AppletOption func(*Applet) error
54
54
type ThreadInitializer func (thread * starlark.Thread ) * starlark.Thread
55
55
56
56
type Applet struct {
57
- ID string
57
+ ID string
58
+ Globals map [string ]starlark.StringDict
59
+ MainFile string
58
60
59
61
loader ModuleLoader
60
62
initializers []ThreadInitializer
61
63
loadedPaths map [string ]bool
62
64
63
- globals map [string ]starlark.StringDict
64
-
65
- mainFile string
66
65
mainFun * starlark.Function
67
66
schemaFile string
68
67
@@ -130,7 +129,7 @@ func NewApplet(id string, src []byte, opts ...AppletOption) (*Applet, error) {
130
129
func NewAppletFromFS (id string , fsys fs.FS , opts ... AppletOption ) (* Applet , error ) {
131
130
a := & Applet {
132
131
ID : id ,
133
- globals : make (map [string ]starlark.StringDict ),
132
+ Globals : make (map [string ]starlark.StringDict ),
134
133
loadedPaths : make (map [string ]bool ),
135
134
}
136
135
@@ -153,23 +152,18 @@ func (a *Applet) Run(ctx context.Context) (roots []render.Root, err error) {
153
152
return a .RunWithConfig (ctx , nil )
154
153
}
155
154
156
- // RunWithConfig exceutes the applet's main function, passing it configuration as a
157
- // starlark dict. It returns the render roots that are returned by the applet.
158
- func (a * Applet ) RunWithConfig (ctx context.Context , config map [string ]string ) (roots []render.Root , err error ) {
159
- var args starlark.Tuple
160
- if a .mainFun .NumParams () > 0 {
161
- starlarkConfig := AppletConfig (config )
162
- args = starlark.Tuple {starlarkConfig }
163
- }
164
-
165
- returnValue , err := a .Call (ctx , a .mainFun , args ... )
166
- if err != nil {
167
- return nil , err
168
- }
155
+ // ExtractRoots extracts render roots from a Starlark value. It expects the value
156
+ // to be either a single render root or a list of render roots.
157
+ //
158
+ // It's used internally by RunWithConfig to extract the roots returned by the applet.
159
+ func ExtractRoots (val starlark.Value ) ([]render.Root , error ) {
160
+ var roots []render.Root
169
161
170
- if returnRoot , ok := returnValue .(render_runtime.Rootable ); ok {
162
+ if val == starlark .None {
163
+ // no roots returned
164
+ } else if returnRoot , ok := val .(render_runtime.Rootable ); ok {
171
165
roots = []render.Root {returnRoot .AsRenderRoot ()}
172
- } else if returnList , ok := returnValue .(* starlark.List ); ok {
166
+ } else if returnList , ok := val .(* starlark.List ); ok {
173
167
roots = make ([]render.Root , returnList .Len ())
174
168
iter := returnList .Iterate ()
175
169
defer iter .Done ()
@@ -188,7 +182,29 @@ func (a *Applet) RunWithConfig(ctx context.Context, config map[string]string) (r
188
182
i ++
189
183
}
190
184
} else {
191
- return nil , fmt .Errorf ("expected app implementation to return Root(s) but found: %s" , returnValue .Type ())
185
+ return nil , fmt .Errorf ("expected app implementation to return Root(s) but found: %s" , val .Type ())
186
+ }
187
+
188
+ return roots , nil
189
+ }
190
+
191
+ // RunWithConfig exceutes the applet's main function, passing it configuration as a
192
+ // starlark dict. It returns the render roots that are returned by the applet.
193
+ func (a * Applet ) RunWithConfig (ctx context.Context , config map [string ]string ) (roots []render.Root , err error ) {
194
+ var args starlark.Tuple
195
+ if a .mainFun .NumParams () > 0 {
196
+ starlarkConfig := AppletConfig (config )
197
+ args = starlark.Tuple {starlarkConfig }
198
+ }
199
+
200
+ returnValue , err := a .Call (ctx , a .mainFun , args ... )
201
+ if err != nil {
202
+ return nil , err
203
+ }
204
+
205
+ roots , err = ExtractRoots (returnValue )
206
+ if err != nil {
207
+ return nil , err
192
208
}
193
209
194
210
return roots , nil
@@ -220,7 +236,7 @@ func (app *Applet) CallSchemaHandler(ctx context.Context, handlerName, parameter
220
236
return options , nil
221
237
222
238
case schema .ReturnSchema :
223
- sch , err := schema .FromStarlark (resultVal , app .globals [app .schemaFile ])
239
+ sch , err := schema .FromStarlark (resultVal , app .Globals [app .schemaFile ])
224
240
if err != nil {
225
241
return "" , err
226
242
}
@@ -253,7 +269,7 @@ func (app *Applet) RunTests(t *testing.T) {
253
269
return thread
254
270
})
255
271
256
- for file , globals := range app .globals {
272
+ for file , globals := range app .Globals {
257
273
for name , global := range globals {
258
274
if ! strings .HasPrefix (name , "test_" ) {
259
275
continue
@@ -347,7 +363,7 @@ func (a *Applet) ensureLoaded(fsys fs.FS, pathToLoad string, currentlyLoading ..
347
363
348
364
// normalize path so that it can be used as a key
349
365
pathToLoad = path .Clean (pathToLoad )
350
- if _ , ok := a .globals [pathToLoad ]; ok {
366
+ if _ , ok := a .Globals [pathToLoad ]; ok {
351
367
// already loaded, good to go
352
368
return nil
353
369
}
@@ -390,7 +406,7 @@ func (a *Applet) ensureLoaded(fsys fs.FS, pathToLoad string, currentlyLoading ..
390
406
return nil , err
391
407
}
392
408
393
- if g , ok := a .globals [modulePath ]; ! ok {
409
+ if g , ok := a .Globals [modulePath ]; ! ok {
394
410
return nil , fmt .Errorf ("module %s not loaded" , modulePath )
395
411
} else {
396
412
return g , nil
@@ -416,17 +432,17 @@ func (a *Applet) ensureLoaded(fsys fs.FS, pathToLoad string, currentlyLoading ..
416
432
if err != nil {
417
433
return fmt .Errorf ("starlark.ExecFile: %v" , err )
418
434
}
419
- a .globals [pathToLoad ] = globals
435
+ a .Globals [pathToLoad ] = globals
420
436
421
437
// if the file is in the root directory, check for the main function
422
438
// and schema function
423
439
mainFun , _ := globals ["main" ].(* starlark.Function )
424
440
if mainFun != nil {
425
- if a .mainFile != "" {
426
- return fmt .Errorf ("multiple files with a main() function:\n - %s\n - %s" , pathToLoad , a .mainFile )
441
+ if a .MainFile != "" {
442
+ return fmt .Errorf ("multiple files with a main() function:\n - %s\n - %s" , pathToLoad , a .MainFile )
427
443
}
428
444
429
- a .mainFile = pathToLoad
445
+ a .MainFile = pathToLoad
430
446
a .mainFun = mainFun
431
447
}
432
448
@@ -454,7 +470,7 @@ func (a *Applet) ensureLoaded(fsys fs.FS, pathToLoad string, currentlyLoading ..
454
470
}
455
471
456
472
default :
457
- a .globals [pathToLoad ] = starlark.StringDict {
473
+ a .Globals [pathToLoad ] = starlark.StringDict {
458
474
"file" : & file.File {
459
475
FS : fsys ,
460
476
Path : pathToLoad ,
0 commit comments