Passed
Push — main ( 0f6dda...8fe6b9 )
by Igor
02:04 queued 11s
created

validation.TemplateParameterList.Prepend   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
// TemplateParameterList is a list of template parameters that can be injection into violation message.
17
type TemplateParameterList []TemplateParameter
18
19
// Prepend returns TemplateParameterList prepended by given parameters.
20
func (params TemplateParameterList) Prepend(parameters ...TemplateParameter) TemplateParameterList {
21 1
	return append(parameters, params...)
22
}
23
24
func renderMessage(template string, parameters []TemplateParameter) string {
25 1
	message := template
26
27 1
	for _, p := range parameters {
28 1
		message = strings.ReplaceAll(message, p.Key, p.Value)
29
	}
30
31 1
	return message
32
}
33