Chromium Code Reviews| Index: buildbucket/swarmbucket/templatestr/template.go |
| diff --git a/buildbucket/swarmbucket/templatestr/template.go b/buildbucket/swarmbucket/templatestr/template.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f93318ea37e3ac973bb8e47e3ca7ac3b3a84ce97 |
| --- /dev/null |
| +++ b/buildbucket/swarmbucket/templatestr/template.go |
| @@ -0,0 +1,97 @@ |
| +// 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.
|
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +// Package templatestr is a generic string template resolver. |
| +// |
| +// It resolves templates embedded in a string into a complete resolved string. |
| +// For example, |
| +// |
| +// Resolve("foo/${value}", map[string]string{"bar": "value"} will resolve to |
| +// "foo/bar". |
| +package templatestr |
| + |
| +import ( |
| + "regexp" |
| + "strings" |
| + |
| + "github.com/luci/luci-go/common/errors" |
| +) |
| + |
| +// We're looking for: ${name}. The "$" can be escaped by preceding it with a |
| +// "\". |
| +// |
| +// Submatch indices: |
| +// [0:1] Full match |
| +// [2:3] Preceding character(?) |
| +// [4:5] (key) |
| +// [6:7] Terminating Brace or empty (if incomplete) |
| +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
|
| + |
| +// Params contains supported SwarmBucket string substitution parameters. |
| +// |
| +// A template string can be resolved by passing it to a Params instance's |
| +// resolve method. |
| +type Params struct { |
| + // SwarmingRunID is the substitution to use for the "swarming_run_id" template |
| + // parameter. |
| + // |
| + // Note that this is the Swarming Run ID, not Task ID. The Run ID is the |
| + // combination of the Task ID with the try number. |
| + 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.
|
| +} |
| + |
| +// Resolve resolves v against p's parameters, populating any template fields |
| +// with their respective parameter value. |
| +// |
| +// If the string includes an erroneous template reference, or if the referenced |
| +// template variable isn't included in the "substitutions" map, Resolve will |
| +// return an error. |
| +func (p *Params) Resolve(v string) (string, error) { |
| + smi := namedFormatMatcher.FindAllStringSubmatchIndex(v, -1) |
| + |
| + var ( |
| + parts = make([]string, 0, (len(smi)*2)+1) |
| + pos = 0 |
| + ) |
| + |
| + for _, match := range smi { |
| + if v[match[6]:match[7]] != "}" { |
| + return "", errors.Reason("incomplete template: %(template)q"). |
| + D("template", v). |
| + Err() |
| + } |
| + |
| + // Add anything in between the previous match and the current. If our match |
| + // includes a non-escape character, add that too. |
| + if pos < match[3] { |
| + parts = append(parts, v[pos:match[3]]) |
| + } |
| + pos = match[1] |
| + |
| + key := v[match[4]:match[5]] |
| + subst, err := p.getTemplateValue(key) |
| + if err != nil { |
| + return "", errors.Annotate(err).Err() |
| + } |
| + parts = append(parts, subst) |
| + } |
| + parts = append(parts, v[pos:]) |
| + |
| + return strings.Join(parts, ""), nil |
| +} |
| + |
| +func (p *Params) getTemplateValue(v string) (string, error) { |
| + switch v { |
| + case "swarming_run_id": |
| + if p.SwarmingRunID == "" { |
| + return "", errors.New("no Swarming run ID available") |
| + } |
| + return p.SwarmingRunID, nil |
| + |
| + default: |
| + return "", errors.Reason("unknown substitution for: %(value)q"). |
| + D("value", v). |
| + Err() |
| + } |
| +} |