5 Commits

5 changed files with 26 additions and 38 deletions

View File

@ -1,4 +1,3 @@
# atom-feed # atom
An extensible implementation of an Atom feed that aims to be very close to RFC4287. An extensible implementation of an Atom feed that aims to be very close to RFC4287.

View File

@ -82,6 +82,12 @@ func isValidLanguageTag(tag LanguageTag) bool {
return err == nil return err == nil
} }
// isValidAttribute checks whether an Attribute is valid. It returns a bool.
func isValidAttribute(attribute string) bool {
regex := regexp.MustCompile(`^[a-zA-Z0-9_]+="[^"]*"$`)
return regex.MatchString(attribute)
}
// NewURN generates an new valid IRI based on a UUID. It returns an IRI. // NewURN generates an new valid IRI based on a UUID. It returns an IRI.
func NewURN() IRI { func NewURN() IRI {
return IRI(fmt.Sprint("urn:uuid:", uuid.New())) return IRI(fmt.Sprint("urn:uuid:", uuid.New()))

View File

@ -1,21 +1,27 @@
package atom package atom
import "fmt" import (
"encoding/xml"
)
type CommonAttributes struct { type CommonAttributes struct {
Base IRI `xml:"base,attr,omitempty"` Base IRI `xml:"base,attr,omitempty"`
Lang LanguageTag `xml:"lang,attr,omitempty"` Lang LanguageTag `xml:"lang,attr,omitempty"`
UndefinedAttributes []*ExtensionAttribute `xml:",any"` UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"`
} }
// Check checks the CommonAttributes for incompatibilities with RFC4287. It // NewCommonAttributes creates a new set of CommonAttributes. It returns a
// returns an error. // *CommonAttributes.
func (c *CommonAttributes) Check() error { func NewCommonAttributes() *CommonAttributes {
for i, e := range c.UndefinedAttributes { return new(CommonAttributes)
if err := e.Check(); err != nil { }
return fmt.Errorf("extension attribute %v of common attributes: %v", i, err)
} // AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
if c.UndefinedAttributes == nil {
c.UndefinedAttributes = make([]*xml.Attr, 1)
c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
} else {
c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
} }
return nil
} }

View File

@ -1,22 +0,0 @@
package atom
import (
"encoding/xml"
"errors"
)
// TODO: Is this really correct?
type ExtensionAttribute struct {
Value any `xml:",attr"`
XMLName xml.Name
}
// Check checks the ExtensionAttribute for incompatibilities with RFC4287. It
// returns an error.
func (e *ExtensionAttribute) Check() error {
if e.Value == nil {
return errors.New("value element of extension attribute empty")
}
return nil
}

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
) )
// TODO: Is this really correct?
type ExtensionElement struct { type ExtensionElement struct {
Value any `xml:",innerxml"` Value any `xml:",innerxml"`
XMLName xml.Name XMLName xml.Name