added XSDTime datatype

This commit is contained in:
Simon Martens
2024-12-22 20:40:58 +01:00
parent ff3ed74b5e
commit 0ad9e0122f
13 changed files with 552 additions and 268 deletions

View File

@@ -40,8 +40,8 @@ func (i Issue) Keys() []string {
res = append(res, date)
}
if len(date) > 4 {
res = append(res, i.Datum.When[0:4]+"-"+strconv.Itoa(i.Number.No))
if ref, err := i.Reference(); err == nil {
res = append(res, ref)
}
i.keys = res
@@ -49,6 +49,22 @@ func (i Issue) Keys() []string {
return res
}
// TODO: We could even cache this
func (i Issue) Year() (int, error) {
if date := i.Datum.Date(); date != nil {
return date.Year(), nil
}
return 0, InvalidDateError
}
func (i Issue) Reference() (string, error) {
if date := i.Datum.Date(); date != nil {
return strconv.Itoa(date.Year()) + "-" + strconv.Itoa(i.Number.No), nil
}
return "", InvalidDateError
}
func (i Issue) String() string {
return fmt.Sprintf("Number: %v, Datum: %v, Von: %d, Bis: %d, Additionals: %v, Identifier: %v, AnnotationNote: %v\n", i.Number, i.Datum, i.Von, i.Bis, i.Additionals, i.Identifier, i.AnnotationNote)
}

View File

@@ -9,23 +9,22 @@ import (
)
type Piece struct {
XMLName xml.Name `xml:"beitrag"`
IssueRefs []IssueRef `xml:"stueck"`
PlaceRefs []PlaceRef `xml:"ort"`
CategoryRefs []CategoryRef `xml:"kategorie"`
AgentRefs []AgentRef `xml:"akteur"`
WorkRefs []WorkRef `xml:"werk"`
PieceRefs []PieceRef `xml:"beitrag"`
AdditionalRef []AdditionalRef `xml:"beilage"`
Datum []KGPZDate `xml:"datum"`
Incipit []string `xml:"incipit"`
Title []string `xml:"titel"`
XMLName xml.Name `xml:"beitrag"`
IssueRefs []IssueRef `xml:"stueck"`
PlaceRefs []PlaceRef `xml:"ort"`
CategoryRefs []CategoryRef `xml:"kategorie"`
AgentRefs []AgentRef `xml:"akteur"`
WorkRefs []WorkRef `xml:"werk"`
PieceRefs []PieceRef `xml:"beitrag"`
Datum []KGPZDate `xml:"datum"`
Incipit []string `xml:"incipit"`
Title []string `xml:"titel"`
Identifier
AnnotationNote
}
func (p Piece) String() string {
return fmt.Sprintf("ID: %s\nIssueRefs: %v\nPlaceRefs: %v\nCategoryRefs: %v\nAgentRefs: %v\nWorkRefs: %v\nPieceRefs: %v\nAdditionalRef: %v\nIncipit: %v\nTitle: %v\nAnnotations: %v\nNotes: %v\n", p.ID, p.IssueRefs, p.PlaceRefs, p.CategoryRefs, p.AgentRefs, p.WorkRefs, p.PieceRefs, p.AdditionalRef, p.Incipit, p.Title, p.Annotations, p.Notes)
return fmt.Sprintf("ID: %s\nIssueRefs: %v\nPlaceRefs: %v\nCategoryRefs: %v\nAgentRefs: %v\nWorkRefs: %v\nPieceRefs: %v\nIncipit: %v\nTitle: %v\nAnnotations: %v\nNotes: %v\n", p.ID, p.IssueRefs, p.PlaceRefs, p.CategoryRefs, p.AgentRefs, p.WorkRefs, p.PieceRefs, p.Incipit, p.Title, p.Annotations, p.Notes)
}
func (p Piece) Keys() []string {
@@ -42,11 +41,9 @@ func (p Piece) Keys() []string {
uid := uuid.New()
for _, i := range p.IssueRefs {
ret = append(ret, i.Datum+"-"+strconv.Itoa(i.Nr)+"-"+uid.String())
}
for _, i := range p.AdditionalRef {
ret = append(ret, i.Datum+"-"+strconv.Itoa(i.Nr)+"-b-"+uid.String())
if d := i.Date(); d != nil {
ret = append(ret, strconv.Itoa(d.Year())+"-"+strconv.Itoa(i.Nr)+"-"+uid.String())
}
}
p.keys = ret
@@ -54,6 +51,19 @@ func (p Piece) Keys() []string {
return ret
}
func (p Piece) ReferencesIssue(y, no int) (*IssueRef, bool) {
for _, i := range p.IssueRefs {
if i.Nr == no {
d := i.Date()
if d != nil && d.Year() == y {
return &i, true
}
}
}
return nil, false
}
// TODO: We can make this fast depending on which category to look for
// but we'll have to define rules for every single category (~35 of them)
func (p Piece) IsCat(k string) bool {

View File

@@ -1,17 +1,31 @@
package xmlprovider
import "encoding/xml"
import (
"encoding/xml"
"errors"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/xsdtime"
)
var InvalidDateError = errors.New("Invalid date")
const DateLayout = "2006-01-02"
type KGPZDate struct {
XMLName xml.Name `xml:"datum"`
When string `xml:"when,attr"`
NotBefore string `xml:"notBefore,attr"`
NotAfter string `xml:"notAfter,attr"`
From string `xml:"from,attr"`
To string `xml:"to,attr"`
XMLName xml.Name `xml:"datum"`
DateAttributes
Value
}
type DateAttributes struct {
When xsdtime.XSDDate `xml:"when,attr"`
NotBefore xsdtime.XSDDate `xml:"notBefore,attr"`
NotAfter xsdtime.XSDDate `xml:"notAfter,attr"`
From xsdtime.XSDDate `xml:"from,attr"`
To xsdtime.XSDDate `xml:"to,attr"`
Cert string `xml:"cert,attr"`
}
type URL struct {
XMLName xml.Name `xml:"url"`
Address string `xml:"address,attr"`

View File

@@ -8,22 +8,23 @@ type AgentRef struct {
}
type AdditionalRef struct {
XMLName xml.Name `xml:"beilage"`
Reference
Datum string `xml:"datum,attr"`
Nr int `xml:"nr,attr"`
AdditionalNo int `xml:"beilage,attr"`
Von int `xml:"von,attr"`
Bis int `xml:"bis,attr"`
XMLName xml.Name `xml:"beilage"`
Reference // Ist nicht im Schema
Datum string `xml:"datum,attr"`
Nr int `xml:"nr,attr"`
AdditionalNo int `xml:"beilage,attr"`
Von int `xml:"von,attr"`
Bis int `xml:"bis,attr"`
}
type IssueRef struct {
XMLName xml.Name `xml:"stueck"`
Reference
Datum string `xml:"datum,attr"`
Nr int `xml:"nr,attr"`
Von int `xml:"von,attr"`
Bis int `xml:"bis,attr"`
XMLName xml.Name `xml:"stueck"`
Reference // Ist nicht im Schema
DateAttributes
Nr int `xml:"nr,attr"`
Von int `xml:"von,attr"`
Bis int `xml:"bis,attr"`
Beilage int `xml:"beilage,attr"`
}
type PlaceRef struct {