forked from ungerik/go-start
/
indirecturl.go
37 lines (31 loc) · 852 Bytes
/
indirecturl.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
package view
import (
"github.com/ungerik/go-start/errs"
)
// IndirectURL encapsulates pointers to URL implementations.
// To break circular dependencies, addresses of URL implementing variables
// can be passed to this function that encapsulates it with an URL
// implementation that dereferences the pointers at runtime.
func IndirectURL(urlPtr interface{}) URL {
switch s := urlPtr.(type) {
case **Page:
return &indirectPageURL{s}
case *ViewWithURL:
return IndirectViewWithURL(s)
case *URL:
return &indirectURL{s}
}
panic(errs.Format("%T not a pointer to a view.URL", urlPtr))
}
type indirectURL struct {
url *URL
}
func (self *indirectURL) URL(ctx *Context) string {
return (*self.url).URL(ctx)
}
type indirectPageURL struct {
page **Page
}
func (self *indirectPageURL) URL(ctx *Context) string {
return self.page.URL(ctx)
}