Simplify inline text content

This commit is contained in:
Jason Streifling 2024-10-17 17:33:33 +02:00
parent 86785be588
commit 4b97bf7fdc

View File

@ -8,8 +8,8 @@ import (
type InlineTextContent struct { type InlineTextContent struct {
*CommonAttributes *CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html Type string `xml:"type,attr,omitempty"` // Must be text or html
Texts []string `xml:",chardata"` Text string `xml:",chardata"`
} }
// newInlineTextContent creates a new InlineTextContent. It returns a // newInlineTextContent creates a new InlineTextContent. It returns a
@ -19,23 +19,11 @@ func newInlineTextContent(mediaType string, content any) (*InlineTextContent, er
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType) return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
} }
texts := make([]string, 0) if reflect.TypeOf(content).Kind() != reflect.String {
t := reflect.TypeOf(content)
switch t.Kind() {
case reflect.Slice:
if t.Elem().Kind() == reflect.String {
for _, t := range content.([]string) {
texts = append(texts, t)
}
}
case reflect.String:
texts = append(texts, content.(string))
default:
return nil, fmt.Errorf("content type %T incompatible with inline text content", content) return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
} }
return &InlineTextContent{Type: mediaType, Texts: texts}, nil return &InlineTextContent{Type: mediaType, Text: content.(string)}, nil
} }
// isContent checks whether the InlineTextContent is a Content. It returns a // isContent checks whether the InlineTextContent is a Content. It returns a