-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.go
35 lines (26 loc) · 1.06 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package validation
import "strings"
// TemplateParameter is injected into the message while rendering the template.
type TemplateParameter struct {
// Key is the marker in the string that will be replaced by value.
// In general, it is recommended to use double curly braces around the key name.
// Example: {{ keyName }}
Key string
// Value is set by constraint when building violation.
Value string
// NeedsTranslation marks that the template value needs to be translated.
NeedsTranslation bool
}
// TemplateParameterList is a list of template parameters that can be injection into violation message.
type TemplateParameterList []TemplateParameter
// Prepend returns [TemplateParameterList] prepended by given parameters.
func (params TemplateParameterList) Prepend(parameters ...TemplateParameter) TemplateParameterList {
return append(parameters, params...)
}
func renderMessage(template string, parameters []TemplateParameter) string {
message := template
for _, p := range parameters {
message = strings.ReplaceAll(message, p.Key, p.Value)
}
return message
}