~chrisppy/go-barefeed

ref: e76763008dc2b1750154fb00285056605c882e40 go-barefeed/feed.go -rw-r--r-- 2.0 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
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
package barefeed

import (
	"fmt"
	"strings"

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

// FeedType is an enumerated type for Feed
type FeedType uint

const (
	// RSS denotes the feed as RSS
	RSS FeedType = iota
	// ATOM denotes the feed as ATOM
	ATOM
)

// 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"`
	FeedType    FeedType  `bare:"feedType"`
	Title       Text      `bare:"title"`
	Updated     Timestamp `bare:"updated"`
	Authors     Persons   `bare:"authors"`
	Links       Links     `bare:"links"`
	Entries     []string  `bare:"entries"`
	Generator   *string   `bare:"generator"`
	Description *Text     `bare:"description"`
}

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

// String converts the enumerated value to a string.  "RSS" is default.
func (t FeedType) String() string {
	switch t {
	case ATOM:
		return "ATOM"
	default:
		return "RSS"
	}
}

// ToFeedType converts a string to the enumerated value.  RSS is default.
func ToFeedType(t string) FeedType {
	switch t {
	case "ATOM":
		return ATOM
	default:
		return RSS
	}
}