...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/chaos-builder/frontend.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/chaos-builder

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package main
    17  
    18  import "fmt"
    19  
    20  type FrontendCodeGenerator struct {
    21  	// name of each Kind of chaos, for example: PodChaos, IOChaos, DNSChaos
    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