Streamline type check in content constructs

This commit is contained in:
Jason Streifling 2024-10-17 18:12:56 +02:00
parent 040d7a6b7b
commit 17aa4b1a15
3 changed files with 13 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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