forked from nginx/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverser.go
99 lines (87 loc) · 1.96 KB
/
traverser.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package sdk
import (
"github.com/nginxinc/nginx-go-crossplane"
)
type (
CrossplaneTraverseCallback = func(parent *crossplane.Directive, current *crossplane.Directive) (bool, error)
CrossplaneTraverseCallbackStr = func(parent *crossplane.Directive, current *crossplane.Directive) string
)
func traverse(root *crossplane.Directive, callback CrossplaneTraverseCallback, stop *bool) error {
if *stop {
return nil
}
for _, child := range root.Block {
result, err := callback(root, child)
if err != nil {
return err
}
if !result {
*stop = true
return nil
}
err = traverse(child, callback, stop)
if err != nil {
return err
}
if *stop {
return nil
}
}
return nil
}
func traverseStr(root *crossplane.Directive, callback CrossplaneTraverseCallbackStr, stop *bool) string {
response := ""
if *stop {
return ""
}
for _, child := range root.Block {
response = callback(root, child)
if response != "" {
*stop = true
return response
}
response = traverseStr(child, callback, stop)
if *stop {
return response
}
}
return response
}
func CrossplaneConfigTraverse(root *crossplane.Config, callback CrossplaneTraverseCallback) error {
stop := false
for _, dir := range root.Parsed {
result, err := callback(nil, dir)
if err != nil {
return err
}
if !result {
return nil
}
err = traverse(dir, callback, &stop)
if err != nil {
return err
}
}
return nil
}
func CrossplaneConfigTraverseStr(root *crossplane.Config, callback CrossplaneTraverseCallbackStr) string {
stop := false
response := ""
for _, dir := range root.Parsed {
response = callback(nil, dir)
if response != "" {
return response
}
response = traverseStr(dir, callback, &stop)
if response != "" {
return response
}
}
return response
}