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

@@ -2,6 +2,7 @@ package atom
import (
"encoding/xml"
"errors"
"fmt"
"html"
)
@@ -9,14 +10,27 @@ import (
type Generator struct {
XMLName xml.Name `xml:"generator"`
*CommonAttributes
URI IRI `xml:"uri,attr,omitempty"`
URI string `xml:"uri,attr,omitempty"` // IRI
Version string `xml:"version,attr,omitempty"`
Text string `xml:",chardata"`
}
// NewGenerator creates a new Generator. It returns a *Generator.
func NewGenerator(text string) *Generator {
return &Generator{Text: html.UnescapeString(text)}
// NewGenerator creates a new Generator. It returns a *Generator and an error.
func NewGenerator(text string) (*Generator, error) {
if text == "" {
return nil, errors.New("error creating new generator: text string empty")
}
return &Generator{Text: html.UnescapeString(text)}, nil
}
// SetURI sets the URI attribute of the Generator. It returns an error.
func (g *Generator) SetURI(uri string) error {
if !isValidIRI(uri) {
return fmt.Errorf("uri %v not correctly formatted", g)
}
return nil
}
// Check checks the Generator for incompatibilities with RFC4287. It returns an