Compare commits

...

3 Commits

2 changed files with 20 additions and 6 deletions

20
atom.go
View File

@ -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()))
}

6
id.go
View File

@ -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.