From 2d1d95502f495413a780fcb7a154e14828f08d53 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 15 Oct 2024 15:36:11 +0200 Subject: [PATCH] Create function to safely create new text construct --- text.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/text.go b/text.go index 2a5ec6c..34e31a6 100644 --- a/text.go +++ b/text.go @@ -1,6 +1,23 @@ package atomfeed +import ( + "fmt" +) + type Text interface { Check() error IsText() bool } + +func NewText(textType, content string) (Text, error) { + switch textType { + case "text", "html": + return &PlainText{Type: textType, Text: content}, nil + case "xhtml": + return &XHTMLText{Type: textType, XHTMLDiv: content}, nil + case "": + return &PlainText{Type: "text", Text: content}, nil + default: + return nil, fmt.Errorf("%v is not a valid text type", textType) + } +}