From 31b6e51cb80d5a9b0f3961bf5508af5283ddfb29 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 15 Oct 2024 20:47:16 +0200 Subject: [PATCH] Add newInlineTextContent --- inlineTextContent.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/inlineTextContent.go b/inlineTextContent.go index be94161..fedf747 100644 --- a/inlineTextContent.go +++ b/inlineTextContent.go @@ -1,6 +1,10 @@ package atomfeed -import "errors" +import ( + "errors" + "fmt" + "reflect" +) type InlineTextContent struct { *CommonAttributes @@ -14,6 +18,30 @@ func (i *InlineTextContent) hasSRC() bool { return false } 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 { 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")