Make undefined content in category and link type string

This commit is contained in:
Jason Streifling 2024-10-17 18:11:06 +02:00
parent 4b97bf7fdc
commit 705b651f08
2 changed files with 11 additions and 29 deletions

View File

@ -8,19 +8,14 @@ import (
type Category struct {
*CommonAttributes
Content Content `xml:"content"` // undefinedContent in RFC4287
Content string `xml:",any"` // undefinedContent in RFC4287
Term string `xml:"term,attr"`
Scheme IRI `xml:"scheme,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`
}
// NewCategory creates a new Category. It returns a *Category and an error.
func NewCategory(term string) (*Category, error) {
content, err := NewContent(InlineText, "", "")
if err != nil {
return nil, fmt.Errorf("error creating content element: %v", err)
}
func NewCategory(term, content string) (*Category, error) {
return &Category{Term: term, Content: content}, nil
}
@ -46,12 +41,8 @@ func (c *Category) Check() error {
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
}
if c.Content == nil {
return errors.New("no content element of category")
} else {
if err := c.Content.Check(); err != nil {
return fmt.Errorf("content element of category: %v", err)
}
if c.Content == "" {
return errors.New("content element of category empty")
}
return nil

17
link.go
View File

@ -9,7 +9,7 @@ import (
type Link struct {
*CommonAttributes
Title string `xml:"title,attr,omitempty"`
Content Content `xml:"content"` // undefinedContent in RFC4287
Content string `xml:",any"` // undefinedContent in RFC4287
Href IRI `xml:"href,attr"`
Rel string `xml:"rel,attr,omitempty"`
Type MediaType `xml:"type,attr,omitempty"`
@ -18,12 +18,7 @@ type Link struct {
}
// NewLink creates a new Link. It returns a *Link and an error.
func NewLink(href string) (*Link, error) {
content, err := NewContent(InlineText, "", "")
if err != nil {
return nil, fmt.Errorf("error creating content element: %v", err)
}
func NewLink(href, content string) (*Link, error) {
return &Link{Href: IRI(href), Content: content}, nil
}
@ -56,12 +51,8 @@ func (l *Link) Check() error {
}
}
if l.Content == nil {
return fmt.Errorf("no content element of link %v", l.Href)
} else {
if err := l.Content.Check(); err != nil {
return fmt.Errorf("content element of link %v: %v", l.Href, err)
}
if l.Content == "" {
return errors.New("content element of link empty")
}
return nil