From 9df834d92743a47dda4f1e9e01e5ff7d35ae0edc Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 15 Oct 2024 21:13:46 +0200 Subject: [PATCH] Add newOutOfLineContent --- outOfLineContent.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/outOfLineContent.go b/outOfLineContent.go index 17d4f64..dfc3871 100644 --- a/outOfLineContent.go +++ b/outOfLineContent.go @@ -1,6 +1,12 @@ package atomfeed -import "errors" +import ( + "errors" + "fmt" + "mime" + "net/url" + "reflect" +) type OutOfLineContent struct { *CommonAttributes @@ -8,6 +14,22 @@ type OutOfLineContent struct { 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) hasSRC() bool { return true }