Created NewLink

This commit is contained in:
Jason Streifling 2024-10-16 17:17:41 +02:00
parent bc9fd49d18
commit b76e529ca3
2 changed files with 14 additions and 5 deletions

View File

@ -8,7 +8,7 @@ import (
type Category struct {
*CommonAttributes
Content Content `xml:"content"` // should this even exist in here?
Content Content `xml:"content"` // undefinedContent in RFC4287
Term string `xml:"term,attr"`
Scheme URI `xml:"scheme,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`

17
link.go
View File

@ -7,8 +7,8 @@ import (
type Link struct {
*CommonAttributes
Title *Text `xml:"title,attr,omitempty"`
Content *Content `xml:"content"`
Title Text `xml:"title,attr,omitempty"`
Content Content `xml:"content"` // undefinedContent in RFC4287
Href URI `xml:"href,attr"`
Rel string `xml:"rel,attr,omitempty"`
Type MediaType `xml:"type,attr,omitempty"`
@ -16,13 +16,22 @@ type Link struct {
Length uint `xml:"length,attr,omitempty"`
}
func NewLink(href string) (*Link, error) {
content, err := NewContent(InlineText, "", "")
if err != nil {
return nil, fmt.Errorf("error creating content element: %v", err)
}
return &Link{Href: URI(href), Content: content}, nil
}
func (l *Link) Check() error {
if l.Href == "" {
return errors.New("href attribute of link empty")
}
if l.Title != nil {
if err := (*l.Title).Check(); err != nil {
if err := l.Title.Check(); err != nil {
return fmt.Errorf("title attribute of link %v: %v", l.Href, err)
}
}
@ -30,7 +39,7 @@ 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 {
if err := l.Content.Check(); err != nil {
return fmt.Errorf("content element of link %v: %v", l.Href, err)
}
}