github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/validators/function.go (about) 1 package validators 2 3 import ( 4 "fmt" 5 "github.com/ismailbayram/bigpicture/internal/graph" 6 "strings" 7 ) 8 9 type FunctionValidatorArgs struct { 10 Module string `json:"module" validate:"required=true"` 11 MaxLineCount int `json:"max_line_count" validate:"required=true,gte=1"` 12 Ignore []string `json:"ignore"` 13 } 14 15 type FunctionValidator struct { 16 args *FunctionValidatorArgs 17 tree *graph.Tree 18 } 19 20 func NewFunctionValidator(args map[string]any, tree *graph.Tree) (*FunctionValidator, error) { 21 validatorArgs := &FunctionValidatorArgs{} 22 if err := validateArgs(args, validatorArgs); err != nil { 23 return nil, err 24 } 25 26 module, err := validatePath(validatorArgs.Module, tree) 27 if err != nil { 28 return nil, err 29 } 30 validatorArgs.Module = module 31 32 return &FunctionValidator{ 33 args: validatorArgs, 34 tree: tree, 35 }, nil 36 } 37 38 func (v *FunctionValidator) Validate() error { 39 for _, node := range v.tree.Nodes { 40 if isIgnored(v.args.Ignore, node.Path) { 41 continue 42 } 43 44 if !strings.HasPrefix(node.Path, v.args.Module) { 45 continue 46 } 47 48 for _, function := range node.Functions { 49 if function.LineCount > v.args.MaxLineCount { 50 return fmt.Errorf( 51 "Line count of function '%s' in '%s' is %d, but maximum allowed is %d", 52 function.Name, 53 node.Path, 54 function.LineCount, 55 v.args.MaxLineCount, 56 ) 57 } 58 } 59 } 60 61 return nil 62 }