Compare commits

..

3 Commits

7 changed files with 57 additions and 51 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

View File

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

View File

@ -3,13 +3,12 @@ package atom
import (
"errors"
"fmt"
"reflect"
)
type InlineXHTMLContent struct {
*CommonAttributes
Type string `xml:"type,attr"`
XHTMLDiv string `xml:"xhtmldiv"`
XHTMLDiv XHTMLDiv
}
// newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a
@ -19,11 +18,12 @@ func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent,
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
}
if reflect.TypeOf(content).Kind() != reflect.String {
xhtmlDiv, ok := content.(XHTMLDiv)
if !ok {
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
}
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil
}
// 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")
}
if i.XHTMLDiv == "" {
return errors.New("xhtmlDiv element of inline xhtml content empty")
if err := i.XHTMLDiv.Check(); err != nil {
return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err)
}
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

View File

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

30
xhtmlDiv.go Normal file
View File

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