big refacto

This commit is contained in:
2023-10-03 00:40:01 +02:00
parent aa722718f7
commit 932f423faf
35 changed files with 354 additions and 934 deletions

View File

@@ -0,0 +1,29 @@
package utils
import "fmt"
type Injector struct {
content map[string]any
}
func (i *Injector) Get(key string) any {
val, ok := i.content[key]
if !ok {
panic(fmt.Sprintf("Can't get key %s from injector", key))
}
return val
}
func Get[T any](i *Injector, key string) T {
return i.Get(key).(T)
}
func (i *Injector) Set(key string, content any) {
if i.content == nil {
i.content = map[string]any{}
}
_, ok := i.content[key]
if ok {
panic(fmt.Sprintf("Key %s already have content", key))
}
i.content[key] = content
}

View File

@@ -0,0 +1,12 @@
package validator
import (
"nos-comptes/internal/utils"
)
const ValidatorInjectorKey = "VALIDATOR"
func Setup(injector *utils.Injector) {
injector.Set(ValidatorInjectorKey, newValidator())
}

View File

@@ -0,0 +1,28 @@
package validator
import (
"gopkg.in/go-playground/validator.v9"
"nos-comptes/internal/storage/validators"
"reflect"
"strings"
)
func newValidator() *validator.Validate {
va := validator.New()
va.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)
if len(name) < 1 {
return ""
}
return name[0]
})
for k, v := range validators.CustomValidators {
if v.Validator != nil {
va.RegisterValidationCtx(k, v.Validator)
}
}
return va
}