-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc096ae
commit 01d636a
Showing
4 changed files
with
113 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
module github.com/upvestco/documentation_assets | ||
module github.com/upvestco/documentation_assets | ||
|
||
go 1.22.0 | ||
|
||
toolchain go1.22.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/xml" | ||
"errors" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type RSS struct { | ||
XMLName xml.Name `xml:"rss"` | ||
Channel Channel `xml:"channel"` | ||
} | ||
|
||
type Channel struct { | ||
Title string `xml:"title"` | ||
Description string `xml:"description"` | ||
PubDate string `xml:"pubDate"` | ||
Items []Item `xml:"item"` | ||
} | ||
|
||
type Item struct { | ||
Title string `xml:"title"` | ||
Description string `xml:"description"` | ||
PubDate string `xml:"pubDate"` | ||
GUID string `xml:"guid"` | ||
} | ||
|
||
func (r *RSS) Validate() error { | ||
rules := []func() error{ | ||
r.validatePubDate, | ||
r.validateItemGUIDs, | ||
r.validateItemDates, | ||
r.validatePubDateUpdated, | ||
} | ||
var errs []error | ||
for _, rule := range rules { | ||
if err := rule(); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
return errors.Join(errs...) | ||
} | ||
|
||
func (r *RSS) validatePubDate() error { | ||
err := validateRSSDate(r.Channel.PubDate) | ||
if err != nil { | ||
return fmt.Errorf("channel pub date: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (r *RSS) validateItemDates() error { | ||
for _, item := range r.Channel.Items { | ||
err := validateRSSDate(item.PubDate) | ||
if err != nil { | ||
return fmt.Errorf("item '%s' pub date: %v", item.Title, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (r *RSS) validateItemGUIDs() error { | ||
guids := make(map[string]bool) | ||
for _, item := range r.Channel.Items { | ||
if guids[item.GUID] { | ||
return fmt.Errorf("duplicate GUID found: %s", item.GUID) | ||
} | ||
guids[item.GUID] = true | ||
} | ||
return nil | ||
} | ||
|
||
func (r *RSS) validatePubDateUpdated() error { | ||
if len(r.Channel.Items) == 0 { | ||
return nil | ||
} | ||
|
||
chanDate, err := time.Parse(time.RFC1123Z, r.Channel.PubDate) | ||
if err != nil { | ||
return fmt.Errorf("invalid date format in channel '%s'", r.Channel.PubDate) | ||
} | ||
|
||
latestItem := r.Channel.Items[0] | ||
itemDate, err := time.Parse(time.RFC1123Z, latestItem.PubDate) | ||
if err != nil { | ||
return fmt.Errorf("invalid date format in item '%s'", latestItem.PubDate) | ||
} | ||
|
||
if !chanDate.Equal(itemDate) { | ||
return fmt.Errorf("publication dates of channel and item do not match") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateRSSDate(str string) error { | ||
t, err := time.Parse(time.RFC1123Z, str) | ||
if err != nil { | ||
return fmt.Errorf("invalid date format in %s", str) | ||
} | ||
// Check that day of week was set correctly, as it is ignored by time.Parse. | ||
if str != t.Format(time.RFC1123Z) { | ||
return fmt.Errorf("day of week is not correct: expected %s, got %s", t.Format(time.RFC1123Z), str) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters