MCPcopy Create free account
hub / github.com/github/gh-aw / UnquoteYAMLKey

Function UnquoteYAMLKey

pkg/workflow/yaml.go:164–188  ·  view source on GitHub ↗

UnquoteYAMLKey removes quotes from a YAML key at the start of a line. The YAML marshaler automatically adds quotes around YAML reserved words and keywords to prevent parsing ambiguity. For example, the word "on" is a YAML boolean value, so the marshaler outputs it as "on": to distinguish it from th

(yamlStr string, key string)

Source from the content-addressed store, hash-verified

162// result := UnquoteYAMLKey(input, "on")
163// // result: "on:\n push:\n branches:\n - main"
164func UnquoteYAMLKey(yamlStr string, key string) string {
165 yamlLog.Printf("Unquoting YAML key: %s", key)
166
167 // Create a regex pattern that matches the quoted key at the start of a line
168 // Pattern: (start of line or newline) + (optional whitespace) + quoted key + colon
169 pattern := `(^|\n)([ \t]*)"` + regexp.QuoteMeta(key) + `":`
170
171 // Use cached compiled regex to avoid recompiling on every call
172 var re *regexp.Regexp
173 if cached, ok := unquoteYAMLKeyCache.Load(key); ok {
174 var typeOK bool
175 re, typeOK = cached.(*regexp.Regexp)
176 if !typeOK {
177 unquoteYAMLKeyCache.Delete(key)
178 re = regexp.MustCompile(pattern)
179 unquoteYAMLKeyCache.Store(key, re)
180 }
181 } else {
182 re = regexp.MustCompile(pattern)
183 unquoteYAMLKeyCache.Store(key, re)
184 }
185 // Use ReplaceAllString with capture group references for a single-pass replacement.
186 // ${1} = line start (^ or \n), ${2} = optional whitespace
187 return re.ReplaceAllString(yamlStr, "${1}${2}"+key+":")
188}
189
190// UnquoteYAMLTopLevelKey removes quotes from a YAML key only when it appears
191// at the very start of the YAML content.

Callers 5

TestUnquoteYAMLKeyFunction · 0.85
BenchmarkUnquoteYAMLKeyFunction · 0.85

Calls 4

LoadMethod · 0.80
DeleteMethod · 0.80
MustCompileMethod · 0.80
PrintfMethod · 0.45

Tested by 2

TestUnquoteYAMLKeyFunction · 0.68
BenchmarkUnquoteYAMLKeyFunction · 0.68