Added more error handling and necessary functions

This commit is contained in:
2024-10-19 12:28:09 +02:00
parent d4e7bce5e2
commit f4dfd6d060
22 changed files with 317 additions and 122 deletions

View File

@@ -1,10 +1,13 @@
package atom
import "encoding/xml"
import (
"encoding/xml"
"errors"
)
type CommonAttributes struct {
Base IRI `xml:"base,attr,omitempty"`
Lang LanguageTag `xml:"lang,attr,omitempty"`
Base string `xml:"base,attr,omitempty"` // IRI
Lang string `xml:"lang,attr,omitempty"` // LanguageTag
UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"`
}
@@ -14,12 +17,21 @@ func NewCommonAttributes() *CommonAttributes {
return new(CommonAttributes)
}
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
// AddAttribute adds the Attribute to the CommonAttributes. It returns an error.
func (c *CommonAttributes) AddAttribute(name, value string) error {
if name == "" {
return errors.New("error adding attribute: name string empty")
}
if value == "" {
return errors.New("error adding attribute: value string empty")
}
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
}