Changed internal params attribute to map[string]interface{};
fixed issue with having the same value for two path parameters
This commit is contained in:
parent
ade833df39
commit
ada0c0871a
24
router.go
24
router.go
|
@ -42,7 +42,7 @@ type APIGHandler func(ctx *APIGContext)
|
||||||
type APIGRouter struct {
|
type APIGRouter struct {
|
||||||
request *events.APIGatewayProxyRequest
|
request *events.APIGatewayProxyRequest
|
||||||
endpoints map[string]*radix.Tree
|
endpoints map[string]*radix.Tree
|
||||||
params map[string]string
|
params map[string]interface{}
|
||||||
prefix string
|
prefix string
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
context context.Context
|
context context.Context
|
||||||
|
@ -71,7 +71,7 @@ func NewAPIGRouter(cfg *APIGRouterConfig) *APIGRouter {
|
||||||
patch: radix.New(),
|
patch: radix.New(),
|
||||||
delete: radix.New(),
|
delete: radix.New(),
|
||||||
},
|
},
|
||||||
params: map[string]string{},
|
params: map[string]interface{}{},
|
||||||
prefix: cfg.Prefix,
|
prefix: cfg.Prefix,
|
||||||
headers: cfg.Headers,
|
headers: cfg.Headers,
|
||||||
context: cfg.Context,
|
context: cfg.Context,
|
||||||
|
@ -118,23 +118,21 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
|
||||||
splitPath = stripSlashesAndSplit(path)
|
splitPath = stripSlashesAndSplit(path)
|
||||||
)
|
)
|
||||||
|
|
||||||
for k := range r.params {
|
for p := range r.params {
|
||||||
pname := strings.TrimPrefix(k, "{")
|
if r.request.PathParameters[p] != "" {
|
||||||
pname = strings.TrimSuffix(pname, "}")
|
pval := r.request.PathParameters[p]
|
||||||
if r.request.PathParameters[pname] != "" {
|
|
||||||
pval := r.request.PathParameters[pname]
|
|
||||||
for i, v := range splitPath {
|
for i, v := range splitPath {
|
||||||
if v == pval {
|
if v == pval {
|
||||||
splitPath[i] = k
|
splitPath[i] = "{" + p + "}"
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
path = "/" + strings.Join(splitPath, "/")
|
path = "/" + strings.Join(splitPath, "/")
|
||||||
|
|
||||||
if handlersInterface, ok = endpointTree.Get(path); !ok {
|
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.StatusCode = http.StatusNotFound
|
||||||
response.Body = string(respbody)
|
response.Body = string(respbody)
|
||||||
|
@ -160,7 +158,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
|
||||||
status, respbody, err = ctx.respDeconstruct()
|
status, respbody, err = ctx.respDeconstruct()
|
||||||
|
|
||||||
if err != nil {
|
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") {
|
if strings.Contains(err.Error(), "record not found") {
|
||||||
status = 204
|
status = 204
|
||||||
} else if status != 204 && status < 400 {
|
} else if status != 204 && status < 400 {
|
||||||
|
@ -200,7 +198,9 @@ func (r *APIGRouter) addEndpoint(method string, route string, handlers []APIGHan
|
||||||
rtearr := stripSlashesAndSplit(route)
|
rtearr := stripSlashesAndSplit(route)
|
||||||
for _, v := range rtearr {
|
for _, v := range rtearr {
|
||||||
if strings.HasPrefix(v, "{") {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue