Implement more sophisticated updateDateTime function
This commit is contained in:
parent
ee72e91593
commit
da5d955b2d
10
atom.go
10
atom.go
@ -7,6 +7,7 @@ import (
|
|||||||
"mime"
|
"mime"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
@ -109,6 +110,15 @@ func isValidAttribute(attribute string) bool {
|
|||||||
return regex.MatchString(attribute)
|
return regex.MatchString(attribute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update updates the Date
|
||||||
|
func updateDateTime(d *Date) {
|
||||||
|
if d == nil {
|
||||||
|
d = NewDate(time.Now())
|
||||||
|
} else {
|
||||||
|
d.DateTime = DateTime(time.Now())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewURN generates an new valid IRI based on a UUID. It returns an IRI.
|
// NewURN generates an new valid IRI based on a UUID. It returns an IRI.
|
||||||
func NewURN() string {
|
func NewURN() string {
|
||||||
return fmt.Sprint("urn:uuid:", uuid.New())
|
return fmt.Sprint("urn:uuid:", uuid.New())
|
||||||
|
20
entry.go
20
entry.go
@ -69,7 +69,7 @@ func NewEntry(title string) *Entry {
|
|||||||
// AddAuthor adds the Person as an author to the Entry. It returns its index as
|
// AddAuthor adds the Person as an author to the Entry. It returns its index as
|
||||||
// an int.
|
// an int.
|
||||||
func (e *Entry) AddAuthor(p *Person) int {
|
func (e *Entry) AddAuthor(p *Person) int {
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return addToSlice(&e.Authors, p)
|
return addToSlice(&e.Authors, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,13 +79,13 @@ func (e *Entry) DeleteAuthor(index int) error {
|
|||||||
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
|
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCategory adds the Category to the Entry. It returns ts index as an int.
|
// AddCategory adds the Category to the Entry. It returns ts index as an int.
|
||||||
func (e *Entry) AddCategory(c *Category) int {
|
func (e *Entry) AddCategory(c *Category) int {
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return addToSlice(&e.Categories, c)
|
return addToSlice(&e.Categories, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,14 +96,14 @@ func (e *Entry) DeleteCategory(index int) error {
|
|||||||
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err)
|
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddContributor adds the Person as a contributor to the Entry. It returns its
|
// AddContributor adds the Person as a contributor to the Entry. It returns its
|
||||||
// index as an int.
|
// index as an int.
|
||||||
func (e *Entry) AddContributor(c *Person) int {
|
func (e *Entry) AddContributor(c *Person) int {
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return addToSlice(&e.Contributors, c)
|
return addToSlice(&e.Contributors, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,13 +114,13 @@ func (e *Entry) DeleteContributor(index int) error {
|
|||||||
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err)
|
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLink adds the Link to the Entry. It returns its index as an int.
|
// AddLink adds the Link to the Entry. It returns its index as an int.
|
||||||
func (e *Entry) AddLink(l *Link) int {
|
func (e *Entry) AddLink(l *Link) int {
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return addToSlice(&e.Links, l)
|
return addToSlice(&e.Links, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,14 +130,14 @@ func (e *Entry) DeleteLink(index int) error {
|
|||||||
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
|
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddExtension adds the ExtensionElement to the Entry. It returns its index as
|
// AddExtension adds the ExtensionElement to the Entry. It returns its index as
|
||||||
// an int.
|
// an int.
|
||||||
func (e *Entry) AddExtension(x *ExtensionElement) int {
|
func (e *Entry) AddExtension(x *ExtensionElement) int {
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return addToSlice(&e.Extensions, x)
|
return addToSlice(&e.Extensions, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ func (e *Entry) DeleteExtension(index int) error {
|
|||||||
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err)
|
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Updated = NewDate(time.Now())
|
updateDateTime(e.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
feed.go
26
feed.go
@ -38,7 +38,7 @@ func NewFeed(title string) *Feed {
|
|||||||
// AddAuthor adds the Person as an author to the Feed. It returns its index as
|
// AddAuthor adds the Person as an author to the Feed. It returns its index as
|
||||||
// an int.
|
// an int.
|
||||||
func (f *Feed) AddAuthor(p *Person) int {
|
func (f *Feed) AddAuthor(p *Person) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Authors, p)
|
return addToSlice(&f.Authors, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,13 +48,13 @@ func (f *Feed) DeleteAuthor(index int) error {
|
|||||||
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCategory adds the Category to the Feed. It returns its index as an int.
|
// AddCategory adds the Category to the Feed. It returns its index as an int.
|
||||||
func (f *Feed) AddCategory(c *Category) int {
|
func (f *Feed) AddCategory(c *Category) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Categories, c)
|
return addToSlice(&f.Categories, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,14 +65,14 @@ func (f *Feed) DeleteCategory(index int) error {
|
|||||||
return fmt.Errorf("error deleting category %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting category %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddContributor adds the Person as a contributor to the Feed. It returns its
|
// AddContributor adds the Person as a contributor to the Feed. It returns its
|
||||||
// index as an int.
|
// index as an int.
|
||||||
func (f *Feed) AddContributor(c *Person) int {
|
func (f *Feed) AddContributor(c *Person) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Contributors, c)
|
return addToSlice(&f.Contributors, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,14 +83,14 @@ func (f *Feed) DeleteContributor(index int) error {
|
|||||||
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
|
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
|
||||||
// It returns its index as an int.
|
// It returns its index as an int.
|
||||||
func (f *Feed) AddLink(l *Link) int {
|
func (f *Feed) AddLink(l *Link) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Links, l)
|
return addToSlice(&f.Links, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,13 +100,13 @@ func (f *Feed) DeleteLink(index int) error {
|
|||||||
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddExtension adds the Extension to the Feed. It returns its index as an int.
|
// AddExtension adds the Extension to the Feed. It returns its index as an int.
|
||||||
func (f *Feed) AddExtension(e *ExtensionElement) int {
|
func (f *Feed) AddExtension(e *ExtensionElement) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Extensions, e)
|
return addToSlice(&f.Extensions, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,13 +117,13 @@ func (f *Feed) DeleteExtension(index int) error {
|
|||||||
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddEntry adds the Entry to the Feed. It returns its index as an int.
|
// AddEntry adds the Entry to the Feed. It returns its index as an int.
|
||||||
func (f *Feed) AddEntry(e *Entry) int {
|
func (f *Feed) AddEntry(e *Entry) int {
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return addToSlice(&f.Entries, e)
|
return addToSlice(&f.Entries, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +133,7 @@ func (f *Feed) DeleteEntry(index int) error {
|
|||||||
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
|
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ func (f *Feed) DeleteEntryByURI(uri string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
f.Entries = append(f.Entries[:index], f.Entries[index+1:]...)
|
f.Entries = append(f.Entries[:index], f.Entries[index+1:]...)
|
||||||
f.Updated = NewDate(time.Now())
|
updateDateTime(f.Updated)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user