Compare commits
6 Commits
v0.1.5
...
b496ac3691
Author | SHA1 | Date | |
---|---|---|---|
b496ac3691 | |||
13e7a16178 | |||
3734a3eb3d | |||
e0902d9ba4 | |||
434783165b | |||
46138d302b |
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()))
|
||||
}
|
||||
|
2
entry.go
2
entry.go
@ -213,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)
|
||||
}
|
||||
}
|
||||
|
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.
|
||||
|
18
link.go
18
link.go
@ -38,16 +38,22 @@ func (l *Link) Check() error {
|
||||
}
|
||||
}
|
||||
|
||||
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.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 !isValidMediaType(string(l.Type)) {
|
||||
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, 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 !isValidLanguageTag(l.HrefLang) {
|
||||
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
||||
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 {
|
||||
|
Reference in New Issue
Block a user