Added more error handling and necessary functions
This commit is contained in:
44
link.go
44
link.go
@@ -9,17 +9,39 @@ import (
|
||||
type Link struct {
|
||||
XMLName xml.Name `xml:"link"`
|
||||
*CommonAttributes
|
||||
Title string `xml:"title,attr,omitempty"`
|
||||
Href IRI `xml:"href,attr"`
|
||||
Rel string `xml:"rel,attr,omitempty"`
|
||||
Type MediaType `xml:"type,attr,omitempty"`
|
||||
HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
|
||||
Length uint `xml:"length,attr,omitempty"`
|
||||
Title string `xml:"title,attr,omitempty"`
|
||||
Href string `xml:"href,attr"` // IRI
|
||||
Rel string `xml:"rel,attr,omitempty"`
|
||||
Type string `xml:"type,attr,omitempty"` // MediaType
|
||||
HrefLang string `xml:"hreflang,attr,omitempty"` // LanguageTag
|
||||
Length uint `xml:"length,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewLink creates a new Link. It returns a *Link.
|
||||
func NewLink(href string) *Link {
|
||||
return &Link{Href: IRI(href)}
|
||||
// NewLink creates a new Link. It returns a *Link and an error.
|
||||
func NewLink(href string) (*Link, error) {
|
||||
if !isValidIRI(href) {
|
||||
return nil, fmt.Errorf("href %v not correctly formatted", href)
|
||||
}
|
||||
|
||||
return &Link{Href: href}, nil
|
||||
}
|
||||
|
||||
// SetType sets the Type attribute of the Link. It returns an error.
|
||||
func (l *Link) SetType(t string) error {
|
||||
if !isValidMediaType(t) {
|
||||
return fmt.Errorf("type %v invalid media type", l)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHrefLang sets the HrefLang attribute of the Link. It returns an error.
|
||||
func (l *Link) SetHrefLang(h string) error {
|
||||
if !isValidLanguageTag(h) {
|
||||
return fmt.Errorf("hreflang %v invalid language tag", l)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Link for incompatibilities with RFC4287. It returns an
|
||||
@@ -34,13 +56,13 @@ func (l *Link) Check() error {
|
||||
}
|
||||
|
||||
if l.Rel != "" {
|
||||
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
|
||||
if strings.Contains(l.Rel, ":") && !isValidIRI(l.Rel) {
|
||||
return fmt.Errorf("rel attribute of link %v not correctly formatted", l)
|
||||
}
|
||||
}
|
||||
|
||||
if l.Type != "" {
|
||||
if !isValidMediaType(string(l.Type)) {
|
||||
if !isValidMediaType(l.Type) {
|
||||
return fmt.Errorf("type attribute of link %v invalid media type", l)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user