Compare commits

..

No commits in common. "7764589dd3a2df61379e4cb02d36eca8daec4868" and "987feb822615886361262a7bb1bf811a1f821470" have entirely different histories.

7 changed files with 9 additions and 111 deletions

View File

@ -1,34 +1,8 @@
package atomfeed package atomfeed
import (
"fmt"
)
const (
InlineText = iota
InlineXHTML
InlineOther
OutOfLine
)
type Content interface { type Content interface {
isContent() bool isContent() bool
hasSRC() bool hasSRC() bool
getType() string getType() string
Check() error Check() error
} }
func NewContent(contentType int, mediaType string, content any) (Content, error) {
switch contentType {
case 0:
return newInlineTextContent(mediaType, content)
case 1:
return newInlineXHTMLContent(mediaType, content)
case 2:
return newInlineOtherContent(mediaType, content)
case 3:
return newOutOfLineContent(mediaType, content)
default:
return nil, fmt.Errorf("%v is not a valid text type", contentType)
}
}

View File

@ -1,23 +1,11 @@
package atomfeed package atomfeed
import ( import "errors"
"errors"
"fmt"
"mime"
)
type InlineOtherContent struct { type InlineOtherContent struct {
*CommonAttributes *CommonAttributes
AnyElement any `xml:"anyelement,omitempty"`
Type MediaType `xml:"type,attr,omitempty"` Type MediaType `xml:"type,attr,omitempty"`
} AnyElement []*any `xml:"anyelement,omitempty"`
func newInlineOtherContent(mediaType string, content any) (*InlineOtherContent, error) {
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return nil, fmt.Errorf("media type %v incompatible with inline other content", mediaType)
}
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
} }
func (i *InlineOtherContent) isContent() bool { return true } func (i *InlineOtherContent) isContent() bool { return true }

View File

@ -1,10 +1,6 @@
package atomfeed package atomfeed
import ( import "errors"
"errors"
"fmt"
"reflect"
)
type InlineTextContent struct { type InlineTextContent struct {
*CommonAttributes *CommonAttributes
@ -12,30 +8,6 @@ type InlineTextContent struct {
Texts []string `xml:"texts,omitempty"` Texts []string `xml:"texts,omitempty"`
} }
func newInlineTextContent(mediaType string, content any) (*InlineTextContent, error) {
if mediaType != "text" && mediaType != "html" && mediaType != "" {
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
}
texts := make([]string, 0)
t := reflect.TypeOf(content)
switch t.Kind() {
case reflect.Slice:
if t.Elem().Kind() == reflect.String {
for _, t := range content.([]string) {
texts = append(texts, t)
}
}
case reflect.String:
texts = append(texts, content.(string))
default:
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
}
return &InlineTextContent{Type: mediaType, Texts: texts}, nil
}
func (i *InlineTextContent) isContent() bool { return true } func (i *InlineTextContent) isContent() bool { return true }
func (i *InlineTextContent) hasSRC() bool { return false } func (i *InlineTextContent) hasSRC() bool { return false }

View File

@ -1,10 +1,6 @@
package atomfeed package atomfeed
import ( import "errors"
"errors"
"fmt"
"reflect"
)
type InlineXHTMLContent struct { type InlineXHTMLContent struct {
*CommonAttributes *CommonAttributes
@ -12,18 +8,6 @@ type InlineXHTMLContent struct {
XHTMLDiv string `xml:"xhtmldiv"` XHTMLDiv string `xml:"xhtmldiv"`
} }
func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent, error) {
if mediaType != "xhtml" {
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
}
if reflect.TypeOf(content).Kind() != reflect.String {
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
}
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil
}
func (i *InlineXHTMLContent) isContent() bool { return true } func (i *InlineXHTMLContent) isContent() bool { return true }
func (i *InlineXHTMLContent) hasSRC() bool { return false } func (i *InlineXHTMLContent) hasSRC() bool { return false }

View File

@ -1,12 +1,6 @@
package atomfeed package atomfeed
import ( import "errors"
"errors"
"fmt"
"mime"
"net/url"
"reflect"
)
type OutOfLineContent struct { type OutOfLineContent struct {
*CommonAttributes *CommonAttributes
@ -14,22 +8,6 @@ type OutOfLineContent struct {
SRC URI `xml:"src,attr"` SRC URI `xml:"src,attr"`
} }
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
}
if reflect.TypeOf(content).Kind() != reflect.String {
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
}
if _, err := url.ParseRequestURI(content.(string)); err != nil {
return nil, errors.New("content not a valid uri")
}
return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(URI)}, nil
}
func (o *OutOfLineContent) isContent() bool { return true } func (o *OutOfLineContent) isContent() bool { return true }
func (o *OutOfLineContent) hasSRC() bool { return true } func (o *OutOfLineContent) hasSRC() bool { return true }

View File

@ -31,7 +31,7 @@ func (p *Person) Check() error {
if p.URI != "" { if p.URI != "" {
if _, err := url.ParseRequestURI(string(p.URI)); err != nil { if _, err := url.ParseRequestURI(string(p.URI)); err != nil {
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name) return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
} }
} }

View File

@ -12,7 +12,7 @@ type Text interface {
func NewText(textType, content string) (Text, error) { func NewText(textType, content string) (Text, error) {
switch textType { switch textType {
case "text", "": case "text":
return &PlainText{Type: textType, Text: content}, nil return &PlainText{Type: textType, Text: content}, nil
case "html": case "html":
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
@ -24,6 +24,8 @@ func NewText(textType, content string) (Text, error) {
Content: content, Content: content,
}, },
}, nil }, nil
case "":
return &PlainText{Type: "text", Text: content}, nil
default: default:
return nil, fmt.Errorf("%v is not a valid text type", textType) return nil, fmt.Errorf("%v is not a valid text type", textType)
} }