@@ -16,6 +16,7 @@ package apiEditor
16
16
17
17
import (
18
18
"encoding/json"
19
+ "strings"
19
20
20
21
"github.com/sirupsen/logrus"
21
22
@@ -24,13 +25,13 @@ import (
24
25
"github.com/erda-project/erda/pkg/expression"
25
26
)
26
27
27
- const props1 string = `{
28
+ var props1 = `{
28
29
"loopFormField":[
29
30
{
30
31
"component":"formGroup",
31
32
"key":"loop",
32
33
"componentProps":{
33
- "defaultExpand":false ,
34
+ "defaultExpand": ` + LoopFormFieldDefaultExpand . string () + ` ,
34
35
"expandable":true,
35
36
"title":"循环策略"
36
37
},
@@ -335,9 +336,42 @@ const props3 string = `}
335
336
//}
336
337
const props4 string = `}`
337
338
338
- func genProps (input , execute string ) interface {} {
339
+ type optionKey string
340
+
341
+ // placeholder for whether the loop strategy is expanded by default
342
+ const LoopFormFieldDefaultExpand = optionKey ("defaultExpand" )
343
+
344
+ func (opt optionKey ) string () string {
345
+ return "{{_" + string (opt ) + "_}}"
346
+ }
347
+
348
+ type replaceOption struct {
349
+ key optionKey
350
+ value string
351
+ }
352
+
353
+ var defaultReplaceOptions = []replaceOption {
354
+ {
355
+ key : LoopFormFieldDefaultExpand ,
356
+ value : "false" ,
357
+ },
358
+ }
359
+
360
+ func genProps (input , execute string , replaceOpts ... replaceOption ) interface {} {
361
+ // because props are assembled by splicing json strings,
362
+ // dynamic setting values can only be replaced by placeholders.
363
+ var propsJson = props1 + input + props2 + input + props3 + execute + props4
364
+
365
+ for _ , opt := range replaceOpts {
366
+ propsJson = strings .ReplaceAll (propsJson , opt .key .string (), opt .value )
367
+ }
368
+
369
+ for _ , opt := range defaultReplaceOptions {
370
+ propsJson = strings .ReplaceAll (propsJson , opt .key .string (), opt .value )
371
+ }
372
+
339
373
var propsI interface {}
340
- if err := json .Unmarshal ([]byte (props1 + input + props2 + input + props3 + execute + props4 ), & propsI ); err != nil {
374
+ if err := json .Unmarshal ([]byte (propsJson ), & propsI ); err != nil {
341
375
logrus .Errorf ("init props name=testplan component=formModal propsType=CreateTestPlan err: errMsg: %v" , err )
342
376
}
343
377
0 commit comments