MCPcopy Create free account
hub / github.com/cli/cli / parseTemplate

Function parseTemplate

script/ghmock/restcheck.go:58–82  ·  view source on GitHub ↗
(tmpl string)

Source from the content-addressed store, hash-verified

56}
57
58func parseTemplate(tmpl string) []routeSegment {
59 parts := splitPath(tmpl)
60 segs := make([]routeSegment, len(parts))
61 for i, p := range parts {
62 switch {
63 case !strings.Contains(p, "{"):
64 segs[i] = routeSegment{literal: p}
65 case paramPattern.FindString(p) == p:
66 segs[i] = routeSegment{param: p[1 : len(p)-1]}
67 default:
68 var expr strings.Builder
69 expr.WriteString("^")
70 last := 0
71 for _, loc := range paramPattern.FindAllStringIndex(p, -1) {
72 expr.WriteString(regexp.QuoteMeta(p[last:loc[0]]))
73 expr.WriteString("[^/]+")
74 last = loc[1]
75 }
76 expr.WriteString(regexp.QuoteMeta(p[last:]))
77 expr.WriteString("$")
78 segs[i] = routeSegment{pattern: regexp.MustCompile(expr.String())}
79 }
80 }
81 return segs
82}
83
84func splitPath(p string) []string {
85 p = strings.Trim(p, "/")

Callers 2

TestRESTMatchFunction · 0.85
loadRESTIndexFunction · 0.85

Calls 3

splitPathFunction · 0.85
ContainsMethod · 0.80
StringMethod · 0.65

Tested by 1

TestRESTMatchFunction · 0.68