summaryrefslogtreecommitdiff
path: root/internal/storage/index.go
blob: bfeafbe69d345ee8332d8b51849acc998898f435 (plain)
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
package storage

import (
	"encoding/json"
	"fmt"
	"time"
)

const IndexFileName = "index.json"
const MedleysIndexFileName = "medleys.json"

const IndexSchemaVersion = 2

type IndexEntry struct {
	Link             string    `json:"link"`
	ModifiedTime     time.Time `json:"modifiedTime"`
	Title            string    `json:"title"`
	ShortDescription string    `json:"shortDescription"`
	ActionDate       string    `json:"actionDate"`
	PublishedTime    time.Time `json:"publishedTime"`
	Thumbnail        string    `json:"thumbnail"`
	Tags             []string  `json:"tags"`
	Geolocation      string    `json:"geolocation"`
	Medley           string    `json:"medley,omitempty"`
	MedleyPart       int       `json:"medleyPart,omitempty"`
}

type IndexV2Category struct {
	GeneratedAt time.Time             `json:"generatedAt"`
	Pages       map[string]IndexEntry `json:"pages"`
}

type IndexV1Category struct {
	GeneratedAt time.Time    `json:"generatedAt"`
	Pages       []IndexEntry `json:"pages"`
}

type Index struct {
	SchemaVersion int       `json:"schemaVersion"`
	GeneratedAt   time.Time `json:"generatedAt"`
	Categories    any       `json:"categories"`
}

func (idx *Index) UnmarshalJSON(data []byte) error {
	var tmp struct {
		SchemaVersion int             `json:"schemaVersion"`
		GeneratedAt   time.Time       `json:"generatedAt"`
		Categories    json.RawMessage `json:"categories"`
	}

	if err := json.Unmarshal(data, &tmp); err != nil {
		return err
	}

	idx.SchemaVersion = tmp.SchemaVersion
	idx.GeneratedAt = tmp.GeneratedAt

	switch tmp.SchemaVersion {
	case 1:
		var categories map[string]*IndexV1Category
		if err := json.Unmarshal(tmp.Categories, &categories); err != nil {
			return fmt.Errorf("unmarshal map[string]*IndexV1Category: %w", err)
		}
		idx.Categories = &categories
	case 2:
		var categories map[string]*IndexV2Category
		if err := json.Unmarshal(tmp.Categories, &categories); err != nil {
			return fmt.Errorf("unmarshal map[string]*IndexV2Category: %w", err)
		}
		idx.Categories = &categories
	default:
		return fmt.Errorf("unsupported index version: %d", tmp.SchemaVersion)
	}

	return nil
}

type IndexV1 struct {
	SchemaVersion int                         `json:"schemaVersion"`
	GeneratedAt   time.Time                   `json:"generatedAt"`
	Categories    map[string]*IndexV1Category `json:"categories"`
}

type MedleyEntry struct {
	Codename string   `json:"codename"`
	Content  []string `json:"content"`
}

type MedleyPageEntry struct {
	Codename string `json:"codename"`
	Position int    `json:"position"`
}