Commit 03e220e
committed
feat(resolv): make the
BREAKING CHANGE: removed the `Query` related methods of the `Context` interface and added the `PosSet` interface.
The `Query` related methods of the `Context` interface has been removed as follows:
Methods of deletion to Context:
```go
type Context interface {
...
QueryByKeyWords(kw KeyWords) Pos
QueryAllByKeyWords(kw KeyWords) []Pos
...
}
```
Methods of addition to Context Interface:
```go
type Context interface {
...
ChildrenPosSet() PosSet
...
}
```
Added the `PosSet` interface as follows:
Interface of addition:
```go
type PosSet interface {
Filter(fn func(pos Pos) bool) PosSet
Map(fn func(pos Pos) (Pos, error)) PosSet
MapToPosSet(fn func(pos Pos) PosSet) PosSet
QueryOne(kw KeyWords) Pos
QueryAll(kw KeyWords) PosSet
List() []Pos
Targets() []Context
Append(pos ...Pos) PosSet
AppendWithPosSet(posSet PosSet) PosSet
Error() error
}
```
Methods of addition to Pos Interface:
```go
type Pos interface {
...
QueryOne(kw KeyWords) Pos
QueryAll(kw KeyWords) PosSet
}
```
To migrate the code for the `Context` operations, follow the example below:
Before:
```go
package main
import (
"fmt"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
)
func main() {
conf, err := configuration.NewNginxConfigFromJsonBytes(jsondata)
if err != nil {
panic(err)
}
for _, pos := range conf.Main().QueryByKeyWords(context.NewKeyWords(context_type.TypeHttp).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeServer).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Target().
QueryAllByKeyWords(context.NewKeyWords(context_type.TypeDirective).
SetCascaded(false).
SetStringMatchingValue("server_name test1.com").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)) { // query `server` context, its server name is "test1.com"
server, _ := pos.Position()
if server.QueryByKeyWords(context.NewKeyWords(context_type.TypeDirective).
SetCascaded(false).
SetRegexpMatchingValue("^listen 80$").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Target().Error() != nil { // query `server` context, its listen port is 80
continue
}
// query the "proxy_pass" `directive` context, which is in `if` context(value: "($http_api_name != '')") and `location` context(value: "/test1-location")
ctx, idx := server.QueryByKeyWords(context.NewKeyWords(context_type.TypeLocation).
SetRegexpMatchingValue(`^/test1-location$`).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeIf).
SetRegexpMatchingValue(`^\(\$http_api_name != ''\)$`).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Target().
QueryByKeyWords(context.NewKeyWords(context_type.TypeDirective).
SetStringMatchingValue("proxy_pass").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).Position()
// insert an inline comment after the "proxy_pass" `directive` context
err = ctx.Insert(local.NewContext(context_type.TypeInlineComment, fmt.Sprintf("[%s]test comments", time.Now().String())), idx+1).Error()
if err != nil {
panic(err)
}
}
}
```
After:
```go
package main
import (
"fmt"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local"
)
func main() {
conf, err := configuration.NewNginxConfigFromJsonBytes(jsondata)
if err != nil {
panic(err)
}
ctx, idx := conf.Main().ChildrenPosSet().
QueryOne(nginx_ctx.NewKeyWords(context_type.TypeHttp).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
QueryAll(nginx_ctx.NewKeyWords(context_type.TypeServer).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
Filter( // filter out `server` context positions, theirs server name is "test1.com"
func(pos nginx_ctx.Pos) bool {
return pos.QueryOne(context.NewKeyWords(context_type.TypeDirective).
SetCascaded(false).
SetStringMatchingValue("server_name test1.com").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
Target().Error() == nil
},
).
Filter( // filter out `server` context positions, theirs listen port is 80
func(pos nginx_ctx.Pos) bool {
return pos.QueryOne(context.NewKeyWords(context_type.TypeDirective).
SetCascaded(false).
SetRegexpMatchingValue("^listen 80$").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
Target().Error() == nil
},
).
// query the "proxy_pass" `directive` context position, which is in `if` context(value: "($http_api_name != '')") and `location` context(value: "/test1-location")
QueryOne(nginx_ctx.NewKeyWords(context_type.TypeLocation).
SetRegexpMatchingValue(`^/test1-location$`).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
QueryOne(nginx_ctx.NewKeyWords(context_type.TypeIf).
SetRegexpMatchingValue(`^\(\$http_api_name != ''\)$`).
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
QueryOne(nginx_ctx.NewKeyWords(context_type.TypeDirective).
SetStringMatchingValue("proxy_pass").
SetSkipQueryFilter(context.SkipDisabledCtxFilterFunc)).
Position()
// insert an inline comment after the "proxy_pass" `directive` context
err = ctx.Insert(local.NewContext(context_type.TypeInlineComment, fmt.Sprintf("[%s]test comments", time.Now().String())), idx+1).Error()
if err != nil {
panic(err)
}
}
```Context more supportive of functional-style operations on streams of elements1 parent d1919fe commit 03e220e
File tree
34 files changed
+652
-734
lines changed- internal
- bifrost/transport/v1/fake
- pkg/auth
- daemon
- service
- pkg
- client/bifrost/v1/transport
- json/nginx
- resolv
- V2
- nginx/configuration
- utils
- V3/nginx/configuration
- context
- local
- nginx
- test/grpc_client
- bifrost
- health_check
34 files changed
+652
-734
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
294 | 316 | | |
295 | 317 | | |
296 | 318 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
94 | | - | |
| 95 | + | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
117 | 119 | | |
118 | 120 | | |
119 | 121 | | |
120 | | - | |
| 122 | + | |
| 123 | + | |
121 | 124 | | |
122 | 125 | | |
123 | 126 | | |
| |||
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
149 | | - | |
| 152 | + | |
| 153 | + | |
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
| |||
158 | 162 | | |
159 | 163 | | |
160 | 164 | | |
161 | | - | |
| 165 | + | |
| 166 | + | |
162 | 167 | | |
163 | 168 | | |
164 | 169 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
18 | 20 | | |
19 | | - | |
20 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
36 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
37 | 42 | | |
38 | | - | |
39 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
40 | 46 | | |
41 | 47 | | |
42 | 48 | | |
| |||
50 | 56 | | |
51 | 57 | | |
52 | 58 | | |
53 | | - | |
54 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
| |||
74 | 81 | | |
75 | 82 | | |
76 | 83 | | |
77 | | - | |
78 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
79 | 87 | | |
80 | 88 | | |
81 | 89 | | |
| |||
104 | 112 | | |
105 | 113 | | |
106 | 114 | | |
107 | | - | |
| 115 | + | |
| 116 | + | |
108 | 117 | | |
109 | 118 | | |
110 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | | - | |
31 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
45 | 50 | | |
46 | | - | |
47 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
| |||
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
82 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
83 | 91 | | |
84 | | - | |
| 92 | + | |
| 93 | + | |
85 | 94 | | |
86 | 95 | | |
87 | 96 | | |
| |||
111 | 120 | | |
112 | 121 | | |
113 | 122 | | |
114 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
115 | 126 | | |
116 | | - | |
117 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
118 | 130 | | |
119 | 131 | | |
120 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
| 364 | + | |
364 | 365 | | |
365 | 366 | | |
366 | 367 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | | - | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
62 | | - | |
63 | | - | |
64 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
65 | 70 | | |
66 | | - | |
| 71 | + | |
| 72 | + | |
67 | 73 | | |
68 | 74 | | |
69 | 75 | | |
| |||
0 commit comments