From 17aa4b1a15982a08281f9649ca30c0f33777c450 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 17 Oct 2024 18:12:56 +0200 Subject: [PATCH] Streamline type check in content constructs --- inlineTextContent.go | 6 +++--- inlineXHTMLContent.go | 12 ++++++------ outOfLineContent.go | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/inlineTextContent.go b/inlineTextContent.go index 73dd477..a4557b6 100644 --- a/inlineTextContent.go +++ b/inlineTextContent.go @@ -3,7 +3,6 @@ package atom import ( "errors" "fmt" - "reflect" ) type InlineTextContent struct { @@ -19,11 +18,12 @@ func newInlineTextContent(mediaType string, content any) (*InlineTextContent, er return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType) } - if reflect.TypeOf(content).Kind() != reflect.String { + text, ok := content.(string) + if !ok { return nil, fmt.Errorf("content type %T incompatible with inline text content", content) } - return &InlineTextContent{Type: mediaType, Text: content.(string)}, nil + return &InlineTextContent{Type: mediaType, Text: text}, nil } // isContent checks whether the InlineTextContent is a Content. It returns a diff --git a/inlineXHTMLContent.go b/inlineXHTMLContent.go index 27775e9..6202681 100644 --- a/inlineXHTMLContent.go +++ b/inlineXHTMLContent.go @@ -3,13 +3,12 @@ package atom import ( "errors" "fmt" - "reflect" ) type InlineXHTMLContent struct { *CommonAttributes Type string `xml:"type,attr"` - XHTMLDiv string `xml:"xhtmldiv"` + XHTMLDiv XHTMLDiv } // newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a @@ -19,11 +18,12 @@ func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent, return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType) } - if reflect.TypeOf(content).Kind() != reflect.String { + xhtmlDiv, ok := content.(XHTMLDiv) + if !ok { return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content) } - return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil + return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil } // isContent checks whether the InlineXHTMLContent is a Content. It returns a @@ -44,8 +44,8 @@ func (i *InlineXHTMLContent) Check() error { return errors.New("type attribute of inline xhtml content must be xhtml") } - if i.XHTMLDiv == "" { - return errors.New("xhtmlDiv element of inline xhtml content empty") + if err := i.XHTMLDiv.Check(); err != nil { + return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err) } return nil diff --git a/outOfLineContent.go b/outOfLineContent.go index f95952d..3c44a75 100644 --- a/outOfLineContent.go +++ b/outOfLineContent.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "mime" - "reflect" ) type OutOfLineContent struct { @@ -20,15 +19,16 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType) } - if reflect.TypeOf(content).Kind() != reflect.String { + iri, ok := content.(IRI) + if !ok { return nil, fmt.Errorf("content type %T incompatible with out of line content", content) } - if !isValidIRI(content.(IRI)) { + if !isValidIRI(iri) { return nil, errors.New("content not a valid uri") } - return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(IRI)}, nil + return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil } // isContent checks whether the OutOfLineContent is a Content. It returns a