atom/category.go

45 lines
1006 B
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"
2024-10-13 17:19:40 +02:00
"fmt"
)
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.
func NewCategory(term string) *Category {
return &Category{Term: term}
2024-10-13 17:19:40 +02:00
}
// SetLabel sets the Label attribute of the Category.
func (c *Category) SetLabel(label string) {
c.Label = Unescape(label)
}
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
}