Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
|
nodir
2017/02/18 03:19:48
the template format is not buildbucket specific re
dnj
2017/02/18 06:08:13
Acknowledged.
| |
| 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 templatestr is a generic string template resolver. | |
| 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 templatestr | |
| 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(`(?:(^|[^\\])\$)\{([^\}]+)(\}|$)`) | |
|
nodir
2017/02/18 03:19:48
buildbucket uses python string.Template class for
dnj
2017/02/18 06:08:13
I went ahead and implemented a Python-style string
| |
| 30 | |
| 31 // Params contains supported SwarmBucket string substitution parameters. | |
| 32 // | |
| 33 // A template string can be resolved by passing it to a Params instance's | |
| 34 // resolve method. | |
| 35 type Params 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 | |
|
nodir
2017/02/18 04:27:52
i think this package mixes code of different abstr
dnj
2017/02/18 06:08:13
I was leaving room for future substitutions.
| |
| 42 } | |
| 43 | |
| 44 // Resolve resolves v against p's parameters, populating any template fields | |
| 45 // with their respective parameter value. | |
| 46 // | |
| 47 // If the string includes an erroneous template reference, or if the referenced | |
| 48 // template variable isn't included in the "substitutions" map, Resolve will | |
| 49 // return an error. | |
| 50 func (p *Params) Resolve(v string) (string, error) { | |
| 51 smi := namedFormatMatcher.FindAllStringSubmatchIndex(v, -1) | |
| 52 | |
| 53 var ( | |
| 54 parts = make([]string, 0, (len(smi)*2)+1) | |
| 55 pos = 0 | |
| 56 ) | |
| 57 | |
| 58 for _, match := range smi { | |
| 59 if v[match[6]:match[7]] != "}" { | |
| 60 return "", errors.Reason("incomplete template: %(templat e)q"). | |
| 61 D("template", v). | |
| 62 Err() | |
| 63 } | |
| 64 | |
| 65 // Add anything in between the previous match and the current. I f our match | |
| 66 // includes a non-escape character, add that too. | |
| 67 if pos < match[3] { | |
| 68 parts = append(parts, v[pos:match[3]]) | |
| 69 } | |
| 70 pos = match[1] | |
| 71 | |
| 72 key := v[match[4]:match[5]] | |
| 73 subst, err := p.getTemplateValue(key) | |
| 74 if err != nil { | |
| 75 return "", errors.Annotate(err).Err() | |
| 76 } | |
| 77 parts = append(parts, subst) | |
| 78 } | |
| 79 parts = append(parts, v[pos:]) | |
| 80 | |
| 81 return strings.Join(parts, ""), nil | |
| 82 } | |
| 83 | |
| 84 func (p *Params) getTemplateValue(v string) (string, error) { | |
| 85 switch v { | |
| 86 case "swarming_run_id": | |
| 87 if p.SwarmingRunID == "" { | |
| 88 return "", errors.New("no Swarming run ID available") | |
| 89 } | |
| 90 return p.SwarmingRunID, nil | |
| 91 | |
| 92 default: | |
| 93 return "", errors.Reason("unknown substitution for: %(value)q"). | |
| 94 D("value", v). | |
| 95 Err() | |
| 96 } | |
| 97 } | |
| OLD | NEW |