1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Write a first version of the ROLIE feed.

This commit is contained in:
Sascha L. Teichmann 2021-11-17 02:16:55 +01:00
parent b8d51da4cb
commit 1d0d8a6d03
3 changed files with 136 additions and 5 deletions

View file

@ -1,5 +1,12 @@
package csaf
import (
"encoding/json"
"io"
"sort"
"time"
)
type Link struct {
Rel string `json:"rel"`
HRef string `json:"href"`
@ -30,7 +37,7 @@ type Entry struct {
Link []Link `json:"link"`
Published TimeStamp `json:"published"`
Updated TimeStamp `json:"updated"`
Summary Summary `json:"summary"`
Summary *Summary `json:"summary,omitempty"`
Content Content `json:"content"`
Format Format `json:"format"`
}
@ -38,8 +45,39 @@ type Entry struct {
type ROLIEFeed struct {
ID string `json:"id"`
Title string `json:"title"`
Link []Link `json:"link"`
Category []Category `json:"category"`
Link []Link `json:"link,omitempty"`
Category []Category `json:"category,omitempty"`
Updated TimeStamp `json:"updated"`
Entry []*Entry `json:"entry"`
Entry []*Entry `json:"entry,omitempty"`
}
func LoadROLIEFeed(r io.Reader) (*ROLIEFeed, error) {
dec := json.NewDecoder(r)
var rf ROLIEFeed
if err := dec.Decode(&rf); err != nil {
return nil, err
}
return &rf, nil
}
func (rf *ROLIEFeed) Save(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(rf)
}
func (rf *ROLIEFeed) EntryByID(id string) *Entry {
for _, entry := range rf.Entry {
if entry.ID == id {
return entry
}
}
return nil
}
func (rf *ROLIEFeed) SortEntriesByUpdated() {
entries := rf.Entry
sort.Slice(entries, func(i, j int) bool {
return time.Time(entries[j].Updated).Before(time.Time(entries[i].Updated))
})
}