Skip to content

Commit

Permalink
Merge branch 'master' into release.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Jun 8, 2022
2 parents 9f9f142 + dbac8a3 commit cd55778
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ issues.
* Spurious `duplicate endpoint /**` error for APIs sharing the same base path ([#507](https://github.com/avenga/couper/pull/507))
* Invalid (by [OpenAPI validation](./docs/REFERENCE.md#openapi-block)) backend response missing in [`backend_responses`](./docs/REFERENCE.md#backend_responses) ([#501](https://github.com/avenga/couper/pull/501))
* Ignore the `expected_status` check for a request configured via a [`proxy`](./docs/REFERENCE.md#proxy-block) or [`request`](./docs/REFERENCE.md#request-block) block if a [`backend` error](./docs/ERRORS.md#endpoint-error-types) occured ([#505](https://github.com/avenga/couper/pull/505))
* `merge()` function removes key with `null` value. ([#518](https://github.com/avenga/couper/pull/518))

* **Removed**
* support for `beta_oidc` block (use [`oidc` block](./docs/REFERENCE.md#oidc-block) instead) ([#475](https://github.com/avenga/couper/pull/475))
Expand Down
10 changes: 3 additions & 7 deletions eval/lib/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/zclconf/go-cty/cty/function"
)

var (
MergeFunc = newMergeFunction()
)
var MergeFunc = newMergeFunction()

func newMergeFunction() function.Function {
return function.New(&function.Spec{
Expand Down Expand Up @@ -76,9 +74,7 @@ func mergeObjects(args []cty.Value) cty.Value {

for it := arg.ElementIterator(); it.Next(); {
k, v := it.Element()
if v.IsNull() {
delete(outputMap, k.AsString())
} else if existingVal, ok := outputMap[k.AsString()]; !ok {
if existingVal, ok := outputMap[k.AsString()]; !ok {
// key not set
outputMap[k.AsString()] = v
} else if vType := v.Type(); vType.IsPrimitiveType() {
Expand All @@ -97,7 +93,7 @@ func mergeObjects(args []cty.Value) cty.Value {
}

func mergeTuples(args []cty.Value) cty.Value {
outputList := []cty.Value{}
var outputList []cty.Value
for _, arg := range args {
if arg.IsNull() {
continue
Expand Down
34 changes: 22 additions & 12 deletions eval/lib/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func TestMerge(t *testing.T) {
cf, err := configload.LoadBytes([]byte(`server "test" {}`), "couper.hcl")
helper.Must(err)

hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext()
mergeFn := hclContext.Functions["merge"]

tests := []struct {
name string
args []cty.Value
Expand Down Expand Up @@ -144,34 +147,44 @@ func TestMerge(t *testing.T) {
/*
merge(
{"k2": {"k2.2": 2}},
{"k2": null},
{"k2": {"k4.2": 4}}
{"k0": null},
{"k2": {"k4.2": 4}},
{"k2": {"k4.0": null}}
)
*/
"merge objects with tombstone null",
"merge objects with null",
[]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"k2": cty.ObjectVal(map[string]cty.Value{
"k2.2": cty.NumberIntVal(2),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"k2": cty.NullVal(cty.Bool),
"k0": cty.NullVal(cty.Bool),
}),
cty.ObjectVal(map[string]cty.Value{
"k2": cty.MapVal(map[string]cty.Value{
"k4.2": cty.NumberIntVal(4),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"k2": cty.MapVal(map[string]cty.Value{
"k4.0": cty.NullVal(cty.Bool),
}),
}),
},
/*
{
"k0": null,
"k2": {"k4.2": 4}
}
*/
cty.ObjectVal(map[string]cty.Value{
"k2": cty.MapVal(map[string]cty.Value{
"k0": cty.NullVal(cty.Bool),
"k2": cty.ObjectVal(map[string]cty.Value{
"k2.2": cty.NumberIntVal(2),
"k4.2": cty.NumberIntVal(4),
"k4.0": cty.NullVal(cty.Bool),
}),
}),
},
Expand Down Expand Up @@ -264,17 +277,14 @@ func TestMerge(t *testing.T) {
},
}

hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext()

for _, tt := range tests {
t.Run(tt.name, func(subT *testing.T) {
helper := test.New(subT)

mergedV, err := hclContext.Functions["merge"].Call(tt.args)
helper.Must(err)
h := test.New(subT)

mergedV, merr := mergeFn.Call(tt.args)
h.Must(merr)
if !mergedV.RawEquals(tt.want) {
subT.Errorf("Wrong return value; expected %#v, got: %#v", tt.want, mergedV)
subT.Errorf("Wrong return value:\nwant:\t%#v\ngot:\t%#v\n", tt.want, mergedV)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5208,7 +5208,7 @@ func TestEndpoint_ResponseNilEvaluation(t *testing.T) {
{"/conditional/nested", true, ""},
{"/conditional/nested/true", true, ""},
{"/conditional/nested/false", true, ""},
{"/functions/arg-items", true, `{"foo":"bar","obj":{"key":"val"}}`},
{"/functions/arg-items", true, `{"foo":"bar","obj":{"key":"val"},"xxxx":null}`},
{"/functions/tuple-expr", true, `{"array":["a","b"]}`},
{"/rte1", true, "2"},
{"/rte2", true, "2"},
Expand Down

0 comments on commit cd55778

Please sign in to comment.