Add newInlineTextContent
This commit is contained in:
parent
4bad8ae99f
commit
31b6e51cb8
@ -1,6 +1,10 @@
|
|||||||
package atomfeed
|
package atomfeed
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
type InlineTextContent struct {
|
type InlineTextContent struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
@ -14,6 +18,30 @@ func (i *InlineTextContent) hasSRC() bool { return false }
|
|||||||
|
|
||||||
func (i *InlineTextContent) getType() string { return i.Type }
|
func (i *InlineTextContent) getType() string { return i.Type }
|
||||||
|
|
||||||
|
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) Check() error {
|
func (i *InlineTextContent) Check() error {
|
||||||
if i.Type != "" && i.Type != "text" && i.Type != "html" {
|
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 errors.New("type attribute of inline text content must be text or html if not omitted")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user