-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.go
38 lines (32 loc) · 1006 Bytes
/
helper.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
36
37
38
package valigo
import (
"context"
"github.com/insei/fmap/v3"
"github.com/insei/valigo/shared"
"github.com/insei/valigo/translator"
)
// helper is a struct that provides functionality for handling errors and translations.
type helper struct {
t translator.Translator
getFieldLocation func(field fmap.Field) string
}
// ErrorT returns a shared.Error with the given location, message, and value.
// The message is translated using the translator.
func (h *helper) ErrorT(ctx context.Context, field fmap.Field, value any, localeKey string, args ...any) shared.Error {
location := h.getFieldLocation(field)
msg := h.t.T(ctx, localeKey, args...)
return shared.Error{
Location: location,
Message: msg,
Value: value,
}
}
// newHelper returns a new helper with a default translator and getFieldLocation function.
func newHelper() *helper {
return &helper{
t: translator.New(),
getFieldLocation: func(field fmap.Field) string {
return field.GetStructPath()
},
}
}