Skip to content

Commit

Permalink
Merge pull request #389 from avenga/indexes-unary
Browse files Browse the repository at this point in the history
RelativeTraversalExpr, IndexExpr, UnaryOpExpr
  • Loading branch information
Marcel Ludwig authored Nov 23, 2021
2 parents d7ffacb + 552b450 commit 5faaf09
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Unreleased changes are available as `avenga/couper:edge` container.
* Reduced memory usage for backend response bodies which just get piped to the client and are not required to be read by Couper due to a variable references ([#375](https://github.com/avenga/couper/pull/375))
* However, if a huge message body is passed and additionally referenced via e.g. `json_body`, Couper may require a lot of memory for storing the data structure.
* For each SAML attribute listed in [`array_attributes`](./docs/REFERENCE.md#saml-block) at least an empty array is created in `request.context.<label>.attributes.<name>` ([#369](https://github.com/avenga/couper/pull/369))
* Missing support for RelativeTraversalExpr, IndexExpr, UnaryOpExpr ([#389](https://github.com/avenga/couper/pull/389))

---

Expand Down
42 changes: 42 additions & 0 deletions eval/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ func newLiteralValueExpr(ctx *hcl.EvalContext, exp hcl.Expression) hclsyntax.Exp
expr.Each = newLiteralValueExpr(ctx, expr.Each)
expr.Source = newLiteralValueExpr(ctx, expr.Source)
return expr
case *hclsyntax.IndexExpr:
if val := newLiteralValueExpr(ctx, expr.Collection); val != nil {
expr.Collection = val
}
if val := newLiteralValueExpr(ctx, expr.Key); val != nil {
expr.Key = val
}
return expr
case *hclsyntax.RelativeTraversalExpr:
if val := newLiteralValueExpr(ctx, expr.Source); val != nil {
expr.Source = val
}
return expr
case *hclsyntax.UnaryOpExpr:
if val := newLiteralValueExpr(ctx, expr.Val); val != nil {
expr.Val = val
}
return expr
case *hclsyntax.AnonSymbolExpr:
return expr
case *hclsyntax.LiteralValueExpr:
Expand Down Expand Up @@ -235,6 +253,30 @@ func clone(exp hcl.Expression) hclsyntax.Expression {
ex := *expr
ex.Source = clone(expr.Source)
return &ex
case *hclsyntax.IndexExpr:
ex := &hclsyntax.IndexExpr{
Collection: clone(expr.Collection),
Key: clone(expr.Key),
SrcRange: expr.SrcRange,
OpenRange: expr.OpenRange,
BracketRange: expr.BracketRange,
}
return ex
case *hclsyntax.RelativeTraversalExpr:
ex := &hclsyntax.RelativeTraversalExpr{
Source: clone(expr.Source),
Traversal: expr.Traversal,
SrcRange: expr.SrcRange,
}
return ex
case *hclsyntax.UnaryOpExpr:
ex := &hclsyntax.UnaryOpExpr{
Op: expr.Op,
Val: clone(expr.Val),
SrcRange: expr.SrcRange,
SymbolRange: expr.SymbolRange,
}
return ex
case *hclsyntax.AnonSymbolExpr:
return &hclsyntax.AnonSymbolExpr{SrcRange: expr.SrcRange}
default:
Expand Down
6 changes: 6 additions & 0 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4281,6 +4281,12 @@ func TestEndpoint_ResponseNilEvaluation(t *testing.T) {
{"/conditional/nested/false", true, ""},
{"/functions/arg-items", true, `{"foo":"bar","obj":{"key":"val"}}`},
{"/functions/tuple-expr", true, `{"array":["a","b"]}`},
{"/rte1", true, "2"},
{"/rte2", true, "2"},
{"/ie1", true, "2"},
{"/ie2", true, "2"},
{"/uoe1", true, "-2"},
{"/uoe2", true, "true"},
} {
t.Run(tc.path[1:], func(subT *testing.T) {
helper := test.New(subT)
Expand Down
60 changes: 60 additions & 0 deletions server/testdata/integration/endpoint_eval/20_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,66 @@ server "cty.NilVal" {
}
}
}

endpoint "/rte1" {
response {
# *hclsyntax.RelativeTraversalExpr
headers = {
X-Value = [0, 2, 4][1]
Z-Value = "y"
}
}
}

endpoint "/rte2" {
response {
# *hclsyntax.RelativeTraversalExpr
headers = {
X-Value = {a = 2, b = 4}["a"]
Z-Value = "y"
}
}
}

endpoint "/ie1" {
response {
# *hclsyntax.IndexExpr
headers = {
X-Value = [0, 2, 4][2 - 1]
Z-Value = "y"
}
}
}

endpoint "/ie2" {
response {
# *hclsyntax.IndexExpr
headers = {
X-Value = {"/ie1" = 1, "/ie2" = 2}[request.path]
Z-Value = "y"
}
}
}

endpoint "/uoe1" {
response {
# *hclsyntax.UnaryOpExpr
headers = {
X-Value = -2
Z-Value = "y"
}
}
}

endpoint "/uoe2" {
response {
# *hclsyntax.UnaryOpExpr
headers = {
X-Value = json_encode(!false)
Z-Value = "y"
}
}
}
}

settings {
Expand Down

0 comments on commit 5faaf09

Please sign in to comment.