-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determine buffering if req or beresp json_body response occurs in hcl…
….bodies #44
- Loading branch information
Marcel Ludwig
committed
Oct 9, 2020
1 parent
2f002d2
commit 98909db
Showing
3 changed files
with
109 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package eval | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
type BufferOption uint8 | ||
|
||
const ( | ||
BufferNone BufferOption = iota | ||
BufferRequest | ||
BufferResponse | ||
) | ||
|
||
// MustBuffer determines if any of the hcl.bodies makes use of 'post' or 'json_body'. | ||
func MustBuffer(ctxBodies []hcl.Body) BufferOption { | ||
result := BufferNone | ||
for _, body := range ctxBodies { | ||
attrs, err := body.JustAttributes() | ||
if err != nil { | ||
return result | ||
} | ||
for _, attr := range attrs { | ||
for _, traversal := range attr.Expr.Variables() { | ||
rootName := traversal.RootName() | ||
if rootName != "req" && rootName != "beresp" { | ||
continue | ||
} | ||
for _, step := range traversal[1:] { | ||
nameField := reflect.ValueOf(step).FieldByName("Name") | ||
name := nameField.String() | ||
switch name { | ||
case "json_body": | ||
switch rootName { | ||
case "req": | ||
result |= BufferRequest | ||
case "beresp": | ||
result |= BufferResponse | ||
} | ||
case "post": | ||
if rootName == "req" { | ||
result |= BufferRequest | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters