Fixed bug with Reader exhaustion
This commit is contained in:
parent
d8a295be75
commit
425f0fd56d
6 changed files with 18 additions and 42 deletions
|
|
@ -1,12 +1,6 @@
|
|||
package dicttranslate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func find(query string, dict map[string]string, maxDist int) (string, bool) {
|
||||
func Find(query string, dict map[string]string, maxDist int) (string, bool) {
|
||||
var keys []string
|
||||
for i := range dict {
|
||||
keys = append(keys, i)
|
||||
|
|
@ -14,10 +8,3 @@ func find(query string, dict map[string]string, maxDist int) (string, bool) {
|
|||
key, f := match(query, keys, maxDist)
|
||||
return dict[key], f
|
||||
}
|
||||
|
||||
func FindFromReader(query string, reader io.Reader, maxDist int) (string, bool) {
|
||||
content, _ := ioutil.ReadAll(reader)
|
||||
dict := map[string]string{}
|
||||
_ = json.Unmarshal(content, &dict)
|
||||
return find(query, dict, maxDist)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,12 @@ package dicttranslate
|
|||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindEmpty(t *testing.T) {
|
||||
dict := map[string]string{}
|
||||
_, f := find("", dict, 0)
|
||||
_, f := Find("", dict, 0)
|
||||
assert.False(t, f)
|
||||
}
|
||||
|
||||
|
|
@ -16,18 +15,7 @@ func TestFindEntry(t *testing.T) {
|
|||
dict := map[string]string{
|
||||
"entry": "value",
|
||||
}
|
||||
val, f := find("entry", dict, 0)
|
||||
val, f := Find("entry", dict, 0)
|
||||
assert.True(t, f)
|
||||
assert.Equal(t, "value", val)
|
||||
}
|
||||
|
||||
func TestFindFromReaderFail(t *testing.T) {
|
||||
_, f := FindFromReader("entry", strings.NewReader("{}"), 0)
|
||||
assert.False(t, f)
|
||||
}
|
||||
|
||||
func TestFindFromReaderSuccess(t *testing.T) {
|
||||
value, f := FindFromReader("entry", strings.NewReader("{\"entry\":\"value\"}"), 0)
|
||||
assert.True(t, f)
|
||||
assert.Equal(t, "value", value)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue