From 1325d1596214f4a204019749842a5acc35e62b5f Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Thu, 16 May 2024 22:40:32 -0400 Subject: [PATCH] flow: flatten headers and query params when len 1 in echo server task --- flow/tasks/api/serve.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/flow/tasks/api/serve.go b/flow/tasks/api/serve.go index de0c8fc93..46111cb38 100644 --- a/flow/tasks/api/serve.go +++ b/flow/tasks/api/serve.go @@ -18,6 +18,7 @@ import ( hofcontext "github.com/hofstadter-io/hof/flow/context" "github.com/hofstadter-io/hof/flow/flow" "github.com/hofstadter-io/hof/flow/tasks/csp" + "github.com/hofstadter-io/hof/lib/cuetils" ) type Serve struct { @@ -262,8 +263,9 @@ func (T *Serve) routeFromValue(path string, route cue.Value, e *echo.Echo, ctx * resp := tmp.LookupPath(cue.ParsePath("resp")) if resp.Err() != nil { - fmt.Println("handler resp error:", resp.Err()) - return resp.Err() + e := cuetils.ExpandCueError(resp.Err()) + fmt.Println("handler resp error:", e, tmp) + return e } err = T.fillRespFromValue(resp, c) @@ -320,9 +322,29 @@ func (T *Serve) buildReqValue(c echo.Context) (map[string]interface{}, error) { R := c.Request() req["method"] = R.Method - req["header"] = R.Header req["url"] = R.URL - req["query"] = c.QueryParams() + + // flatten any headers of len 1 + hm := map[string]any{} + for k,v := range R.Header { + if len(v) == 1 { + hm[k] = v[0] + } else { + hm[k] = v + } + } + req["header"] = hm + + // flatten any query params of len 1 + qm := map[string]any{} + for k,v := range c.QueryParams() { + if len(v) == 1 { + qm[k] = v[0] + } else { + qm[k] = v + } + } + req["query"] = qm b, err := io.ReadAll(R.Body) if err != nil {