-
Notifications
You must be signed in to change notification settings - Fork 14
/
option.go
154 lines (131 loc) · 3.61 KB
/
option.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//go:generate stringer -type=Option -output=./option_string.go
package buffer
import (
"reflect"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/avenga/couper/config/body"
"github.com/avenga/couper/eval/attributes"
"github.com/avenga/couper/eval/variables"
)
type Option uint8
const (
None Option = iota
Request
Response
)
func (i Option) GoString() string {
var result []string
for _, o := range []Option{Request, Response} {
if (i & o) == o {
result = append(result, o.String())
}
}
if len(result) == 0 {
return None.String()
}
return strings.Join(result, "|")
}
func (i Option) Response() bool {
return i&Response == Response
}
// Must determine if any of the hcl.bodies makes use of 'body', 'form_body' or 'json_body' or
// of known attributes and variables which require a parsed client-request or backend-response body.
func Must(bodies ...hcl.Body) Option {
result := None
if len(bodies) == 0 {
return result
}
var allExprs []hclsyntax.Expression
allAttributes := body.CollectAttributes(bodies...)
allBlockTypes := body.CollectBlockTypes(bodies...)
for _, blockType := range allBlockTypes {
if opt := bufferWithBlock(blockType); opt != None {
result |= opt
}
}
// TODO: follow func call and their referenced remains
for _, attr := range allAttributes {
allExprs = append(allExprs, attr.Expr)
if opt := bufferWithAttribute(attr.Name); opt != None {
result |= opt
}
}
for _, expr := range allExprs {
for _, traversal := range expr.Variables() {
rootName := traversal.RootName()
if len(traversal) == 1 {
if rootName == variables.ClientRequest || rootName == variables.BackendRequest {
result |= Request
}
if rootName == variables.BackendResponses || rootName == variables.BackendResponse {
result |= Response
}
continue
}
if rootName != variables.ClientRequest && rootName != variables.BackendRequest &&
rootName != variables.BackendResponses && rootName != variables.BackendResponse {
continue
}
for _, t := range traversal[1:] {
nameField := reflect.ValueOf(t).FieldByName("Name")
name := nameField.String()
switch name {
case variables.Body:
switch rootName {
case variables.ClientRequest:
fallthrough
case variables.BackendRequest:
result |= Request
case variables.BackendResponse:
fallthrough
case variables.BackendResponses:
result |= Response
}
case variables.CTX: // e.g. jwt token (value) could be read from any (body) source
if rootName == variables.ClientRequest || rootName == variables.BackendRequest {
result |= Request
}
case variables.FormBody:
if rootName == variables.ClientRequest || rootName == variables.BackendRequest {
result |= Request
}
case variables.JSONBody:
switch rootName {
case variables.ClientRequest:
fallthrough
case variables.BackendRequest:
result |= Request
case variables.BackendResponse:
fallthrough
case variables.BackendResponses:
result |= Response
}
default:
// e.g. backend_responses.default
if len(traversal) == 2 {
if rootName == variables.BackendResponse || rootName == variables.BackendResponses {
result |= Response
}
}
}
}
}
}
return result
}
func bufferWithAttribute(attrName string) Option {
switch attrName {
case attributes.AddFormParams, attributes.SetFormParams, attributes.DelFormParams:
return Request
}
return None
}
func bufferWithBlock(name string) Option {
switch name {
case "openapi":
return Response
}
return None
}