CreatePath combines the route's path pattern with a Params map to produce a valid path.
(params Params)
| 85 | // CreatePath combines the route's path pattern with a Params map |
| 86 | // to produce a valid path. |
| 87 | func (r Route) CreatePath(params Params) (string, error) { |
| 88 | components := strings.Split(r.Path, "/") |
| 89 | for i, c := range components { |
| 90 | if len(c) == 0 { |
| 91 | continue |
| 92 | } |
| 93 | if c[0] == ':' { |
| 94 | val, ok := params[c[1:]] |
| 95 | if !ok { |
| 96 | return "", fmt.Errorf("missing param %s", c) |
| 97 | } |
| 98 | components[i] = val |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | u, err := url.Parse(strings.Join(components, "/")) |
| 103 | if err != nil { |
| 104 | return "", err |
| 105 | } |
| 106 | return u.String(), nil |
| 107 | } |
| 108 | |
| 109 | // Router combines route and resource information in order to generate HTTP |
| 110 | // requests. |
no test coverage detected