2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
2024-10-19 12:28:09 +02:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2024-10-19 14:12:51 +02:00
|
|
|
"fmt"
|
2024-10-19 12:28:09 +02:00
|
|
|
)
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
type CommonAttributes struct {
|
2024-10-19 12:28:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-17 20:48:32 +02:00
|
|
|
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
|
|
|
// *CommonAttributes.
|
2024-10-19 14:52:19 +02:00
|
|
|
func newCommonAttributes() *CommonAttributes {
|
2024-10-17 20:48:32 +02:00
|
|
|
return new(CommonAttributes)
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
// AddAttribute adds the attribute to the CommonAttributes. It returns an int.
|
|
|
|
func (c *CommonAttributes) AddAttribute(name, value string) int {
|
|
|
|
return addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
|
2024-10-19 14:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteAttribute deletes the attribute at index from the CommonAttributes. It
|
|
|
|
// return an error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (c *CommonAttributes) DeleteAttribute(index int) error {
|
|
|
|
if err := deleteFromSlice(&c.UndefinedAttributes, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", index, c, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-19 14:12:51 +02:00
|
|
|
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
|
|
|
|
// returns an error.
|
|
|
|
func (c *CommonAttributes) Check() error {
|
|
|
|
for i, u := range c.UndefinedAttributes {
|
|
|
|
if u.Name.Local == "" {
|
|
|
|
return fmt.Errorf("xml name of undefined attribute %v empty", i)
|
|
|
|
}
|
|
|
|
if u.Value == "" {
|
|
|
|
return fmt.Errorf("value of undefined attribute %v empty", i)
|
|
|
|
}
|
|
|
|
}
|
2024-10-19 12:28:09 +02:00
|
|
|
|
|
|
|
return nil
|
2024-10-17 20:48:32 +02:00
|
|
|
}
|