-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
57 lines (49 loc) · 1.46 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package repository implements Repository interface
package repository
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/tenkoh/go-sitecheck"
)
var _ sitecheck.Repository = (*JSONRepository)(nil)
// JSONRepository implements Repository interface.
// This read and write data in JSON format.
// Save method must be called to save the data.
type JSONRepository struct {
updates sitecheck.SiteUpdates
}
// NewJSONRepository returns a new JSONRepository.
func NewJSONRepository(r io.Reader) (*JSONRepository, error) {
u := sitecheck.SiteUpdates{}
if err := json.NewDecoder(r).Decode(&u); err != nil {
return nil, fmt.Errorf("error decoding json: %w", err)
}
return &JSONRepository{updates: u}, nil
}
// Query returns the site updates for the given urls.
// If a url is not found in the repository, it contains no updates.
func (r *JSONRepository) Query(_ context.Context, urls ...string) (sitecheck.SiteUpdates, error) {
u := sitecheck.SiteUpdates{}
for _, url := range urls {
if us, ok := r.updates[url]; ok {
u[url] = us
} else {
u[url] = []time.Time{}
}
}
return u, nil
}
// Upcert updates the site updates with the given updates.
func (r *JSONRepository) Upcert(_ context.Context, updates sitecheck.SiteUpdates) error {
for url, ud := range updates {
r.updates[url] = ud
}
return nil
}
// Save saves the site updates to the given writer.
func (r *JSONRepository) Save(w io.Writer) error {
return json.NewEncoder(w).Encode(r.updates)
}