forked from wendal/gor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
238 lines (201 loc) · 4.28 KB
/
types.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gor
import (
"fmt"
"github.com/wendal/mustache"
"log"
"time"
)
// 最重要的封装类之一
// Golang是强静态语言,无法动态添加/删除属性, 而元数据(map[string]interface{})允许包含用户自定义的key
// 所以只能使用Mapper这类封装部分常用Getter
type Mapper map[string]interface{}
func (m Mapper) Get(key string) interface{} {
return m[key]
}
func (m Mapper) GetString(key string) string {
val := m[key]
if val == nil {
return ""
}
if str, ok := val.(string); ok {
return str
}
return fmt.Sprintf("%v", val)
}
func (m *Mapper) String(key string) string {
return m.GetString(key)
}
// goyaml2获取string的机制决定了string肯定trim了的. 但依赖这个特性,靠谱不?
/*
func (m *Mapper) StringTrim(key string) string {
str := m.GetString(key)
return strings.Trim(str, " \t\n\r")
}
*/
func (m *Mapper) Int64(key string) int64 {
return m.GetInt(key)
}
func (m *Mapper) Int(key string) int {
return int(m.GetInt(key))
}
func (m Mapper) GetInt(key string) int64 {
val := m[key]
if val == nil {
return 0
}
i, ok := val.(int64)
if ok {
return i
}
i2, ok := val.(int)
if ok {
return int64(i2)
}
return 0
}
func (m Mapper) Id() string {
return m.GetString("id")
}
func (m Mapper) Url() string {
return m.GetString("url")
}
func (m Mapper) Layout() string {
return m.GetString("layout")
}
func (m Mapper) Permalink() string {
return m.GetString("permalink")
}
func (m Mapper) Tags() []string {
return m.GetStrings("tags")
}
func (m Mapper) Categories() []string {
return m.GetStrings("categories")
}
// 由于是类型不可预知,所以需要自行封装为[]string
func (m Mapper) GetStrings(key string) (strs []string) {
v := m[key]
strs = make([]string, 0)
if v == nil {
return
}
switch v.(type) {
case string:
strs = []string{v.(string)}
case []interface{}:
for _, v2 := range v.([]interface{}) {
strs = append(strs, fmt.Sprintf("%v", v2))
}
case []string:
strs = v.([]string)
default:
log.Println(">>", v)
}
return
}
type DocContent struct {
Source string `json:"-"`
Main string `json:"-"`
TPL *mustache.Template `json:"-"`
}
type CollatedYear struct {
Year string
Months []*CollatedMonth
months map[string]*CollatedMonth `json:"-"`
}
type CollatedMonth struct {
Month string
_month time.Month `json:"-"`
Posts []string
}
type CollatedYears []*CollatedYear
func (c CollatedYears) Len() int {
return len(c)
}
func (c CollatedYears) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c CollatedYears) Less(i, j int) bool {
return c[i].Year > c[j].Year
}
type CollatedMonths []*CollatedMonth
func (c CollatedMonths) Len() int {
return len(c)
}
func (c CollatedMonths) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c CollatedMonths) Less(i, j int) bool {
return c[i]._month > c[j]._month
}
type WebSite struct {
Root string
SiteCnf SiteConfig
TopCnf TopConfig
Posts map[string]PostBean
Pages map[string]PageBean
ThemeCnf ThemeConfig
Layouts map[string]Mapper
RootURL string
BasePath string
BaiseURLs map[string]string
Tags map[string]*Tag
Catalogs map[string]*Catalog
Chronological []string
Collated CollatedYears
}
type SiteConfig struct {
Title string
Tagline string
Author Mapper
Navigation []string
//Urls map[string]interface{} // for user custom
}
type TopConfig struct {
Theme string
Production_url string
Posts PostConfig
Pages PageConfig
Paginator PaginatorConfig
}
type PostConfig struct {
Permalink string
Summary_lines int
Latest int
Layout string
Exclude string
}
type PageConfig struct {
Permalink string
Layout string
Exclude string
}
type PaginatorConfig struct {
Namespace string
Per_page int
Root_page string
Layout string
}
type PostBean struct {
Id string
Title string
Date string
Layout string
Permalink string
Categories []string
Tags []string
Url string
_Date time.Time
_Meta map[string]interface{}
}
type PageBean struct {
Id string
Title string
Date time.Time
Layout string
Permalink string
Categories []string
Tags []string
Url string
_Meta map[string]interface{}
}
type ThemeConfig struct{}