atom/id.go

36 lines
681 B
Go
Raw Permalink Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
2024-10-15 17:26:26 +02:00
import (
2024-10-17 20:10:18 +02:00
"encoding/xml"
2024-10-15 17:26:26 +02:00
"errors"
"fmt"
)
2024-10-13 17:19:40 +02:00
type ID struct {
2024-10-17 20:10:18 +02:00
XMLName xml.Name `xml:"id"`
2024-10-13 17:19:40 +02:00
*CommonAttributes
URI IRI `xml:",chardata"`
2024-10-13 17:19:40 +02:00
}
2024-10-17 19:28:09 +02:00
// NewID creates a new ID. It returns a *ID and an error.
func NewID(uri IRI) (*ID, error) {
if !isValidIRI(uri) {
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
}
return &ID{URI: IRI(uri)}, nil
2024-10-15 17:26:26 +02:00
}
2024-10-16 19:59:28 +02:00
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
2024-10-13 17:19:40 +02:00
func (i *ID) Check() error {
if i.URI == "" {
return errors.New("uri element of id empty")
2024-10-16 17:07:57 +02:00
} else {
if !isValidIRI(i.URI) {
2024-10-16 17:07:57 +02:00
return fmt.Errorf("uri element %v of id not correctly formatted", i.URI)
}
2024-10-13 17:19:40 +02:00
}
return nil
}