Passed
Pull Request — main (#46)
by Igor
02:07
created

validation.Scope.withContext   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
nop 1
1
package validation
2
3
import (
4
	"context"
5
	"fmt"
6
7
	languagepkg "github.com/muonsoft/language"
8
	"golang.org/x/text/language"
9
)
10
11
// Scope holds the current state of validation. On the client-side of the package,
12
// it can be used to build violations.
13
type Scope struct {
14
	context          context.Context
15
	propertyPath     *PropertyPath
16
	language         language.Tag
17
	translator       *Translator
18
	violationFactory ViolationFactory
19
	constraints      map[string]Constraint
20
}
21
22
// Context returns context value that was passed to the validator by Context argument or
23
// by creating scoped validator with the validator.WithContext method.
24
func (s Scope) Context() context.Context {
25 1
	return s.context
26
}
27
28
// BuildViolation is used to create violations in validation methods of constraints.
29
// This method automatically injects the property path and language of the current validation scope.
30
func (s Scope) BuildViolation(code, message string) *ViolationBuilder {
31 1
	b := NewViolationBuilder(s.violationFactory).BuildViolation(code, message)
32 1
	b.SetPropertyPath(s.propertyPath)
33
34 1
	if s.language != language.Und {
35 1
		b.SetLanguage(s.language)
36 1
	} else if s.context != nil {
37 1
		b.SetLanguage(languagepkg.FromContext(s.context))
38
	}
39
40 1
	return b
41
}
42
43
func (s *Scope) applyOptions(options ...Option) error {
44 1
	for _, option := range options {
45 1
		var err error
46 1
		if o, ok := option.(internalOption); ok {
47 1
			err = o.setUpOnScope(s)
48
		} else {
49 1
			err = option.SetUp()
50
		}
51 1
		if err != nil {
52 1
			return s.describeOptionError(option, err)
53
		}
54
	}
55
56 1
	return nil
57
}
58
59
func (s *Scope) describeOptionError(option Option, err error) error {
60 1
	c, ok := option.(Constraint)
61 1
	if !ok {
62
		return fmt.Errorf(`failed to set up option: %w`, err)
63
	}
64
65 1
	if s.propertyPath == nil {
66 1
		err = fmt.Errorf(`failed to set up constraint "%s": %w`, c.Name(), err)
67
	} else {
68 1
		err = fmt.Errorf(`failed to set up constraint "%s" at path "%s": %w`, c.Name(), s.propertyPath.String(), err)
69
	}
70
71 1
	return err
72
}
73
74
func (s Scope) withContext(ctx context.Context) Scope {
75 1
	s.context = ctx
76
77 1
	return s
78
}
79
80
func (s Scope) withLanguage(tag language.Tag) Scope {
81 1
	s.language = tag
82
83 1
	return s
84
}
85
86
func (s Scope) atProperty(name string) Scope {
87 1
	s.propertyPath = s.propertyPath.WithProperty(name)
88
89 1
	return s
90
}
91
92
func (s Scope) atIndex(index int) Scope {
93 1
	s.propertyPath = s.propertyPath.WithIndex(index)
94
95 1
	return s
96
}
97
98
func newScope() Scope {
99 1
	translator := newTranslator()
100
101 1
	return Scope{
102
		context:          context.Background(),
103
		translator:       translator,
104
		violationFactory: newViolationFactory(translator),
105
		constraints:      make(map[string]Constraint),
106
	}
107
}
108