Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 // Package subst is a generic string template resolver. | |
|
hinoka
2017/02/17 22:07:40
subst is confusing, how about just "substitution"
dnj
2017/02/18 04:48:32
Changed to "template_string". This isn't just for
| |
| 6 // | |
| 7 // It resolves templates embedded in a string into a complete resolved string. | |
| 8 // For example, | |
| 9 // | |
| 10 // Resolve("foo/${value}", map[string]string{"bar": "value"} will resolve t o | |
| 11 // "foo/bar". | |
| 12 package subst | |
| 13 | |
| 14 import ( | |
| 15 "regexp" | |
| 16 "strings" | |
| 17 | |
| 18 "github.com/luci/luci-go/common/errors" | |
| 19 ) | |
| 20 | |
| 21 // We're looking for: ${name}. The "$" can be escaped by preceding it with a | |
| 22 // "\". | |
| 23 // | |
| 24 // Submatch indices: | |
| 25 // [0:1] Full match | |
| 26 // [2:3] Preceding character(?) | |
| 27 // [4:5] (key) | |
| 28 // [6:7] Terminating Brace or empty (if incomplete) | |
| 29 var namedFormatMatcher = regexp.MustCompile(`(?:(^|[^\\])\$)\{([^\}]+)(\}|$)`) | |
|
hinoka
2017/02/17 22:07:40
What about using https://golang.org/pkg/text/templ
dnj
2017/02/18 04:48:32
template is more of a document-wide package. It's
| |
| 30 | |
| 31 // Subst contains supported SwarmBucket string substitutions. | |
| 32 // | |
| 33 // A template string can be resolved by passing it to a Subst instance's | |
| 34 // resolve method. | |
| 35 type Subst struct { | |
| 36 // SwarmingRunID is the substitution to use for the "swarming_run_id" te mplate | |
| 37 // parameter. | |
| 38 // | |
| 39 // Note that this is the Swarming Run ID, not Task ID. The Run ID is the | |
| 40 // combination of the Task ID with the try number. | |
| 41 SwarmingRunID string | |
| 42 } | |
| 43 | |
| 44 // Resolve performs string resolution. | |
|
hinoka
2017/02/17 22:07:40
Of what to what?
dnj
2017/02/18 04:48:32
Done.
| |
| 45 // | |
| 46 // If the string includes an erroneous template reference, or if the referenced | |
| 47 // template variable isn't included in the "substitutions" map, Resolve will | |
| 48 // return an error. | |
| 49 func (s *Subst) Resolve(v string) (string, error) { | |
|
hinoka
2017/02/17 22:07:40
If we have less than 3 inputs, i think a function
dnj
2017/02/18 04:48:32
I think a struct is good, (a) because if we add mo
| |
| 50 smi := namedFormatMatcher.FindAllStringSubmatchIndex(v, -1) | |
| 51 | |
| 52 var ( | |
| 53 parts = make([]string, 0, (len(smi)*2)+1) | |
| 54 pos = 0 | |
| 55 ) | |
| 56 | |
| 57 for _, match := range smi { | |
| 58 if v[match[6]:match[7]] != "}" { | |
| 59 return "", errors.Reason("incomplete template: %(templat e)q"). | |
| 60 D("template", v). | |
| 61 Err() | |
| 62 } | |
| 63 | |
| 64 // Add anything in between the previous match and the current. I f our match | |
| 65 // includes a non-escape character, add that too. | |
| 66 if pos < match[3] { | |
| 67 parts = append(parts, v[pos:match[3]]) | |
| 68 } | |
| 69 pos = match[1] | |
| 70 | |
| 71 key := v[match[4]:match[5]] | |
| 72 subst, err := s.getTemplateValue(key) | |
| 73 if err != nil { | |
| 74 return "", errors.Annotate(err).Err() | |
| 75 } | |
| 76 parts = append(parts, subst) | |
| 77 } | |
| 78 parts = append(parts, v[pos:]) | |
| 79 | |
| 80 return strings.Join(parts, ""), nil | |
| 81 } | |
| 82 | |
| 83 func (s *Subst) getTemplateValue(v string) (string, error) { | |
| 84 switch v { | |
| 85 case "swarming_run_id": | |
| 86 if s.SwarmingRunID == "" { | |
| 87 return "", errors.New("no Swarming run ID available") | |
| 88 } | |
| 89 return s.SwarmingRunID, nil | |
| 90 | |
| 91 default: | |
| 92 return "", errors.Reason("unknown substitution for: %(value)q"). | |
| 93 D("value", v). | |
| 94 Err() | |
| 95 } | |
| 96 } | |
| OLD | NEW |