From b70ff82141f9bc8762b0c5a58b5357867376d5d1 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 17 Oct 2024 21:08:41 +0200 Subject: [PATCH] Delete extensionAttribute.go and simplify undefined attributes --- atom.go | 6 ++++++ commonAttributes.go | 20 ++++++++++---------- extensionAttribute.go | 26 -------------------------- 3 files changed, 16 insertions(+), 36 deletions(-) delete mode 100644 extensionAttribute.go diff --git a/atom.go b/atom.go index b5e6671..6c855d1 100644 --- a/atom.go +++ b/atom.go @@ -82,6 +82,12 @@ func isValidLanguageTag(tag LanguageTag) bool { 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. func NewURN() IRI { return IRI(fmt.Sprint("urn:uuid:", uuid.New())) diff --git a/commonAttributes.go b/commonAttributes.go index 3c6473b..f5641cf 100644 --- a/commonAttributes.go +++ b/commonAttributes.go @@ -3,9 +3,9 @@ package atom import "fmt" type CommonAttributes struct { - Base IRI `xml:"base,attr,omitempty"` - Lang LanguageTag `xml:"lang,attr,omitempty"` - UndefinedAttributes []*ExtensionAttribute `xml:",any"` + Base IRI `xml:"base,attr,omitempty"` + Lang LanguageTag `xml:"lang,attr,omitempty"` + UndefinedAttributes []string `xml:",attr"` } // NewCommonAttributes creates a new set of CommonAttributes. It returns a @@ -15,21 +15,21 @@ func NewCommonAttributes() *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 { - c.UndefinedAttributes = make([]*ExtensionAttribute, 1) - c.UndefinedAttributes[0] = e + c.UndefinedAttributes = make([]string, 1) + c.UndefinedAttributes[0] = fmt.Sprint(name, `="`, value, `"`) } else { - c.UndefinedAttributes = append(c.UndefinedAttributes, e) + c.UndefinedAttributes = append(c.UndefinedAttributes, fmt.Sprint(name, `="`, 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) + for _, a := range c.UndefinedAttributes { + if !isValidAttribute(a) { + return fmt.Errorf("attribute %v of undefined attributes of common attributes not correctly formatted", a) } } diff --git a/extensionAttribute.go b/extensionAttribute.go deleted file mode 100644 index 8e38700..0000000 --- a/extensionAttribute.go +++ /dev/null @@ -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 -}