Passed
Pull Request — main (#49)
by Igor
02:01
created

template.go   A

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
cc 4
eloc 15
dl 0
loc 33
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validation.renderMessage 0 8 2
A validation.TemplateParameterList.Prepend 0 2 1
A validation.TemplateParameterList.With 0 2 1
1
package validation
2
3
import "strings"
4
5
// TemplateParameter is injected into the message while rendering the template.
6
type TemplateParameter struct {
7
	// Key is the marker in the string that will be replaced by value.
8
	// In general, it is recommended to use double curly braces around the key name.
9
	// Example: {{ keyName }}
10
	Key string
11
12
	// Value is set by constraint when building violation.
13
	Value string
14
}
15
16
type TemplateParameterList []TemplateParameter
17
18
func (params TemplateParameterList) With(parameters ...TemplateParameter) TemplateParameterList {
19
	return append(params, parameters...)
20
}
21
22
func (params TemplateParameterList) Prepend(parameters ...TemplateParameter) TemplateParameterList {
23 1
	return append(parameters, params...)
24
}
25
26
func renderMessage(template string, parameters []TemplateParameter) string {
27 1
	message := template
28
29 1
	for _, p := range parameters {
30 1
		message = strings.ReplaceAll(message, p.Key, p.Value)
31
	}
32
33 1
	return message
34
}
35