forked from hpsaturn/wego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
40 lines (32 loc) · 806 Bytes
/
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
package backends
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/schachmat/wego/iface"
)
type jsnConfig struct {
}
func (c *jsnConfig) Setup() {
}
// Fetch will try to open the file specified in the location string argument and
// read it as json content to fill the data. The numdays argument will only work
// to further limit the amount of days in the output. It obviously cannot
// produce more data than is available in the file.
func (c *jsnConfig) Fetch(loc string, numdays int) (ret iface.Data) {
b, err := ioutil.ReadFile(loc)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(b, &ret)
if err != nil {
log.Fatal(err)
}
if len(ret.Forecast) > numdays {
ret.Forecast = ret.Forecast[:numdays]
}
return
}
func init() {
iface.AllBackends["json"] = &jsnConfig{}
}