Compare commits

..

No commits in common. "17aa4b1a15982a08281f9649ca30c0f33777c450" and "4b97bf7fdc1ababd8fd8e141c55c11837468dc81" have entirely different histories.

7 changed files with 51 additions and 57 deletions

View File

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

View File

@ -3,6 +3,7 @@ package atom
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
) )
type InlineTextContent struct { type InlineTextContent struct {
@ -18,12 +19,11 @@ func newInlineTextContent(mediaType string, content any) (*InlineTextContent, er
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType) return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
} }
text, ok := content.(string) if reflect.TypeOf(content).Kind() != reflect.String {
if !ok {
return nil, fmt.Errorf("content type %T incompatible with inline text content", content) return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
} }
return &InlineTextContent{Type: mediaType, Text: text}, nil return &InlineTextContent{Type: mediaType, Text: content.(string)}, nil
} }
// isContent checks whether the InlineTextContent is a Content. It returns a // isContent checks whether the InlineTextContent is a Content. It returns a

View File

@ -3,12 +3,13 @@ package atom
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
) )
type InlineXHTMLContent struct { type InlineXHTMLContent struct {
*CommonAttributes *CommonAttributes
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
XHTMLDiv XHTMLDiv XHTMLDiv string `xml:"xhtmldiv"`
} }
// newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a // newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a
@ -18,12 +19,11 @@ func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent,
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType) return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
} }
xhtmlDiv, ok := content.(XHTMLDiv) if reflect.TypeOf(content).Kind() != reflect.String {
if !ok {
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content) return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
} }
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil
} }
// isContent checks whether the InlineXHTMLContent is a Content. It returns a // isContent checks whether the InlineXHTMLContent is a Content. It returns a
@ -44,8 +44,8 @@ func (i *InlineXHTMLContent) Check() error {
return errors.New("type attribute of inline xhtml content must be xhtml") return errors.New("type attribute of inline xhtml content must be xhtml")
} }
if err := i.XHTMLDiv.Check(); err != nil { if i.XHTMLDiv == "" {
return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err) return errors.New("xhtmlDiv element of inline xhtml content empty")
} }
return nil return nil

17
link.go
View File

@ -9,7 +9,7 @@ import (
type Link struct { type Link struct {
*CommonAttributes *CommonAttributes
Title string `xml:"title,attr,omitempty"` Title string `xml:"title,attr,omitempty"`
Content string `xml:",any"` // undefinedContent in RFC4287 Content Content `xml:"content"` // undefinedContent in RFC4287
Href IRI `xml:"href,attr"` Href IRI `xml:"href,attr"`
Rel string `xml:"rel,attr,omitempty"` Rel string `xml:"rel,attr,omitempty"`
Type MediaType `xml:"type,attr,omitempty"` Type MediaType `xml:"type,attr,omitempty"`
@ -18,7 +18,12 @@ type Link struct {
} }
// NewLink creates a new Link. It returns a *Link and an error. // NewLink creates a new Link. It returns a *Link and an error.
func NewLink(href, content string) (*Link, error) { 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: IRI(href), Content: content}, nil return &Link{Href: IRI(href), Content: content}, nil
} }
@ -51,8 +56,12 @@ func (l *Link) Check() error {
} }
} }
if l.Content == "" { if l.Content == nil {
return errors.New("content element of link empty") 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)
}
} }
return nil return nil

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"mime" "mime"
"reflect"
) )
type OutOfLineContent struct { type OutOfLineContent struct {
@ -19,16 +20,15 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType) return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
} }
iri, ok := content.(IRI) if reflect.TypeOf(content).Kind() != reflect.String {
if !ok {
return nil, fmt.Errorf("content type %T incompatible with out of line content", content) return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
} }
if !isValidIRI(iri) { if !isValidIRI(content.(IRI)) {
return nil, errors.New("content not a valid uri") return nil, errors.New("content not a valid uri")
} }
return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(IRI)}, nil
} }
// isContent checks whether the OutOfLineContent is a Content. It returns a // isContent checks whether the OutOfLineContent is a Content. It returns a

View File

@ -1,30 +0,0 @@
package atom
import (
"encoding/xml"
"errors"
)
type XHTMLDiv struct {
XMLName xml.Name `xml:"div"`
XMLNS string `xml:"xmlns,attr"`
Content string `xml:",innerxml"`
}
// NewXHTMLDiv creates a new XHTMLDiv. It returns a *XHTMLDiv.
func NewXHTMLDiv(content string) *XHTMLDiv {
return &XHTMLDiv{
XMLNS: "http://www.w3.org/1999/xhtml",
Content: content,
}
}
// Check checks the XHTMLDiv for incompatibilities with RFC4287. It returns an
// error.
func (x *XHTMLDiv) Check() error {
if x.XMLNS != "http://www.w3.org/1999/xhtml" {
return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
}
return nil
}

View File

@ -1,10 +1,16 @@
package atom package atom
import ( import (
"encoding/xml"
"errors" "errors"
"fmt"
) )
type XHTMLDiv struct {
XMLName xml.Name `xml:"div"`
XMLNS string `xml:"xmlns,attr"`
Content string `xml:",innerxml"`
}
type XHTMLText struct { type XHTMLText struct {
*CommonAttributes *CommonAttributes
Type string `xml:"type,attr"` // Must be xhtml Type string `xml:"type,attr"` // Must be xhtml
@ -21,8 +27,8 @@ func (x *XHTMLText) Check() error {
return errors.New("type attribute of xhtml text must be xhtml") return errors.New("type attribute of xhtml text must be xhtml")
} }
if err := x.XHTMLDiv.Check(); err != nil { if x.XHTMLDiv.XMLNS != "http://www.w3.org/1999/xhtml" {
return fmt.Errorf("xhtml div element %v of xhtml text %v: %v", x.XHTMLDiv, x, err) return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
} }
return nil return nil