Create needed functions for common attributes and their extensions

This commit is contained in:
Jason Streifling 2024-10-17 20:48:32 +02:00
parent 3b3f1f7e41
commit 3172a4865a
3 changed files with 25 additions and 6 deletions

View File

@ -8,6 +8,22 @@ type CommonAttributes struct {
UndefinedAttributes []*ExtensionAttribute `xml:",any"`
}
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
// *CommonAttributes.
func NewCommonAttributes() *CommonAttributes {
return new(CommonAttributes)
}
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
func (c *CommonAttributes) AddExtensionAttribute(e *ExtensionAttribute) {
if c.UndefinedAttributes == nil {
c.UndefinedAttributes = make([]*ExtensionAttribute, 1)
c.UndefinedAttributes[0] = e
} else {
c.UndefinedAttributes = append(c.UndefinedAttributes, e)
}
}
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
// returns an error.
func (c *CommonAttributes) Check() error {

View File

@ -1,20 +1,24 @@
package atom
import (
"encoding/xml"
"errors"
"fmt"
)
// TODO: Is this really correct?
type ExtensionAttribute struct {
Value any `xml:",attr"`
XMLName xml.Name
Attr string `xml:",attr"`
}
// NewExtensionAttribute creates a new ExtensionAttribute. It returns a
// *ExtensionAttribute.
func NewExtensionAttribute(name, value string) *ExtensionAttribute {
return &ExtensionAttribute{Attr: fmt.Sprint(name, `="`, value, `"`)}
}
// Check checks the ExtensionAttribute for incompatibilities with RFC4287. It
// returns an error.
func (e *ExtensionAttribute) Check() error {
if e.Value == nil {
if e.Attr == "" {
return errors.New("value element of extension attribute empty")
}

View File

@ -5,7 +5,6 @@ import (
"errors"
)
// TODO: Is this really correct?
type ExtensionElement struct {
Value any `xml:",innerxml"`
XMLName xml.Name