-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.go
More file actions
137 lines (110 loc) · 4.04 KB
/
Copy pathmodel.go
File metadata and controls
137 lines (110 loc) · 4.04 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cms
import "maps"
import "net/http"
// Handler is a special handler invoked if specified in Content.
type Handler func(*CMS, Content, map[string]string, http.ResponseWriter, *http.Request)
// Copy is the translated copy for Content.
type Copy map[string]map[string]any
// Sitemap is the sitemap data for the Content.
type Sitemap struct {
Priority string `json:"priority,omitempty"`
}
// Experiment is an A/B testing experiment.
type Experiment struct {
Name string `json:"name"`
Variant string `json:"variant"`
}
// Analytics is the analytics metadata for the Content.
type Analytics struct {
Tags map[string]string `json:"tags,omitempty"`
Experiment Experiment `json:"experiment"`
}
// Content is a page or element for the CMS.
type Content struct {
DisplayName string `json:"display_name"`
DisableCache bool `json:"disable_cache,omitempty"`
Path map[string]string `json:"path,omitempty"`
Sitemap Sitemap `json:"sitemap"`
Header map[string]string `json:"header,omitempty"`
Handler string `json:"handler,omitempty"`
Analytics Analytics `json:"analytics"`
Ref string `json:"ref,omitempty"`
Tpl string `json:"tpl,omitempty"`
Data map[string]any `json:"data,omitempty"`
Copy Copy `json:"copy,omitempty"`
Content map[string][]Content `json:"content,omitempty"`
// File is the path for the current content file.
File string `json:"-"`
// Request is the HTTP request.
Request *http.Request `json:"-"`
// Language is extracted and set from Path automatically.
Language string `json:"-"`
// CanonicalLink is set automatically using the configured hostname and Path.
CanonicalLink string `json:"-"`
// Experiments is a list of A/B experiments extracted from the content (name -> variants).
Experiments map[string][]string `json:"-"`
// SelectedExperiments is a list of selected A/B experiments from the Experiments list.
SelectedExperiments map[string]string `json:"-"`
// SelectedPageExperiment is an experiment from the page experiments list, redirecting if the visitor is on the wrong page.
SelectedPageExperiment string `json:"-"`
// Position is the element position path in JSON.
Position string `json:"-"`
}
// Clone returns a copy of the Content.
func (content *Content) Clone() Content {
path := make(map[string]string)
maps.Copy(path, content.Path)
header := make(map[string]string)
maps.Copy(header, content.Header)
tags := make(map[string]string)
maps.Copy(tags, content.Analytics.Tags)
data := make(map[string]any)
maps.Copy(data, content.Data)
contentCopy := make(Copy)
for k, v := range content.Copy {
contentCopy[k] = make(map[string]any)
maps.Copy(contentCopy[k], v)
}
c := make(map[string][]Content)
for k, v := range content.Content {
c[k] = make([]Content, len(v))
for i, j := range v {
c[k][i] = j.Clone()
}
}
experiments := make(map[string][]string)
for k, v := range content.Experiments {
experiments[k] = make([]string, len(v))
for i, j := range v {
experiments[k][i] = j
}
}
selectedExperiments := make(map[string]string)
maps.Copy(selectedExperiments, content.SelectedExperiments)
return Content{
DisplayName: content.DisplayName,
DisableCache: content.DisableCache,
Path: path,
Sitemap: Sitemap{
Priority: content.Sitemap.Priority,
},
Header: header,
Handler: content.Handler,
Analytics: Analytics{
Tags: tags,
Experiment: content.Analytics.Experiment,
},
Ref: content.Ref,
Tpl: content.Tpl,
Data: data,
Copy: contentCopy,
Content: c,
Request: content.Request,
Language: content.Language,
CanonicalLink: content.CanonicalLink,
Experiments: experiments,
SelectedExperiments: selectedExperiments,
SelectedPageExperiment: content.SelectedPageExperiment,
Position: content.Position,
}
}