From da7cebf972b667d81d8f07140b62dad1a0b2c433 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 20 Oct 2024 09:15:26 +0200 Subject: [PATCH] Switch from slice to map in common attributes to handle undefined attributes mor easily --- commonAttributes.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/commonAttributes.go b/commonAttributes.go index 477ede5..9d6454b 100644 --- a/commonAttributes.go +++ b/commonAttributes.go @@ -3,12 +3,14 @@ package atom import ( "encoding/xml" "fmt" + + "github.com/google/uuid" ) type CommonAttributes struct { - Base string `xml:"base,attr,omitempty"` // IRI - Lang string `xml:"lang,attr,omitempty"` // LanguageTag - UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"` + UndefinedAttributes map[uuid.UUID]*xml.Attr `xml:",attr,omitempty"` + Base string `xml:"base,attr,omitempty"` // IRI + Lang string `xml:"lang,attr,omitempty"` // LanguageTag } // NewCommonAttributes creates a new set of CommonAttributes. It returns a @@ -20,11 +22,14 @@ func newCommonAttributes() *CommonAttributes { // AddAttribute adds the Attribute to the CommonAttributes. func (c *CommonAttributes) AddAttribute(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}) + c.UndefinedAttributes = make(map[uuid.UUID]*xml.Attr, 1) } + c.UndefinedAttributes[uuid.New()] = &xml.Attr{Name: xml.Name{Local: name}, Value: value} +} + +// DeleteAttribute deletes the Attribute from the CommonAttributes. +func (c *CommonAttributes) DeleteAttribute(id uuid.UUID) { + delete(c.UndefinedAttributes, id) } // Check checks the CommonAttributes for incompatibilities with RFC4287. It