Streamline type check in content constructs

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

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