atom/inlineTextContent.go

49 lines
1.5 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
2024-10-17 17:33:33 +02:00
Type string `xml:"type,attr,omitempty"` // Must be text or html
Text string `xml:",chardata"`
2024-10-13 17:19:40 +02:00
}
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)
}
2024-10-17 17:33:33 +02:00
if reflect.TypeOf(content).Kind() != reflect.String {
2024-10-15 20:47:16 +02:00
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
}
2024-10-17 17:33:33 +02:00
return &InlineTextContent{Type: mediaType, Text: content.(string)}, nil
2024-10-15 20:47:16 +02:00
}
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
}