-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
89 lines (70 loc) · 1.92 KB
/
context.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package http
import (
"context"
"errors"
"github.com/gowool/cms"
)
type (
pageSEOKey struct{}
siteKey struct{}
pageKey struct{}
decorableKey struct{}
notDecorableKey struct{}
)
func WithSiteSEO(ctx context.Context, site cms.Site) context.Context {
seo := SEOFrom(ctx)
seo.AddFromSite(site)
ctx = WithSite(ctx, site)
ctx = WithSEO(ctx, seo)
return ctx
}
func WithPageSEO(ctx context.Context, page cms.Page) context.Context {
seo := SEOFrom(ctx)
seo.AddFromPage(page)
ctx = WithPage(ctx, page)
ctx = WithSEO(ctx, seo)
return ctx
}
func WithSEO(ctx context.Context, seo PageSEO) context.Context {
return context.WithValue(ctx, pageSEOKey{}, seo)
}
func SEOFrom(ctx context.Context) PageSEO {
if seo, ok := ctx.Value(pageSEOKey{}).(PageSEO); ok {
return seo
}
return NewPageSEO()
}
func WithSite(ctx context.Context, site cms.Site) context.Context {
if site.ID == 0 {
panic(errors.New("site is not saved"))
}
return context.WithValue(ctx, siteKey{}, site)
}
func SiteFrom(ctx context.Context) (cms.Site, bool) {
site, ok := ctx.Value(siteKey{}).(cms.Site)
return site, ok
}
func WithPage(ctx context.Context, page cms.Page) context.Context {
if page.ID == 0 {
panic(errors.New("page is not saved"))
}
return context.WithValue(ctx, pageKey{}, page)
}
func PageFrom(ctx context.Context) (cms.Page, bool) {
page, ok := ctx.Value(pageKey{}).(cms.Page)
return page, ok
}
func WithDecorable(ctx context.Context, decorable bool) context.Context {
return context.WithValue(ctx, decorableKey{}, decorable)
}
func DecorableFrom(ctx context.Context) bool {
decorable, _ := ctx.Value(decorableKey{}).(bool)
return decorable
}
func WithNotDecorable(ctx context.Context, notDecorable bool) context.Context {
return context.WithValue(ctx, notDecorableKey{}, notDecorable)
}
func NotDecorableFrom(ctx context.Context) bool {
notDecorable, _ := ctx.Value(notDecorableKey{}).(bool)
return notDecorable
}