atom/commonAttributes.go

38 lines
1.1 KiB
Go
Raw Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
import (
"encoding/xml"
"errors"
)
2024-10-13 17:19:40 +02:00
type CommonAttributes struct {
Base string `xml:"base,attr,omitempty"` // IRI
Lang string `xml:"lang,attr,omitempty"` // LanguageTag
2024-10-17 21:44:19 +02:00
UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"`
2024-10-13 17:19:40 +02:00
}
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
// *CommonAttributes.
func NewCommonAttributes() *CommonAttributes {
return new(CommonAttributes)
}
// 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 {
2024-10-17 21:44:19 +02:00
c.UndefinedAttributes = make([]*xml.Attr, 1)
c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
} else {
2024-10-17 21:44:19 +02:00
c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
}
return nil
}