Web Variante in Go V0.1
This commit is contained in:
1 parent
f9e3e94629
commit
692b9b6ef8
60 files changed
+13140
No files matched your search
@@ -0,0 +1,145 @@
|
||||
package liturgy
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"lektorenplan/domain"
|
||||
)
|
||||
|
||||
// SourceLabel is the attribution required by the data source.
|
||||
const SourceLabel = "Daten von www.eucharistiefeier.de"
|
||||
|
||||
//go:embed liturgical-snapshot.json
|
||||
var snapshotJSON []byte
|
||||
|
||||
type snapshotFile struct {
|
||||
Version int `json:"version"`
|
||||
Source string `json:"source"`
|
||||
Diocese string `json:"diocese"`
|
||||
Years []int `json:"years"`
|
||||
Days []snapshotDay `json:"days"`
|
||||
}
|
||||
|
||||
type snapshotDay struct {
|
||||
Date string `json:"date"`
|
||||
Title string `json:"title"`
|
||||
Rank string `json:"rank"`
|
||||
Color string `json:"color"`
|
||||
Remark string `json:"bem"`
|
||||
First string `json:"l1"`
|
||||
Psalm string `json:"ap"`
|
||||
Second string `json:"l2"`
|
||||
Gospel string `json:"ev"`
|
||||
Obligation bool `json:"obligation"`
|
||||
}
|
||||
|
||||
var (
|
||||
cachedSnapshot []domain.LiturgicalDay
|
||||
cachedSnapshotOnce bool
|
||||
)
|
||||
|
||||
func loadSnapshot() (snapshotFile, error) {
|
||||
var file snapshotFile
|
||||
if err := json.Unmarshal(snapshotJSON, &file); err != nil {
|
||||
return file, fmt.Errorf("liturgical snapshot: %w", err)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// SnapshotLoaded reports whether the embedded JSON could be decoded.
|
||||
func SnapshotLoaded() bool {
|
||||
_, err := loadSnapshot()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// SnapshotYears returns the years covered by the embedded snapshot.
|
||||
func SnapshotYears() []int {
|
||||
file, err := loadSnapshot()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return file.Years
|
||||
}
|
||||
|
||||
// SnapshotDays returns the bundled offline calendar.
|
||||
func SnapshotDays() []domain.LiturgicalDay {
|
||||
if cachedSnapshotOnce {
|
||||
return cachedSnapshot
|
||||
}
|
||||
cachedSnapshotOnce = true
|
||||
file, err := loadSnapshot()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
days := make([]domain.LiturgicalDay, 0, len(file.Days))
|
||||
for _, entry := range file.Days {
|
||||
date, ok := domain.DateFromKey(entry.Date)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
days = append(days, domain.LiturgicalDay{
|
||||
Date: date,
|
||||
Title: entry.Title,
|
||||
Rank: domain.LiturgicalRank(entry.Rank),
|
||||
Color: domain.LiturgicalColorFromAPI(entry.Color),
|
||||
Remark: entry.Remark,
|
||||
Readings: domain.Readings{
|
||||
First: entry.First,
|
||||
Psalm: entry.Psalm,
|
||||
Second: entry.Second,
|
||||
Gospel: entry.Gospel,
|
||||
},
|
||||
IsObligation: entry.Obligation,
|
||||
Source: SourceLabel,
|
||||
UpdatedAt: domain.Normalize(time.Now()),
|
||||
})
|
||||
}
|
||||
cachedSnapshot = days
|
||||
return days
|
||||
}
|
||||
|
||||
// SnapshotDaysForYear returns the bundled days of one calendar year.
|
||||
func SnapshotDaysForYear(year int) []domain.LiturgicalDay {
|
||||
var out []domain.LiturgicalDay
|
||||
for _, day := range SnapshotDays() {
|
||||
if domain.Year(day.Date) == year {
|
||||
out = append(out, day)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SnapshotMissing returns bundled days whose date keys are not yet present.
|
||||
func SnapshotMissing(existing map[string]bool) []domain.LiturgicalDay {
|
||||
var out []domain.LiturgicalDay
|
||||
for _, day := range SnapshotDays() {
|
||||
if !existing[day.DateKey()] {
|
||||
out = append(out, day)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// FillLocalGaps completes a year with the local calendar so a plan never has
|
||||
// empty rows when the online source omits days.
|
||||
func FillLocalGaps(year int, days []domain.LiturgicalDay) []domain.LiturgicalDay {
|
||||
seen := make(map[string]bool, len(days))
|
||||
for _, day := range days {
|
||||
seen[day.DateKey()] = true
|
||||
}
|
||||
for _, day := range domain.YearRange(year).Days() {
|
||||
if !seen[domain.Key(day)] {
|
||||
days = append(days, LocalDay(day))
|
||||
}
|
||||
}
|
||||
sortDays(days)
|
||||
return days
|
||||
}
|
||||
|
||||
func sortDays(days []domain.LiturgicalDay) {
|
||||
sort.Slice(days, func(i, j int) bool { return days[i].Date.Before(days[j].Date) })
|
||||
}
|
||||
Reference in new issue
Block a user