-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss.go
157 lines (137 loc) · 5.6 KB
/
rss.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
package sparkle
import "encoding/xml"
type RSS struct {
Channels []*Channel `xml:"channel"`
}
func (r *RSS) MarshalXML(enc *xml.Encoder, _ xml.StartElement) error {
err := enc.EncodeToken(xml.StartElement{
Name: xml.Name{Local: "rss"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "version"}, Value: "2.0"},
{Name: xml.Name{Local: "xmlns:sparkle"}, Value: "http://www.andymatuschak.org/xml-namespaces/sparkle"},
{Name: xml.Name{Local: "xmlns:dc"}, Value: "http://purl.org/dc/elements/1.1/"},
},
})
if err != nil {
return err
}
err = enc.EncodeElement(r.Channels, xml.StartElement{Name: xml.Name{Local: "channel"}})
if err != nil {
return err
}
return enc.EncodeToken(xml.EndElement{Name: xml.Name{Local: "rss"}})
}
func (r *RSS) UnmarshalXML(dec *xml.Decoder, _ xml.StartElement) error {
var data []unmarshalRSS
if err := dec.Decode(&data); err != nil {
return err
}
r.Channels = make([]*Channel, 0, len(data))
for _, item := range data {
channel := &Channel{
Title: item.Title,
Link: item.Link,
Description: item.Description,
Language: item.Language,
Items: make([]*Item, 0, len(item.Items)),
}
for _, item := range item.Items {
channel.Items = append(channel.Items, &Item{
Title: item.Title,
PubDate: item.PubDate,
Description: item.Description,
Version: item.Version,
ReleaseNotesLink: item.ReleaseNotesLink,
CriticalUpdate: (*CriticalUpdate)(item.CriticalUpdate),
Tags: (*Tags)(item.Tags),
MinimumAutoupdateVersion: item.MinimumAutoupdateVersion,
IgnoreSkippedUpgradesBelowVersion: item.IgnoreSkippedUpgradesBelowVersion,
Enclosure: (*Enclosure)(item.Enclosure),
})
}
r.Channels = append(r.Channels, channel)
}
return dec.Skip()
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link,omitempty"`
Description string `xml:"description,omitempty"`
Language string `xml:"language,omitempty"`
Items []*Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
PubDate string `xml:"pubDate"`
Description *CdataString `xml:"description,omitempty"`
Version string `xml:"sparkle:version,omitempty"`
ReleaseNotesLink string `xml:"sparkle:releaseNotesLink,omitempty"`
CriticalUpdate *CriticalUpdate `xml:"sparkle:criticalUpdate,omitempty"`
Tags *Tags `xml:"sparkle:tags,omitempty"`
MinimumAutoupdateVersion string `xml:"sparkle:minimumAutoupdateVersion,omitempty"`
IgnoreSkippedUpgradesBelowVersion string `xml:"sparkle:ignoreSkippedUpgradesBelowVersion,omitempty"`
Enclosure *Enclosure `xml:"enclosure,omitempty"`
}
// CdataString for XML CDATA
// See issue: https://github.com/golang/go/issues/16198
type CdataString struct {
Value string `xml:",cdata"`
}
type CriticalUpdate struct {
Version string `xml:"sparkle:version,attr,omitempty"`
}
type Tags struct {
CriticalUpdate Bool `xml:"sparkle:criticalUpdate,omitempty"`
}
type Bool bool
func (xb *Bool) MarshalText() ([]byte, error) {
return nil, nil
}
func (xb *Bool) UnmarshalText([]byte) error {
*xb = true
return nil
}
type Enclosure struct {
URL string `xml:"url,attr"`
OS string `xml:"sparkle:os,attr"`
Version string `xml:"sparkle:version,attr"`
DSASignature string `xml:"sparkle:dsaSignature,attr,omitempty"`
EDSignature string `xml:"sparkle:edSignature,attr,omitempty"`
InstallerArguments string `xml:"sparkle:installerArguments,attr,omitempty"`
MinimumSystemVersion string `xml:"sparkle:minimumSystemVersion,attr,omitempty"`
Length int `xml:"length,attr,omitempty"`
Type string `xml:"type,attr"`
}
type unmarshalRSS struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Link string `xml:"link,omitempty"`
Description string `xml:"description,omitempty"`
Language string `xml:"language,omitempty"`
Items []struct {
Title string `xml:"title"`
PubDate string `xml:"pubDate"`
Description *CdataString `xml:"description,omitempty"`
Version string `xml:"version,omitempty"`
ReleaseNotesLink string `xml:"releaseNotesLink,omitempty"`
CriticalUpdate *struct {
Version string `xml:"version,attr,omitempty"`
} `xml:"criticalUpdate,omitempty"`
Tags *struct {
CriticalUpdate Bool `xml:"criticalUpdate,omitempty"`
} `xml:"tags,omitempty"`
MinimumAutoupdateVersion string `xml:"minimumAutoupdateVersion,omitempty"`
IgnoreSkippedUpgradesBelowVersion string `xml:"ignoreSkippedUpgradesBelowVersion,omitempty"`
Enclosure *struct {
URL string `xml:"url,attr"`
OS string `xml:"os,attr"`
Version string `xml:"version,attr"`
DSASignature string `xml:"dsaSignature,attr,omitempty"`
EDSignature string `xml:"edSignature,attr,omitempty"`
InstallerArguments string `xml:"installerArguments,attr,omitempty"`
MinimumSystemVersion string `xml:"minimumSystemVersion,attr,omitempty"`
Length int `xml:"length,attr,omitempty"`
Type string `xml:"type,attr"`
} `xml:"enclosure,omitempty"`
} `xml:"item"`
}