2018-09-22 01:13:23 +00:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-09-22 00:59:11 +00:00
|
|
|
package fsm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-28 19:55:53 +00:00
|
|
|
"regexp"
|
2018-09-22 00:59:11 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-09-28 19:55:53 +00:00
|
|
|
var (
|
|
|
|
templateReplaceCaptureRE = regexp.MustCompile(`\$\{?([a-zA-Z0-9_\$]+)\}?`)
|
|
|
|
)
|
|
|
|
|
2018-09-28 20:13:06 +00:00
|
|
|
type TemplateFormatter struct {
|
2018-09-22 00:59:11 +00:00
|
|
|
captureIndexes []int
|
|
|
|
captureCount int
|
|
|
|
fmtString string
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:31:51 +00:00
|
|
|
// NewTemplateFormatter instantiates a TemplateFormatter
|
|
|
|
// from given template string and the maximum amount of captures.
|
2018-10-03 23:30:01 +00:00
|
|
|
func NewTemplateFormatter(template string, captureCount int) *TemplateFormatter {
|
|
|
|
matches := templateReplaceCaptureRE.FindAllStringSubmatch(template, -1)
|
2018-09-22 00:59:11 +00:00
|
|
|
if len(matches) == 0 {
|
|
|
|
// if no regex reference found, keep it as it is
|
2018-10-03 23:30:01 +00:00
|
|
|
return &TemplateFormatter{captureCount: 0, fmtString: template}
|
2018-09-22 00:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var indexes []int
|
2018-10-03 23:30:01 +00:00
|
|
|
valueFormatter := template
|
2018-09-22 00:59:11 +00:00
|
|
|
for _, match := range matches {
|
|
|
|
idx, err := strconv.Atoi(match[len(match)-1])
|
|
|
|
if err != nil || idx > captureCount || idx < 1 {
|
|
|
|
// if index larger than captured count or using unsupported named capture group,
|
|
|
|
// replace with empty string
|
|
|
|
valueFormatter = strings.Replace(valueFormatter, match[0], "", -1)
|
|
|
|
} else {
|
|
|
|
valueFormatter = strings.Replace(valueFormatter, match[0], "%s", -1)
|
|
|
|
// note: the regex reference variable $? starts from 1
|
|
|
|
indexes = append(indexes, idx-1)
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 20:13:06 +00:00
|
|
|
return &TemplateFormatter{
|
2018-09-22 00:59:11 +00:00
|
|
|
captureIndexes: indexes,
|
|
|
|
captureCount: len(indexes),
|
|
|
|
fmtString: valueFormatter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:31:51 +00:00
|
|
|
// Format accepts a list containing captured strings and returns the formatted
|
|
|
|
// string using the template stored in current TemplateFormatter.
|
2018-09-28 20:13:06 +00:00
|
|
|
func (formatter *TemplateFormatter) Format(captures []string) string {
|
2018-09-22 00:59:11 +00:00
|
|
|
if formatter.captureCount == 0 {
|
|
|
|
// no label substitution, keep as it is
|
|
|
|
return formatter.fmtString
|
|
|
|
}
|
2018-09-28 19:55:53 +00:00
|
|
|
indexes := formatter.captureIndexes
|
|
|
|
vargs := make([]interface{}, formatter.captureCount)
|
|
|
|
for i, idx := range indexes {
|
|
|
|
vargs[i] = captures[idx]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(formatter.fmtString, vargs...)
|
2018-09-22 00:59:11 +00:00
|
|
|
}
|