From b96c85d13e43d150f420682a8b4700f63b79c73b Mon Sep 17 00:00:00 2001 From: Markus Teich Date: Fri, 1 Apr 2016 02:56:52 +0200 Subject: [PATCH] add json backend --- backends/json.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 backends/json.go diff --git a/backends/json.go b/backends/json.go new file mode 100644 index 0000000..9460d48 --- /dev/null +++ b/backends/json.go @@ -0,0 +1,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{} +}