MakeFilter returns a Filter which reports whether a specific macro template and its parameters pass the serve-time validation.
(tmpl macro.Template)
| 83 | // MakeFilter returns a Filter which reports whether a specific macro template |
| 84 | // and its parameters pass the serve-time validation. |
| 85 | func MakeFilter(tmpl macro.Template) context.Filter { |
| 86 | if !CanMakeHandler(tmpl) { |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | return func(ctx *context.Context) bool { |
| 91 | for i := range tmpl.Params { |
| 92 | p := tmpl.Params[i] |
| 93 | if !p.CanEval() { |
| 94 | continue // allow. |
| 95 | } |
| 96 | |
| 97 | // 07-29-2019 |
| 98 | // changed to retrieve by param index in order to support |
| 99 | // different parameter names for routes with |
| 100 | // different param types (and probably different param names i.e {name:string}, {id:uint64}) |
| 101 | // in the exact same path pattern. |
| 102 | // |
| 103 | // Same parameter names are not allowed, different param types in the same path |
| 104 | // should have different name e.g. {name} {id:uint64}; |
| 105 | // something like {name} and {name:uint64} |
| 106 | // is bad API design and we do NOT allow it by-design. |
| 107 | entry, found := ctx.Params().Store.GetEntryAt(p.Index) |
| 108 | if !found { |
| 109 | // should never happen. |
| 110 | ctx.StatusCode(p.ErrCode) // status code can change from an error handler, set it here. |
| 111 | return false |
| 112 | } |
| 113 | |
| 114 | value, passed := p.Eval(entry.String()) |
| 115 | if !passed { |
| 116 | ctx.StatusCode(p.ErrCode) // status code can change from an error handler, set it here. |
| 117 | if value != nil && p.HandleError != nil { |
| 118 | // The "value" is an error here, always (see template.Eval). |
| 119 | // This is always a type of ParamErrorHandler at this state (see CanMakeHandler). |
| 120 | p.HandleError.(ParamErrorHandler)(ctx, p.Index, value.(error)) |
| 121 | } |
| 122 | return false |
| 123 | } |
| 124 | |
| 125 | // Fixes binding different path parameters names, |
| 126 | // |
| 127 | // app.Get("/{fullname:string}", strHandler) |
| 128 | // app.Get("/{id:int}", idHandler) |
| 129 | // |
| 130 | // before that user didn't see anything |
| 131 | // but under the hoods the set-ed value was a type of string instead of type of int, |
| 132 | // because store contained both "fullname" (which set-ed by the router itself on its string representation) |
| 133 | // and "id" by the param evaluator (see core/router/handler.go and bindMultiParamTypesHandler->MakeFilter) |
| 134 | // and the MVC get by index (e.g. 0) therefore |
| 135 | // it got the "fullname" of type string instead of "id" int if /{int} requested. |
| 136 | // which is critical for faster type assertion in the upcoming, new iris dependency injection (20 Feb 2020). |
| 137 | ctx.Params().Store[p.Index] = memstore.Entry{ |
| 138 | Key: p.Name, |
| 139 | ValueRaw: value, |
| 140 | } |
| 141 | |
| 142 | // for i, v := range ctx.Params().Store { |
no test coverage detected
searching dependent graphs…