Compare commits
7 Commits
v0.1.4
...
b496ac3691
Author | SHA1 | Date | |
---|---|---|---|
b496ac3691 | |||
13e7a16178 | |||
3734a3eb3d | |||
e0902d9ba4 | |||
434783165b | |||
46138d302b | |||
6b9d5be734 |
20
atom.go
20
atom.go
@ -1,10 +1,12 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mime"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
@ -61,8 +63,17 @@ func isXMLMediaType(mediaType string) bool {
|
||||
// isValidMediaType checks whether a string is a valid media type. It returns a
|
||||
// bool.
|
||||
func isValidMediaType(mediaType string) bool {
|
||||
_, _, err := mime.ParseMediaType(mediaType)
|
||||
return err == nil
|
||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
typeParts := strings.Split(mediaType, "/")
|
||||
if len(typeParts) != 2 || typeParts[0] == "" || typeParts[1] == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// isValidLanguageTag checks whether a LanguageTag is valid. It returns a bool.
|
||||
@ -70,3 +81,8 @@ func isValidLanguageTag(tag LanguageTag) bool {
|
||||
_, err := language.Parse(string(tag))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// NewURN generates an new valid IRI based on a UUID. It returns an IRI.
|
||||
func NewURN() IRI {
|
||||
return IRI(fmt.Sprint("urn:uuid:", uuid.New()))
|
||||
}
|
||||
|
8
entry.go
8
entry.go
@ -34,14 +34,16 @@ type Entry struct {
|
||||
// the atom:entry contains an atom:source element that contains an atom:author
|
||||
// element or, in an Atom Feed Document, the atom:feed element contains an
|
||||
// atom:author element itself.
|
||||
func (e *Entry) checkAuthors() error {
|
||||
func (e *Entry) checkAuthors(authorInFeed bool) error {
|
||||
if e.Authors == nil {
|
||||
if !authorInFeed {
|
||||
if e.Source == nil {
|
||||
return errors.New("no authors set in entry")
|
||||
}
|
||||
if e.Source.Authors == nil {
|
||||
return errors.New("no authors set in entry")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i, a := range e.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
@ -138,7 +140,7 @@ func (e *Entry) Check() error {
|
||||
}
|
||||
}
|
||||
|
||||
if err := e.checkAuthors(); err != nil {
|
||||
if err := e.checkAuthors(true); err != nil {
|
||||
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
|
||||
@ -211,7 +213,7 @@ func (e *Entry) Check() error {
|
||||
// is not an XML media type [RFC3023], does not begin with "text/", and
|
||||
// does not end with "/xml" or "+xml".
|
||||
mediaType := e.Content.getType()
|
||||
if !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
||||
if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
||||
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
|
||||
}
|
||||
}
|
||||
|
2
feed.go
2
feed.go
@ -128,7 +128,7 @@ func (f *Feed) Check() error {
|
||||
// least one atom:author element.
|
||||
if f.Authors == nil {
|
||||
for _, e := range f.Entries {
|
||||
if err := e.checkAuthors(); err != nil {
|
||||
if err := e.checkAuthors(false); err != nil {
|
||||
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
6
id.go
6
id.go
@ -3,8 +3,6 @@ package atom
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ID struct {
|
||||
@ -13,8 +11,8 @@ type ID struct {
|
||||
}
|
||||
|
||||
// NewID creates a new ID. It returns a *ID.
|
||||
func NewID() *ID {
|
||||
return &ID{URI: IRI(fmt.Sprint("urn:uuid:", uuid.New()))}
|
||||
func NewID(id string) *ID {
|
||||
return &ID{URI: IRI(id)}
|
||||
}
|
||||
|
||||
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||
|
8
link.go
8
link.go
@ -38,17 +38,23 @@ func (l *Link) Check() error {
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(l.Rel, ":") || !isValidIRI(IRI(l.Rel)) {
|
||||
if l.Rel != "" {
|
||||
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
|
||||
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
|
||||
}
|
||||
}
|
||||
|
||||
if l.Type != "" {
|
||||
if !isValidMediaType(string(l.Type)) {
|
||||
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
|
||||
}
|
||||
}
|
||||
|
||||
if l.HrefLang != "" {
|
||||
if !isValidLanguageTag(l.HrefLang) {
|
||||
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
||||
}
|
||||
}
|
||||
|
||||
if l.Content == nil {
|
||||
return fmt.Errorf("no content element of link %v", l.Href)
|
||||
|
Reference in New Issue
Block a user