Index functions

This commit is contained in:
Simon Martens
2025-03-26 16:18:53 +01:00
parent dd24aea0a3
commit 052f21e87a
10 changed files with 240 additions and 23 deletions

View File

@@ -3,6 +3,7 @@ package xmlmodels
import (
"encoding/json"
"encoding/xml"
"iter"
xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
)
@@ -17,6 +18,28 @@ type Meta struct {
Recieved []Action `xml:"recieved"`
}
func (m *Meta) Earliest() *Date {
var earliest *Date
for _, action := range m.Sent {
if earliest == nil || action.Earliest().Sort().Before(*earliest.Sort()) {
earliest = action.Earliest()
}
}
if earliest != nil {
return earliest
}
for _, action := range m.Recieved {
if earliest == nil || action.Earliest().Sort().Before(*earliest.Sort()) {
earliest = action.Earliest()
}
}
return earliest
}
func (m Meta) Keys() []any {
return []any{m.Letter}
}
@@ -33,8 +56,30 @@ func (m Meta) String() string {
return string(json)
}
func (m Meta) SendRecieved() iter.Seq2[*Action, *Action] {
return func(yield func(*Action, *Action) bool) {
for i, sent := range m.Sent {
var rec *Action
if i < len(m.Recieved) {
rec = &m.Recieved[i]
}
if !yield(&sent, rec) {
return
}
}
}
}
type Action struct {
Dates []Date `xml:"date,attr"`
Dates []Date `xml:"date"`
Places []RefElement `xml:"place"`
Persons []RefElement `xml:"person"`
}
func (a *Action) Earliest() *Date {
if len(a.Dates) == 0 {
return nil
}
return &a.Dates[0]
}