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 {
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html
Texts []string `xml:",chardata"`
Type string `xml:"type,attr,omitempty"` // Must be text or html
Text string `xml:",chardata"`
}
// 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)
}
texts := make([]string, 0)
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:
if reflect.TypeOf(content).Kind() != reflect.String {
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