forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relative.go
58 lines (44 loc) · 1.56 KB
/
relative.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2014 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webpaths
import (
"strings"
"github.com/andreaskoch/allmark/common/route"
"github.com/andreaskoch/allmark/dataaccess"
)
// Create a new relative web path provider
func newRelativeWebPathProvider(routesProvider dataaccess.RoutesProvider, baseRoute route.Route) *RelativeWebPathProvider {
return &RelativeWebPathProvider{
routesProvider: routesProvider,
baseRoute: baseRoute,
}
}
type RelativeWebPathProvider struct {
routesProvider dataaccess.RoutesProvider
baseRoute route.Route
}
// Get the path relative for the supplied item
func (webPathProvider *RelativeWebPathProvider) Path(itemPath string) string {
// return the supplied item path if it is already absolute
if IsAbsoluteURI(itemPath) {
return itemPath
}
for _, route := range webPathProvider.routesProvider.Routes() {
// ignore all routes which are not a child of the base route
if !route.IsChildOf(webPathProvider.baseRoute) {
continue
}
// ignore all non-matching routes
if !route.IsMatch(itemPath) {
continue
}
// intersect the child route with the base route to get full path
path := strings.TrimPrefix(route.Value(), webPathProvider.baseRoute.Value())
return strings.TrimPrefix(path, "/")
}
return strings.TrimPrefix(strings.Replace(itemPath, webPathProvider.baseRoute.Value(), "", 1), "/")
}
func (webPathProvider *RelativeWebPathProvider) Base() route.Route {
return webPathProvider.baseRoute
}