()
| 30 | } |
| 31 | |
| 32 | func (rp RoutePatterns) Compile() (*regexp.Regexp, error) { |
| 33 | patterns := make([]string, len(rp)) |
| 34 | for i, p := range rp { |
| 35 | p = strings.ReplaceAll(p, "**", ".+") |
| 36 | p = strings.ReplaceAll(p, "*", "[^/]+") |
| 37 | if !strings.HasSuffix(p, "/") { |
| 38 | p += "/?" |
| 39 | } |
| 40 | patterns[i] = p |
| 41 | } |
| 42 | |
| 43 | pattern := fmt.Sprintf("^(%s)$", strings.Join(patterns, "|")) |
| 44 | re, err := regexp.Compile(pattern) |
| 45 | if err != nil { |
| 46 | return nil, xerrors.Errorf("compile regex %q: %w", pattern, err) |
| 47 | } |
| 48 | |
| 49 | return re, nil |
| 50 | } |