Use generics for Add and Delete Methods

This commit is contained in:
2024-10-20 12:03:26 +02:00
parent e0384904b4
commit a49e853efb
6 changed files with 78 additions and 198 deletions

View File

@@ -19,23 +19,15 @@ 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})
}
addToSlice(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
}
// DeleteAttribute deletes the attribute from the CommonAttributes. It return an
// error.
func (c *CommonAttributes) DeleteAttribute(id int) error {
length := len(c.UndefinedAttributes)
if id > length {
return fmt.Errorf("error deleting undefined attribute from common attributes %v: id %v out of range %v", c, id, length)
if err := deleteFromSlice(c.UndefinedAttributes, id); err != nil {
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err)
}
c.UndefinedAttributes = append(c.UndefinedAttributes[:id], c.UndefinedAttributes[id+1:]...)
return nil
}