-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct_walker_access.go
42 lines (35 loc) · 1.21 KB
/
struct_walker_access.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
package gocast
import "context"
type (
structWalkerFunc func(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) error
structWalkerNameFunc func(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) string
)
// StructWalkOptions is the options for StructWalk
type StructWalkOptions struct {
pathTag string
pathExtractor structWalkerNameFunc
}
// PathName returns the path name of the current field
func (w *StructWalkOptions) PathName(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) string {
if w != nil && w.pathExtractor != nil {
return w.pathExtractor(ctx, curObj, field, path)
}
if w != nil && w.pathTag != "" {
return field.Tag(w.pathTag)
}
return field.Name()
}
// WalkOption is the option for StructWalk
type WalkOption func(w *StructWalkOptions)
// WalkWithPathExtractor sets the path extractor for StructWalk
func WalkWithPathExtractor(fn structWalkerNameFunc) WalkOption {
return func(w *StructWalkOptions) {
w.pathExtractor = fn
}
}
// WalkWithPathTag sets the path tag for StructWalk
func WalkWithPathTag(tagName string) WalkOption {
return func(w *StructWalkOptions) {
w.pathTag = tagName
}
}