Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
9445a7c4cd | |||
42416d13e7 | |||
b70ff82141 |
6
atom.go
6
atom.go
@ -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()))
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
||||||
@ -15,23 +17,11 @@ func NewCommonAttributes() *CommonAttributes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
|
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
|
||||||
func (c *CommonAttributes) AddExtensionAttribute(e *ExtensionAttribute) {
|
func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
|
||||||
if c.UndefinedAttributes == nil {
|
if c.UndefinedAttributes == nil {
|
||||||
c.UndefinedAttributes = make([]*ExtensionAttribute, 1)
|
c.UndefinedAttributes = make([]*xml.Attr, 1)
|
||||||
c.UndefinedAttributes[0] = e
|
c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
|
||||||
} else {
|
} else {
|
||||||
c.UndefinedAttributes = append(c.UndefinedAttributes, e)
|
c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
|
|
||||||
// returns an error.
|
|
||||||
func (c *CommonAttributes) Check() error {
|
|
||||||
for i, e := range c.UndefinedAttributes {
|
|
||||||
if err := e.Check(); err != nil {
|
|
||||||
return fmt.Errorf("extension attribute %v of common attributes: %v", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
package atom
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ExtensionAttribute struct {
|
|
||||||
Attr string `xml:",attr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewExtensionAttribute creates a new ExtensionAttribute. It returns a
|
|
||||||
// *ExtensionAttribute.
|
|
||||||
func NewExtensionAttribute(name, value string) *ExtensionAttribute {
|
|
||||||
return &ExtensionAttribute{Attr: fmt.Sprint(name, `="`, value, `"`)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check checks the ExtensionAttribute for incompatibilities with RFC4287. It
|
|
||||||
// returns an error.
|
|
||||||
func (e *ExtensionAttribute) Check() error {
|
|
||||||
if e.Attr == "" {
|
|
||||||
return errors.New("value element of extension attribute empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
Reference in New Issue
Block a user