Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
feat: output result.json (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman authored Nov 30, 2020
1 parent b3d1474 commit cb2f425
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions internal/dal/methods.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions internal/dal/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}

0 comments on commit cb2f425

Please sign in to comment.