atom/inlineTextContent.go

61 lines
1.7 KiB
Go
Raw Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
2024-10-15 20:47:16 +02:00
import (
"errors"
"fmt"
"reflect"
)
2024-10-13 17:19:40 +02:00
type InlineTextContent struct {
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html
Texts []string `xml:"texts,omitempty"`
}
2024-10-16 19:59:28 +02:00
// newInlineTextContent creates a new InlineTextContent. It returns a
// *InlineTextContent and an error.
2024-10-15 20:47:16 +02:00
func newInlineTextContent(mediaType string, content any) (*InlineTextContent, error) {
if mediaType != "text" && mediaType != "html" && mediaType != "" {
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:
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
}
return &InlineTextContent{Type: mediaType, Texts: texts}, nil
}
2024-10-16 19:59:28 +02:00
// isContent checks whether the InlineTextContent is a Content. It returns a
// bool.
2024-10-15 20:52:35 +02:00
func (i *InlineTextContent) isContent() bool { return true }
2024-10-16 19:59:28 +02:00
// hasSRC checks whether the InlineTextContent has a SRC attribute. It returns
// a bool.
2024-10-15 20:52:35 +02:00
func (i *InlineTextContent) hasSRC() bool { return false }
2024-10-16 19:59:28 +02:00
// getType returns the Type of the InlineTextContent as a string.
2024-10-15 20:52:35 +02:00
func (i *InlineTextContent) getType() string { return i.Type }
2024-10-16 19:59:28 +02:00
// Check checks the InlineTextContent for incompatibilities with RFC4287. It
// returns an error.
2024-10-13 17:19:40 +02:00
func (i *InlineTextContent) Check() error {
if i.Type != "" && i.Type != "text" && i.Type != "html" {
return errors.New("type attribute of inline text content must be text or html if not omitted")
}
return nil
}