From ada0c0871a2585b49a847e4bf2202aba338f6533 Mon Sep 17 00:00:00 2001 From: mitchelljfs Date: Mon, 24 Sep 2018 16:59:18 -0700 Subject: [PATCH] Changed internal params attribute to map[string]interface{}; fixed issue with having the same value for two path parameters --- router.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/router.go b/router.go index 9523403..6f1ce94 100644 --- a/router.go +++ b/router.go @@ -42,7 +42,7 @@ type APIGHandler func(ctx *APIGContext) type APIGRouter struct { request *events.APIGatewayProxyRequest endpoints map[string]*radix.Tree - params map[string]string + params map[string]interface{} prefix string headers map[string]string context context.Context @@ -71,7 +71,7 @@ func NewAPIGRouter(cfg *APIGRouterConfig) *APIGRouter { patch: radix.New(), delete: radix.New(), }, - params: map[string]string{}, + params: map[string]interface{}{}, prefix: cfg.Prefix, headers: cfg.Headers, context: cfg.Context, @@ -118,23 +118,21 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { splitPath = stripSlashesAndSplit(path) ) - for k := range r.params { - pname := strings.TrimPrefix(k, "{") - pname = strings.TrimSuffix(pname, "}") - if r.request.PathParameters[pname] != "" { - pval := r.request.PathParameters[pname] + for p := range r.params { + if r.request.PathParameters[p] != "" { + pval := r.request.PathParameters[p] for i, v := range splitPath { if v == pval { - splitPath[i] = k + splitPath[i] = "{" + p + "}" + break } } - } } path = "/" + strings.Join(splitPath, "/") if handlersInterface, ok = endpointTree.Get(path); !ok { - respbody, _ := json.Marshal(map[string]string{"error": "no route matching path found"}) + respbody, _ = json.Marshal(map[string]string{"error": "no route matching path found"}) response.StatusCode = http.StatusNotFound response.Body = string(respbody) @@ -160,7 +158,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse { status, respbody, err = ctx.respDeconstruct() if err != nil { - respbody, _ := json.Marshal(map[string]string{"error": err.Error()}) + respbody, _ = json.Marshal(map[string]string{"error": err.Error()}) if strings.Contains(err.Error(), "record not found") { status = 204 } else if status != 204 && status < 400 { @@ -200,7 +198,9 @@ func (r *APIGRouter) addEndpoint(method string, route string, handlers []APIGHan rtearr := stripSlashesAndSplit(route) for _, v := range rtearr { if strings.HasPrefix(v, "{") { - r.params[v] = "" // adding params as keys with {brackets} + v = strings.TrimPrefix(v, "{") + v = strings.TrimSuffix(v, "}") + r.params[v] = nil // adding params as *unique* keys } }