~chrisppy/go-barefeed

ref: e76763008dc2b1750154fb00285056605c882e40 go-barefeed/entry.go -rw-r--r-- 1.4 KiB
e7676300Chris Palmer fix textType 4 months ago
                                                                                
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
package barefeed

import (
	"fmt"

	"git.sr.ht/~sircmpwn/go-bare"
)

// Entry type contains elements from RSS Item & Atom Entry
type Entry interface {
	bare.Union
}

func init() {
	bare.RegisterUnion((*Entry)(nil)).
		Member(*new(EntryV1), 0)

}

// Entries are a slice of Entry
type Entries []Entry

// Len is the length of the slice
func (e Entries) Len() int {
	return len(e)
}

// Less is how to compare the two Entry
func (e Entries) Less(i, j int) bool {
	var t1 Timestamp
	switch t := e[i].(type) {
	case *EntryV1:
		t1 = e[i].(*EntryV1).Published
	default:
		panic(fmt.Sprintf("invalid type: %v", t))
	}

	var t2 Timestamp
	switch t := e[j].(type) {
	case *EntryV1:
		t2 = e[j].(*EntryV1).Published
	default:
		panic(fmt.Sprintf("invalid type: %v", t))
	}
	return t1 > t2
}

// Swap is how to swap the two Entry
func (e Entries) Swap(i, j int) {
	e[i], e[j] = e[j], e[i]
}

// EntryV1 type contains elements from RSS Item & Atom Entry
type EntryV1 struct {
	FeedPath  string    `bare:"feedPath"`
	ID        string    `bare:"id"`
	Title     Text      `bare:"title"`
	Content   Text      `bare:"content"`
	Published Timestamp `bare:"published"`
	Updated   Timestamp `bare:"updated"`
	Authors   Persons   `bare:"authors"`
	Links     Links     `bare:"links"`
}

// IsUnion function is necessary to make the type compatible with the Union
// interface
func (t EntryV1) IsUnion() {}