From cb2f4251fb00eaeb0b54aeeffb6ae121d602bf68 Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Mon, 30 Nov 2020 07:23:55 +0200 Subject: [PATCH] feat: output result.json (#95) --- internal/dal/methods.go | 17 ++++++++++++++--- internal/dal/methods_test.go | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/internal/dal/methods.go b/internal/dal/methods.go index c990c5c..d5f54ba 100644 --- a/internal/dal/methods.go +++ b/internal/dal/methods.go @@ -1,12 +1,12 @@ package dal import ( + "encoding/json" "errors" "io" "io/ioutil" "os" "path/filepath" - "strconv" "time" "github.com/Djarvur/allcups-itrally-2020-task/internal/app" @@ -16,7 +16,7 @@ const ( fnStartTime = "start.time" fnTreasureKey = "treasure.key" fnGame = "game.data" - fnResult = "result.txt" + fnResult = "result.json" ) func (r *Repo) LoadStartTime() (*time.Time, error) { @@ -67,5 +67,16 @@ func (r *Repo) SaveResult(result int) error { if !os.IsNotExist(err) { return os.ErrExist } - return save(r.cfg.ResultDir, fnResult, []byte(strconv.Itoa(result))) + data := struct { + Status string `json:"status"` + Score int `json:"score"` + }{ + Status: "OK", + Score: result, + } + buf, err := json.Marshal(data) + if err != nil { + return err + } + return save(r.cfg.ResultDir, fnResult, buf) } diff --git a/internal/dal/methods_test.go b/internal/dal/methods_test.go index c9c1039..b1d556c 100644 --- a/internal/dal/methods_test.go +++ b/internal/dal/methods_test.go @@ -120,7 +120,7 @@ func TestSaveResult(tt *testing.T) { t.Nil(r.SaveResult(42)) t.Err(r.SaveResult(7), os.ErrExist) - buf, err := ioutil.ReadFile(cfg.ResultDir + "/result.txt") + buf, err := ioutil.ReadFile(cfg.ResultDir + "/result.json") t.Nil(err) - t.Equal(string(buf), "42") + t.Equal(string(buf), `{"status":"OK","score":42}`) }