summaryrefslogtreecommitdiff
path: root/internal/configuration/schema/validator.go
diff options
context:
space:
mode:
authorJames Elliott <james-d-elliott@users.noreply.github.com>2021-11-23 20:45:38 +1100
committerGitHub <noreply@github.com>2021-11-23 20:45:38 +1100
commit3695aa8140eb91fd54a4cd849e1340ad4c36d987 (patch)
treee2cbb84db06b8058dc89ba9c616f016a223e6e67 /internal/configuration/schema/validator.go
parent884dc99083ba280d1a93103c4e16d4446ff7fdcc (diff)
feat(storage): primary key for all tables and general qol refactoring (#2431)
This is a massive overhaul to the SQL Storage for Authelia. It facilitates a whole heap of utility commands to help manage the database, primary keys, ensures all database requests use a context for cancellations, and paves the way for a few other PR's which improve the database. Fixes #1337
Diffstat (limited to 'internal/configuration/schema/validator.go')
-rw-r--r--internal/configuration/schema/validator.go101
1 files changed, 0 insertions, 101 deletions
diff --git a/internal/configuration/schema/validator.go b/internal/configuration/schema/validator.go
index a37e3eec5..ae16911f7 100644
--- a/internal/configuration/schema/validator.go
+++ b/internal/configuration/schema/validator.go
@@ -1,12 +1,5 @@
package schema
-import (
- "fmt"
- "reflect"
-
- "github.com/Workiva/go-datastructures/queue"
-)
-
// ErrorContainer represents a container where we can add errors and retrieve them.
type ErrorContainer interface {
Push(err error)
@@ -17,100 +10,6 @@ type ErrorContainer interface {
Warnings() []error
}
-// Validator represents the validator interface.
-type Validator struct {
- errors map[string][]error
-}
-
-// NewValidator create a validator.
-func NewValidator() *Validator {
- validator := new(Validator)
- validator.errors = make(map[string][]error)
-
- return validator
-}
-
-// QueueItem an item representing a struct field and its path.
-type QueueItem struct {
- value reflect.Value
- path string
-}
-
-func (v *Validator) validateOne(item QueueItem, q *queue.Queue) error { //nolint:unparam
- if item.value.Type().Kind() == reflect.Ptr {
- if item.value.IsNil() {
- return nil
- }
-
- elem := item.value.Elem()
-
- q.Put(QueueItem{ //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
- value: elem,
- path: item.path,
- })
- } else if item.value.Kind() == reflect.Struct {
- numFields := item.value.Type().NumField()
-
- validateFn := item.value.Addr().MethodByName("Validate")
-
- if validateFn.IsValid() {
- structValidator := NewStructValidator()
- validateFn.Call([]reflect.Value{reflect.ValueOf(structValidator)})
- v.errors[item.path] = structValidator.Errors()
- }
-
- for i := 0; i < numFields; i++ {
- field := item.value.Type().Field(i)
- value := item.value.Field(i)
-
- q.Put(QueueItem{ //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
- value: value,
- path: item.path + "." + field.Name,
- })
- }
- }
-
- return nil
-}
-
-// Validate validate a struct.
-func (v *Validator) Validate(s interface{}) error {
- q := queue.New(40)
- q.Put(QueueItem{value: reflect.ValueOf(s), path: "root"}) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
-
- for !q.Empty() {
- val, err := q.Get(1)
- if err != nil {
- return err
- }
-
- item, ok := val[0].(QueueItem)
- if !ok {
- return fmt.Errorf("Cannot convert item into QueueItem")
- }
-
- v.validateOne(item, q) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
- }
-
- return nil
-}
-
-// PrintErrors display the errors thrown during validation.
-func (v *Validator) PrintErrors() {
- for path, errs := range v.errors {
- fmt.Printf("Errors at %s:\n", path)
-
- for _, err := range errs {
- fmt.Printf("--> %s\n", err)
- }
- }
-}
-
-// Errors return the errors thrown during validation.
-func (v *Validator) Errors() map[string][]error {
- return v.errors
-}
-
// StructValidator is a validator for structs.
type StructValidator struct {
errors []error