forked from ptt/pttweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
116 lines (92 loc) · 2.34 KB
/
struct.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
package main
import (
"bytes"
"encoding/gob"
"golang.org/x/tools/blog/atom"
"github.com/ptt/pttweb/cache"
"github.com/ptt/pttweb/page"
)
// Useful when calling |NewFromBytes|
var (
ZeroArticle *Article
ZeroArticlePart *ArticlePart
ZeroBbsIndex *BbsIndex
ZeroBoardAtomFeed *BoardAtomFeed
)
func gobEncodeBytes(obj interface{}) ([]byte, error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(obj); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func gobDecode(in []byte, out interface{}) error {
buf := bytes.NewBuffer(in)
return gob.NewDecoder(buf).Decode(out)
}
func gobDecodeCacheable(data []byte, obj cache.Cacheable) (cache.Cacheable, error) {
if err := gobDecode(data, obj); err != nil {
return nil, err
}
return obj, nil
}
type Article struct {
ParsedTitle string
PreviewContent string
ContentHtml []byte
ContentTailHtml []byte
IsPartial bool
IsTruncated bool
CacheKey string
NextOffset int
IsValid bool
}
func (_ *Article) NewFromBytes(data []byte) (cache.Cacheable, error) {
return gobDecodeCacheable(data, new(Article))
}
func (a *Article) EncodeToBytes() ([]byte, error) {
return gobEncodeBytes(a)
}
type ArticlePart struct {
ContentHtml string
CacheKey string
NextOffset int
IsValid bool
}
func (_ *ArticlePart) NewFromBytes(data []byte) (cache.Cacheable, error) {
return gobDecodeCacheable(data, new(ArticlePart))
}
func (a *ArticlePart) EncodeToBytes() ([]byte, error) {
return gobEncodeBytes(a)
}
type BbsIndex page.BbsIndex
func (_ *BbsIndex) NewFromBytes(data []byte) (cache.Cacheable, error) {
return gobDecodeCacheable(data, new(BbsIndex))
}
func (bi *BbsIndex) EncodeToBytes() ([]byte, error) {
return gobEncodeBytes(bi)
}
type BoardAtomFeed struct {
Feed *atom.Feed
IsValid bool
}
func (_ *BoardAtomFeed) NewFromBytes(data []byte) (cache.Cacheable, error) {
return gobDecodeCacheable(data, new(BoardAtomFeed))
}
func (bi *BoardAtomFeed) EncodeToBytes() ([]byte, error) {
return gobEncodeBytes(bi)
}
func init() {
gob.Register(Article{})
gob.Register(ArticlePart{})
gob.Register(BbsIndex{})
gob.Register(BoardAtomFeed{})
// Make sure they are |Cacheable|
checkCacheable(new(Article))
checkCacheable(new(ArticlePart))
checkCacheable(new(BbsIndex))
checkCacheable(new(BoardAtomFeed))
}
func checkCacheable(c cache.Cacheable) {
// Empty
}