atom/category.go

76 lines
1.7 KiB
Go
Raw Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
import (
2024-10-17 20:10:18 +02:00
"encoding/xml"
"errors"
2024-10-13 17:19:40 +02:00
"fmt"
"html"
2024-10-13 17:19:40 +02:00
)
type Category struct {
2024-10-17 20:10:18 +02:00
XMLName xml.Name `xml:"category"`
2024-10-13 17:19:40 +02:00
*CommonAttributes
Term string `xml:"term,attr"`
Scheme string `xml:"scheme,attr,omitempty"` // IRI
Label string `xml:"label,attr,omitempty"`
2024-10-15 22:17:10 +02:00
}
// NewCategory creates a new Category. It returns a *Category and an error.
func NewCategory(term string) (*Category, error) {
if term == "" {
return nil, errors.New("error creating new category: term string empty")
}
return &Category{Term: term}, nil
}
// SetTerm sets the Term attribute of the Category. It returns an error.
func (c *Category) SetTerm(t string) error {
if t == "" {
return errors.New("error setting term of category: t string empty")
}
c.Term = t
return nil
2024-10-13 17:19:40 +02:00
}
// SetScheme sets the Scheme attribute of the Category. It returns an error.
func (c *Category) SetScheme(s string) error {
if !isValidIRI(s) {
return fmt.Errorf("scheme %v not correctly formatted", s)
}
c.Scheme = s
return nil
}
// SetLabel sets the Label attribute of the Category. It returns an error.
func (c *Category) SetLabel(label string) error {
if label == "" {
return errors.New("error setting label of category: label string empty")
}
c.Label = html.UnescapeString(label)
return nil
}
2024-10-16 19:59:28 +02:00
// Check checks the Category for incompatibilities with RFC4287. It returns an
// error.
2024-10-13 17:19:40 +02:00
func (c *Category) Check() error {
if c.Term == "" {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("term attribute of category %v empty", c)
2024-10-13 17:19:40 +02:00
}
if c.Scheme != "" {
if !isValidIRI(c.Scheme) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c)
}
}
if !isCorrectlyEscaped(c.Label) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("label attribute of category %v not correctly escaped", c)
}
2024-10-13 17:19:40 +02:00
return nil
}