atom/inlineTextContent.go

52 lines
1.3 KiB
Go

package atomfeed
import (
"errors"
"fmt"
"reflect"
)
type InlineTextContent struct {
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html
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) hasSRC() bool { return false }
func (i *InlineTextContent) getType() string { return i.Type }
func (i *InlineTextContent) Check() error {
if i.Type != "" && i.Type != "text" && i.Type != "html" {
return errors.New("type attribute of inline text content must be text or html if not omitted")
}
return nil
}