Skip to content

Commit

Permalink
file backend: allow data types other than string
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Oct 29, 2019
1 parent c255e74 commit 4d4fac1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
9 changes: 5 additions & 4 deletions file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package file

import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -102,12 +103,12 @@ func nodeWalk(node map[interface{}]interface{}, key string, vars map[string]stri
switch j := j.(type) {
case map[interface{}]interface{}:
nodeWalk(j, key, vars)
case string:
vars[key+"/"+j] = ""
default:
vars[fmt.Sprintf("%s/%v", key, j)] = ""
}
}
case string:
vars[key] = v
default:
vars[key] = fmt.Sprintf("%v", v)
}
}
return nil
Expand Down
49 changes: 43 additions & 6 deletions file/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/HeavyHorst/easykv/testutils"

"gopkg.in/check.v1"
. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -62,7 +63,24 @@ const testfileJSON string = `
}
`

func testGetVal(file, data string, t *C) {
const filepathJSON2 string = "/tmp/easyKV_filetest2.json"
const testfileJSON2 string = `
{
"remtest": [
1,
true,
null
],
"premtest": {
"database": {
"url": 100,
"user": false
}
}
}
`

func testGetVal(file, data string, expected map[string]string, t *C) {
// write testfile
err := ioutil.WriteFile(file, []byte(data), 0666)
if err != nil {
Expand All @@ -71,18 +89,37 @@ func testGetVal(file, data string, t *C) {
defer os.Remove(file)

c, _ := New(file)
err = testutils.GetValues(t, c)
if err != nil {
t.Error(err)

if expected == nil {
err = testutils.GetValues(t, c)
if err != nil {
t.Error(err)
}
} else {
m, err := c.GetValues([]string{"/remtest", "/premtest"})
if err != nil {
t.Error(err)
}
t.Check(m, check.DeepEquals, expected)
}
}

func (s *FilterSuite) TestGetValuesYML(t *C) {
testGetVal(filepathYML, testfileYML, t)
testGetVal(filepathYML, testfileYML, nil, t)
}

func (s *FilterSuite) TestGetValuesJSON(t *C) {
testGetVal(filepathJSON, testfileJSON, t)
testGetVal(filepathJSON, testfileJSON, nil, t)
}

func (s *FilterSuite) TestGetValuesJSON2(t *C) {
testGetVal(filepathJSON2, testfileJSON2, map[string]string{
"/remtest/1": "",
"/remtest/true": "",
"/remtest/<nil>": "",
"/premtest/database/url": "100",
"/premtest/database/user": "false",
}, t)
}

func (s *FilterSuite) TestWatchPrefix(t *C) {
Expand Down

0 comments on commit 4d4fac1

Please sign in to comment.