2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
2024-10-15 21:13:46 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"mime"
|
|
|
|
"reflect"
|
|
|
|
)
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
type OutOfLineContent struct {
|
|
|
|
*CommonAttributes
|
|
|
|
Type MediaType `xml:"type,attr,omitempty"`
|
2024-10-16 17:33:25 +02:00
|
|
|
SRC IRI `xml:"src,attr"`
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// newOutOfLineContent creates a new OutOfLineContent. It returns a
|
|
|
|
// *OutOfLineContent and an error.
|
2024-10-15 21:13:46 +02:00
|
|
|
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
|
|
|
|
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
|
|
|
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.TypeOf(content).Kind() != reflect.String {
|
|
|
|
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
|
|
|
}
|
|
|
|
|
2024-10-16 17:33:25 +02:00
|
|
|
if !isValidIRI(content.(IRI)) {
|
2024-10-15 21:13:46 +02:00
|
|
|
return nil, errors.New("content not a valid uri")
|
|
|
|
}
|
|
|
|
|
2024-10-16 17:33:25 +02:00
|
|
|
return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(IRI)}, nil
|
2024-10-15 21:13:46 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// isContent checks whether the OutOfLineContent is a Content. It returns a
|
|
|
|
// bool.
|
2024-10-15 19:32:14 +02:00
|
|
|
func (o *OutOfLineContent) isContent() bool { return true }
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// hasSRC checks whether the OutOfLineContent has a SRC attribute. It returns a
|
|
|
|
// bool.
|
2024-10-15 19:32:14 +02:00
|
|
|
func (o *OutOfLineContent) hasSRC() bool { return true }
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// getType returns the Type of the OutOfLineContent as a string.
|
2024-10-15 19:32:14 +02:00
|
|
|
func (o *OutOfLineContent) getType() string { return string(o.Type) }
|
2024-10-13 17:19:40 +02:00
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It
|
|
|
|
// returns an error.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (o *OutOfLineContent) Check() error {
|
2024-10-15 21:47:13 +02:00
|
|
|
mediaType := o.getType()
|
|
|
|
|
|
|
|
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
|
|
|
return fmt.Errorf("type attribute %v incompatible with out of line content", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isCompositeMediaType(mediaType) {
|
2024-10-15 19:53:17 +02:00
|
|
|
return errors.New("type attribute of out of line content must not be a composite type")
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
if o.SRC == "" {
|
|
|
|
return errors.New("src attribute of out of line content empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|