From 5d33dcc599e2d0d7b24882b7ae0a3804c24f98ad Mon Sep 17 00:00:00 2001 From: Kostas Mavrikis Date: Fri, 22 Oct 2021 14:45:04 +0200 Subject: [PATCH 1/4] Register default function as coalesce alias --- docs/REFERENCE.md | 2 +- eval/context.go | 1 + server/http_integration_test.go | 1 + server/testdata/integration/functions/01_couper.hcl | 11 +++++++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md index b4a824b24..7f6056714 100644 --- a/docs/REFERENCE.md +++ b/docs/REFERENCE.md @@ -663,7 +663,7 @@ To access the HTTP status code of the `default` response use `backend_responses. | :----------------------------- | :-------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------- | :--------------------------------------------------- | | `base64_decode` | string | Decodes Base64 data, as specified in RFC 4648. | `encoded` (string) | `base64_decode("Zm9v")` | | `base64_encode` | string | Encodes Base64 data, as specified in RFC 4648. | `decoded` (string) | `base64_encode("foo")` | -| `coalesce` | | Returns the first of the given arguments that is not null. | `arg...` (various) | `coalesce(request.cookies.foo, "bar")` | +| `default` | string | Returns the first of the given arguments that is not null. | `arg...` (various) | `default(request.cookies.foo, "bar")` | | `json_decode` | various | Parses the given JSON string and, if it is valid, returns the value it represents. | `encoded` (string) | `json_decode("{\"foo\": 1}")` | | `json_encode` | string | Returns a JSON serialization of the given value. | `val` (various) | `json_encode(request.context.myJWT)` | | `jwt_sign` | string | jwt_sign creates and signs a JSON Web Token (JWT) from information from a referenced [JWT Signing Profile Block](#jwt-signing-profile-block) (or [JWT Block](#jwt-block) with `signing_ttl`) and additional claims provided as a function parameter. | `label` (string), `claims` (object) | `jwt_sign("myJWT")` | diff --git a/eval/context.go b/eval/context.go index 54c63ff15..839f07d33 100644 --- a/eval/context.go +++ b/eval/context.go @@ -494,6 +494,7 @@ func newFunctionsMap() map[string]function.Function { "base64_decode": lib.Base64DecodeFunc, "base64_encode": lib.Base64EncodeFunc, "coalesce": lib.CoalesceFunc, + "default": lib.CoalesceFunc, "json_decode": stdlib.JSONDecodeFunc, "json_encode": stdlib.JSONEncodeFunc, "merge": lib.MergeFunc, diff --git a/server/http_integration_test.go b/server/http_integration_test.go index b5a2d052a..f386234f2 100644 --- a/server/http_integration_test.go +++ b/server/http_integration_test.go @@ -3305,6 +3305,7 @@ func TestFunctions(t *testing.T) { for _, tc := range []testCase{ {"merge", "/v1/merge", map[string]string{"X-Merged-1": "{\"foo\":[1,2]}", "X-Merged-2": "{\"bar\":[3,4]}", "X-Merged-3": "[\"a\",\"b\"]"}, http.StatusOK}, {"coalesce", "/v1/coalesce?q=a", map[string]string{"X-Coalesce-1": "/v1/coalesce", "X-Coalesce-2": "default", "X-Coalesce-3": "default", "X-Coalesce-4": "default"}, http.StatusOK}, + {"default", "/v1/default?q=a", map[string]string{"X-Coalesce-1": "/v1/default", "X-Coalesce-2": "default", "X-Coalesce-3": "default", "X-Coalesce-4": "default"}, http.StatusOK}, } { t.Run(tc.path[1:], func(subT *testing.T) { helper := test.New(subT) diff --git a/server/testdata/integration/functions/01_couper.hcl b/server/testdata/integration/functions/01_couper.hcl index 4d98a97f7..543a3443b 100644 --- a/server/testdata/integration/functions/01_couper.hcl +++ b/server/testdata/integration/functions/01_couper.hcl @@ -22,5 +22,16 @@ server "api" { } } } + + endpoint "/default" { + response { + headers = { + x-coalesce-1 = coalesce(request.path, "default") + x-coalesce-2 = coalesce(request.cookies.undef, "default") + x-coalesce-3 = coalesce(request.query.q[1], "default") + x-coalesce-4 = coalesce(request.cookies.undef, request.query.q[1], "default", request.path) + } + } + } } } From b0621d7afd8a5b030b4e814b2e39fb070ee2b886 Mon Sep 17 00:00:00 2001 From: Kostas Mavrikis Date: Fri, 22 Oct 2021 15:08:53 +0200 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a1f2c2a..1a0ded091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Unreleased changes are available as `avenga/couper:edge` container. +* **Added** + * Register `default` function as `coalesce` alias ([#345](https://github.com/avenga/couper/issues/345)) + --- ## [1.5](https://github.com/avenga/couper/releases/tag/1.5) From a754b8019009f44a6faf3726f67ec6c9b6cf1bf7 Mon Sep 17 00:00:00 2001 From: Kostas Mavrikis Date: Mon, 25 Oct 2021 17:13:26 +0200 Subject: [PATCH 3/4] Rename test function and headers --- server/http_integration_test.go | 2 +- server/testdata/integration/functions/01_couper.hcl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/http_integration_test.go b/server/http_integration_test.go index f386234f2..91949afb1 100644 --- a/server/http_integration_test.go +++ b/server/http_integration_test.go @@ -3305,7 +3305,7 @@ func TestFunctions(t *testing.T) { for _, tc := range []testCase{ {"merge", "/v1/merge", map[string]string{"X-Merged-1": "{\"foo\":[1,2]}", "X-Merged-2": "{\"bar\":[3,4]}", "X-Merged-3": "[\"a\",\"b\"]"}, http.StatusOK}, {"coalesce", "/v1/coalesce?q=a", map[string]string{"X-Coalesce-1": "/v1/coalesce", "X-Coalesce-2": "default", "X-Coalesce-3": "default", "X-Coalesce-4": "default"}, http.StatusOK}, - {"default", "/v1/default?q=a", map[string]string{"X-Coalesce-1": "/v1/default", "X-Coalesce-2": "default", "X-Coalesce-3": "default", "X-Coalesce-4": "default"}, http.StatusOK}, + {"default", "/v1/default?q=a", map[string]string{"X-Default-1": "/v1/default", "X-Default-2": "default", "X-Default-3": "default", "X-Default-4": "default"}, http.StatusOK}, } { t.Run(tc.path[1:], func(subT *testing.T) { helper := test.New(subT) diff --git a/server/testdata/integration/functions/01_couper.hcl b/server/testdata/integration/functions/01_couper.hcl index 543a3443b..b2c4e29fc 100644 --- a/server/testdata/integration/functions/01_couper.hcl +++ b/server/testdata/integration/functions/01_couper.hcl @@ -26,10 +26,10 @@ server "api" { endpoint "/default" { response { headers = { - x-coalesce-1 = coalesce(request.path, "default") - x-coalesce-2 = coalesce(request.cookies.undef, "default") - x-coalesce-3 = coalesce(request.query.q[1], "default") - x-coalesce-4 = coalesce(request.cookies.undef, request.query.q[1], "default", request.path) + x-default-1 = default(request.path, "default") + x-default-2 = default(request.cookies.undef, "default") + x-default-3 = default(request.query.q[1], "default") + x-default-4 = default(request.cookies.undef, request.query.q[1], "default", request.path) } } } From 0541eb255da52166ba7ade448fd40283a46331c3 Mon Sep 17 00:00:00 2001 From: Kostas Mavrikis Date: Mon, 25 Oct 2021 17:43:47 +0200 Subject: [PATCH 4/4] Add the correct url in the changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a0ded091..ef8f45ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ Unreleased changes are available as `avenga/couper:edge` container. * **Added** - * Register `default` function as `coalesce` alias ([#345](https://github.com/avenga/couper/issues/345)) + * Register `default` function as `coalesce` alias ([#356](https://github.com/avenga/couper/pull/356)) ---