-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_field_func.go
More file actions
33 lines (27 loc) · 1.17 KB
/
Copy pathcheck_field_func.go
File metadata and controls
33 lines (27 loc) · 1.17 KB
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
// Copyright (c) 2023-2026 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package v2
import (
"fmt"
"reflect"
)
// CheckFieldFunc is a function that takes the parent struct's reflect.Value along with
// the current field's reflect.Value, performs a check that may depend on a sibling
// field, and returns the resulting value and any error that occurred. The parent is
// only valid when the check is run through CheckStruct.
type CheckFieldFunc func(parent, value reflect.Value) (reflect.Value, error)
// lookupParentField returns the dereferenced value of the named field on the parent
// struct. It panics if the parent is not a valid struct context, or if the named
// field cannot be found, as both indicate a configuration error.
func lookupParentField(checkerName string, parent reflect.Value, name string) reflect.Value {
if !parent.IsValid() {
panic(fmt.Sprintf("%s requires a struct context, use CheckStruct", checkerName))
}
field := parent.FieldByName(name)
if !field.IsValid() {
panic(fmt.Sprintf("field %s not found", name))
}
return reflect.Indirect(field)
}