Compare commits

..

3 Commits

2 changed files with 43 additions and 47 deletions

View File

@ -43,6 +43,16 @@ func (e *Entry) checkAuthors() error {
return nil return nil
} }
func alternateRelExists(l []*Link) bool {
for _, link := range l {
if link.Rel == "alternate" {
return true
}
}
return false
}
func (e *Entry) AddExtension(name string, value any) { func (e *Entry) AddExtension(name string, value any) {
e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}) e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
} }
@ -60,35 +70,33 @@ func (e *Entry) Check() error {
return fmt.Errorf("entry %v: %v", e.ID.URI, err) return fmt.Errorf("entry %v: %v", e.ID.URI, err)
} }
if e.Categories != nil {
for i, c := range e.Categories { for i, c := range e.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err)
} }
} }
}
if e.Content != nil { if e.Content != nil {
if err := (*e.Content).Check(); err != nil { if err := (*e.Content).Check(); err != nil {
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err) return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
} }
} else {
if !alternateRelExists(e.Links) {
return errors.New("no content element of entry %v and no link element with rel \"alternate\"")
}
} }
if e.Contributors != nil {
for i, c := range e.Contributors { for i, c := range e.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
} }
} }
}
if e.Links != nil {
for i, l := range e.Links { for i, l := range e.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err)
} }
} }
}
if e.Published != nil { if e.Published != nil {
if err := e.Published.Check(); err != nil { if err := e.Published.Check(); err != nil {
@ -130,13 +138,11 @@ func (e *Entry) Check() error {
} }
} }
if e.Extensions != nil {
for i, x := range e.Extensions { for i, x := range e.Extensions {
if err := x.Check(); err != nil { if err := x.Check(); err != nil {
return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err) return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err)
} }
} }
}
return nil return nil
} }

10
feed.go
View File

@ -156,21 +156,17 @@ func (f *Feed) Check() error {
} }
} }
if f.Categories != nil {
for i, c := range f.Categories { for i, c := range f.Categories {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("category element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("category element %v of feed %v: %v", i, f.ID.URI, err)
} }
} }
}
if f.Contributors != nil {
for i, c := range f.Contributors { for i, c := range f.Contributors {
if err := c.Check(); err != nil { if err := c.Check(); err != nil {
return fmt.Errorf("contributor element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("contributor element %v of feed %v: %v", i, f.ID.URI, err)
} }
} }
}
if f.Generator != nil { if f.Generator != nil {
if err := f.Generator.Check(); err != nil { if err := f.Generator.Check(); err != nil {
@ -184,13 +180,11 @@ func (f *Feed) Check() error {
} }
} }
if f.Links != nil {
for i, l := range f.Links { for i, l := range f.Links {
if err := l.Check(); err != nil { if err := l.Check(); err != nil {
return fmt.Errorf("link element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("link element %v of feed %v: %v", i, f.ID.URI, err)
} }
} }
}
if hasAlternateDuplicateLinks(f.Links) { if hasAlternateDuplicateLinks(f.Links) {
return errors.New("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found") return errors.New("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found")
} }
@ -229,21 +223,17 @@ func (f *Feed) Check() error {
} }
} }
if f.Extensions != nil {
for i, x := range f.Extensions { for i, x := range f.Extensions {
if err := x.Check(); err != nil { if err := x.Check(); err != nil {
return fmt.Errorf("extension element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("extension element %v of feed %v: %v", i, f.ID.URI, err)
} }
} }
}
if f.Entries != nil {
for i, n := range f.Entries { for i, n := range f.Entries {
if err := n.Check(); err != nil { if err := n.Check(); err != nil {
return fmt.Errorf("entry element %v of feed %v: %v", i, f.ID.URI, err) return fmt.Errorf("entry element %v of feed %v: %v", i, f.ID.URI, err)
} }
} }
}
return nil return nil
} }