-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_names_validator.go
52 lines (39 loc) · 1.04 KB
/
sorted_names_validator.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package nit
import (
"go/ast"
"github.com/pkg/errors"
)
type (
sortedNamesValidator struct {
identType string
exported *bool
last string
}
)
func (v *sortedNamesValidator) validateName(errPrefix string, name *ast.Ident) error {
if err := v.validateExported(errPrefix, name); err != nil {
return err
}
if err := v.validateSortedName(errPrefix, name); err != nil {
return err
}
return nil
}
//-
func (v *sortedNamesValidator) validateExported(errPrefix string, name *ast.Ident) error {
if v.exported == nil || (*v.exported && !name.IsExported()) {
e := name.IsExported()
v.exported = &e
}
if *v.exported != name.IsExported() {
return errors.Wrap(errors.Errorf("%s `%s` is not grouped correctly", v.identType, name.Name), errPrefix)
}
return nil
}
func (v *sortedNamesValidator) validateSortedName(errPrefix string, name *ast.Ident) error {
if v.last != "" && v.last > name.Name {
return errors.Wrap(errors.Errorf("%s `%s` is not sorted", v.identType, name.Name), errPrefix)
}
v.last = name.Name
return nil
}