33 lines
741 B
Go
Raw Normal View History

2020-12-24 12:56:28 +05:30
package replacer
import (
"fmt"
"strings"
)
2020-12-28 01:33:50 +05:30
// Payload marker constants
2020-12-24 12:56:28 +05:30
const (
2020-12-28 01:33:50 +05:30
MarkerGeneral = "§"
MarkerParenthesisOpen = "{{"
MarkerParenthesisClose = "}}"
2020-12-24 12:56:28 +05:30
)
// New creates a new replacer structure for values replacement on the fly.
func New(values map[string]interface{}) *strings.Replacer {
replacerItems := make([]string, 0, len(values)*4)
for key, val := range values {
valueStr := fmt.Sprintf("%s", val)
replacerItems = append(replacerItems,
2020-12-28 01:33:50 +05:30
fmt.Sprintf("%s%s%s", MarkerParenthesisOpen, key, MarkerParenthesisClose),
2020-12-24 12:56:28 +05:30
valueStr,
)
replacerItems = append(replacerItems,
2020-12-28 01:33:50 +05:30
fmt.Sprintf("%s%s%s", MarkerGeneral, key, MarkerGeneral),
2020-12-24 12:56:28 +05:30
valueStr,
)
}
return strings.NewReplacer(replacerItems...)
}