Erstes Commit
This commit is contained in:
27
internal/formatter/json.go
Normal file
27
internal/formatter/json.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"royalroadupdates/internal/royalroad"
|
||||
)
|
||||
|
||||
// jsonEntry is the JSON output shape.
|
||||
type jsonEntry struct {
|
||||
Name string `json:"name"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
|
||||
// PrintJSON writes stories as JSON to w.
|
||||
func PrintJSON(w io.Writer, stories []royalroad.Story) error {
|
||||
entries := make([]jsonEntry, 0, len(stories))
|
||||
for _, s := range stories {
|
||||
entries = append(entries, jsonEntry{
|
||||
Name: s.Name,
|
||||
Date: s.DateModified.UTC().Format("2006-01-02T15:04:05Z"),
|
||||
})
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(entries)
|
||||
}
|
||||
Reference in New Issue
Block a user