Just implement update logic in Methods where it is needed, since if there is no pointer I cannot hand it to any function or method

This commit is contained in:
Jason Streifling 2024-10-20 15:54:49 +02:00
parent 0b70a8ee10
commit 8fc6a10b0d
3 changed files with 138 additions and 32 deletions

View File

@ -24,15 +24,6 @@ func NewDate(t time.Time) *Date {
} }
} }
// Update updates the Date.
func (d *Date) Update() {
if d == nil {
d = NewDate(time.Now())
} else {
d.DateTime = DateTime(time.Now())
}
}
// Check checks the Date for incompatibilities with RFC4287. It returns an // Check checks the Date for incompatibilities with RFC4287. It returns an
// error. // error.
func (d *Date) Check() error { func (d *Date) Check() error {

View File

@ -69,7 +69,12 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Authors, p) return addToSlice(&e.Authors, p)
} }
@ -79,13 +84,23 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Categories, c) return addToSlice(&e.Categories, c)
} }
@ -96,14 +111,24 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Contributors, c) return addToSlice(&e.Contributors, c)
} }
@ -114,13 +139,23 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Links, l) return addToSlice(&e.Links, l)
} }
@ -130,14 +165,24 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Extensions, x) return addToSlice(&e.Extensions, x)
} }
@ -148,7 +193,12 @@ 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.Update() if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return nil return nil
} }

91
feed.go
View File

@ -38,7 +38,12 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Authors, p) return addToSlice(&f.Authors, p)
} }
@ -48,13 +53,23 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Categories, c) return addToSlice(&f.Categories, c)
} }
@ -65,14 +80,24 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Contributors, c) return addToSlice(&f.Contributors, c)
} }
@ -83,14 +108,24 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Links, l) return addToSlice(&f.Links, l)
} }
@ -100,13 +135,23 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Extensions, e) return addToSlice(&f.Extensions, e)
} }
@ -117,13 +162,23 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Entries, e) return addToSlice(&f.Entries, e)
} }
@ -133,7 +188,12 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return nil return nil
} }
@ -155,7 +215,12 @@ 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.Update() if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return nil return nil
} }