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