MCPcopy Index your code
hub / github.com/keploy/keploy / updateTemplateFromTokenized

Function updateTemplateFromTokenized

pkg/util.go:2160–2254  ·  view source on GitHub ↗

updateTemplateFromTokenized handles a single "templated value vs live value" pair where the templated value contains one or more {{.VarName}} placeholders. It supports: - Whole-value placeholder: "{{.var}}" -> var = liveVal - Prefix/suffix literal wrapping: "trace-{{.id}}-v1" -> id = substring betwe

(
	logger *zap.Logger,
	templatedVal, liveVal string,
	placeholderRe *regexp.Regexp,
	currentTemplatedValues, prevTemplatedValues map[string]interface{},
	debugPath string,
)

Source from the content-addressed store, hash-verified

2158// - Multi-placeholder values are best-effort: they only update when the literal separators
2159// between placeholders can unambiguously split the live value.
2160func updateTemplateFromTokenized(
2161 logger *zap.Logger,
2162 templatedVal, liveVal string,
2163 placeholderRe *regexp.Regexp,
2164 currentTemplatedValues, prevTemplatedValues map[string]interface{},
2165 debugPath string,
2166) bool {
2167 trim := strings.TrimSpace(templatedVal)
2168 matches := placeholderRe.FindAllStringSubmatchIndex(templatedVal, -1)
2169 if len(matches) == 0 {
2170 return false
2171 }
2172
2173 // Fast path: the entire (trimmed) templated value is a single placeholder.
2174 if len(matches) == 1 && strings.HasPrefix(trim, "{{") && strings.HasSuffix(trim, "}}") {
2175 inner := placeholderRe.FindStringSubmatch(trim)
2176 if len(inner) < 2 {
2177 return false
2178 }
2179 keys := extractTemplateKeys(inner[1])
2180 if len(keys) != 1 {
2181 return false
2182 }
2183 return applyTemplateUpdate(logger, keys[0], strings.TrimSpace(liveVal), currentTemplatedValues, prevTemplatedValues, debugPath)
2184 }
2185
2186 // General path: split the templated value into alternating literals & placeholders,
2187 // then use each literal as an anchor in liveVal to carve out each live substitution.
2188 type segment struct {
2189 isPlaceholder bool
2190 literal string // only set when !isPlaceholder
2191 key string // only set when isPlaceholder
2192 }
2193 var segs []segment
2194 cursor := 0
2195 for _, m := range matches {
2196 start, end := m[0], m[1]
2197 if start > cursor {
2198 segs = append(segs, segment{literal: templatedVal[cursor:start]})
2199 }
2200 innerMatch := placeholderRe.FindStringSubmatch(templatedVal[start:end])
2201 if len(innerMatch) < 2 {
2202 cursor = end
2203 continue
2204 }
2205 keys := extractTemplateKeys(innerMatch[1])
2206 if len(keys) != 1 {
2207 cursor = end
2208 continue
2209 }
2210 segs = append(segs, segment{isPlaceholder: true, key: keys[0]})
2211 cursor = end
2212 }
2213 if cursor < len(templatedVal) {
2214 segs = append(segs, segment{literal: templatedVal[cursor:]})
2215 }
2216
2217 // Walk live value consuming segments.

Callers 2

Calls 2

extractTemplateKeysFunction · 0.85
applyTemplateUpdateFunction · 0.85

Tested by

no test coverage detected