MCPcopy Create free account
hub / github.com/devfile/api / validateAndReplaceDataWithVariable

Function validateAndReplaceDataWithVariable

pkg/validation/variables/variables.go:74–91  ·  view source on GitHub ↗

validateAndReplaceDataWithVariable validates the string for a global variable and replaces it. An error is returned if the string references an invalid global variable key

(val string, variables map[string]string)

Source from the content-addressed store, hash-verified

72// validateAndReplaceDataWithVariable validates the string for a global variable and replaces it. An error
73// is returned if the string references an invalid global variable key
74func validateAndReplaceDataWithVariable(val string, variables map[string]string) (string, error) {
75 matches := globalVariableRegex.FindAllStringSubmatch(val, -1)
76 var invalidKeys []string
77 for _, match := range matches {
78 varValue, ok := variables[match[1]]
79 if !ok {
80 invalidKeys = append(invalidKeys, match[1])
81 } else {
82 val = strings.Replace(val, match[0], varValue, -1)
83 }
84 }
85
86 if len(invalidKeys) > 0 {
87 return val, &InvalidKeysError{Keys: invalidKeys}
88 }
89
90 return val, nil
91}

Calls

no outgoing calls

Tested by 1