~chrisppy/go-barefeed

ref: 022d6ea6f0215c28fa4b4b69dd9d821c8766ded8 go-barefeed/feed.go -rw-r--r-- 1.5 KiB
022d6ea6Chris Palmer updates from barefeed fixes 5 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
68
69
70
package barefeed

import (
	"fmt"
	"strings"

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

// Feed type contains elements from RSS Channel & Atom Feed
type Feed interface {
	bare.Union
}

func init() {
	bare.RegisterUnion((*Feed)(nil)).
		Member(*new(FeedV1), 0)
}

// Feeds are a slice of Feed
type Feeds []Feed

// Len is the length of the slice
func (f Feeds) Len() int {
	return len(f)
}

// Less is how to compare the two Feed
func (f Feeds) Less(i, j int) bool {
	var t1 string
	switch t := f[i].(type) {
	case *FeedV1:
		t1 = f[i].(*FeedV1).Title.Value
	default:
		panic(fmt.Sprintf("invalid type: %v", t))
	}

	var t2 string
	switch t := f[j].(type) {
	case *FeedV1:
		t2 = f[j].(*FeedV1).Title.Value
	default:
		panic(fmt.Sprintf("invalid type: %v", t))
	}

	return strings.ToLower(t1) < strings.ToLower(t2)
}

// Swap is how to swap the two Feed
func (f Feeds) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

// FeedV1 type contains elements from RSS Channel & Atom Feed
type FeedV1 struct {
	Path        string    `bare:"path"`
	ID          string    `bare:"id"`
	IsAtom      bool      `bare:"isAtom"`
	Title       Text      `bare:"title"`
	Updated     Timestamp `bare:"updated"`
	Entries     Entries   `bare:"entries"`
	Authors     Persons   `bare:"authors"`
	Links       Links     `bare:"links"`
	Generator   *string   `bare:"generator,omitempty"`
	Description *Text     `bare:"description,omitempty"`
}

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