...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package main
17
18 import "fmt"
19
20 type FrontendCodeGenerator struct {
21
22 chaosTypes []string
23 }
24
25 func newFrontendCodeGenerator(chaosTypes []string) *FrontendCodeGenerator {
26 return &FrontendCodeGenerator{chaosTypes}
27 }
28
29 func (it *FrontendCodeGenerator) AppendTypes(typeName string) {
30 it.chaosTypes = append(it.chaosTypes, typeName)
31 }
32
33 const typesTemplate = `import { ExperimentKind } from 'components/NewExperiment/types'
34
35 const mapping = new Map<ExperimentKind, string>([
36 %s])
37
38 export function templateTypeToFieldName(templateType: ExperimentKind): string {
39 return mapping.get(templateType)!
40 }
41 `
42
43 func (it *FrontendCodeGenerator) Render() string {
44 return fmt.Sprintf(typesTemplate, it.mapEntries())
45 }
46
47 func (it *FrontendCodeGenerator) mapEntries() string {
48 entries := ""
49 for _, chaosType := range it.chaosTypes {
50 entries += fmt.Sprintf(` ['%s', '%s'],
51 `, chaosType, lowercaseCamelCase(chaosType))
52 }
53 return entries
54 }
55