Fixed bug with Reader exhaustion
This commit is contained in:
parent
d8a295be75
commit
425f0fd56d
6 changed files with 18 additions and 42 deletions
|
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -20,6 +22,9 @@ func main() {
|
||||||
|
|
||||||
groupId, _ := strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
|
groupId, _ := strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
|
||||||
dict, _ := os.Open("./assets/additional_cards.json")
|
dict, _ := os.Open("./assets/additional_cards.json")
|
||||||
|
dictBytes, _ := ioutil.ReadAll(dict)
|
||||||
|
var dictMap map[string]string
|
||||||
|
_ = json.Unmarshal(dictBytes, &dictMap)
|
||||||
handler := vk.Handler{
|
handler := vk.Handler{
|
||||||
Sender: &vk.ApiSender{
|
Sender: &vk.ApiSender{
|
||||||
Token: os.Getenv("VK_TOKEN"),
|
Token: os.Getenv("VK_TOKEN"),
|
||||||
|
|
@ -30,7 +35,7 @@ func main() {
|
||||||
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
||||||
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
|
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
|
||||||
InfoFetcher: &cardsinfo.Fetcher{
|
InfoFetcher: &cardsinfo.Fetcher{
|
||||||
Dict: dict,
|
Dict: dictMap,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package cardsinfo
|
package cardsinfo
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
type Fetcher struct {
|
type Fetcher struct {
|
||||||
Dict io.Reader
|
Dict map[string]string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ func (f *Fetcher) GetOriginalName(name string) string {
|
||||||
path := scryfallUrl + "/cards/named?fuzzy=" + applyFilters(name)
|
path := scryfallUrl + "/cards/named?fuzzy=" + applyFilters(name)
|
||||||
result := getCardByUrl(path)
|
result := getCardByUrl(path)
|
||||||
if result == "" && f.Dict != nil {
|
if result == "" && f.Dict != nil {
|
||||||
result, _ = dicttranslate.FindFromReader(name, f.Dict, 5)
|
result, _ = dicttranslate.Find(name, f.Dict, 5)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
package cardsinfo
|
package cardsinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gopkg.in/h2non/gock.v1"
|
"gopkg.in/h2non/gock.v1"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -31,19 +29,19 @@ func TestGetOriginalName_Scryfall(t *testing.T) {
|
||||||
assert.Equal(t, "Result Card", name)
|
assert.Equal(t, "Result Card", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOriginalName_Dict(t *testing.T) {
|
func TestGetOriginalName_DictTwice(t *testing.T) {
|
||||||
defer gock.Off()
|
defer gock.Off()
|
||||||
|
|
||||||
gock.New(scryfallUrl + "/cards/named?fuzzy=card").Reply(http.StatusOK).JSON(card{})
|
gock.New(scryfallUrl + "/cards/named?fuzzy=card").Persist().Reply(http.StatusOK).JSON(card{})
|
||||||
serialized, _ := json.Marshal(map[string]string{
|
|
||||||
"card": "Card",
|
|
||||||
})
|
|
||||||
dict := strings.NewReader(string(serialized))
|
|
||||||
f := &Fetcher{
|
f := &Fetcher{
|
||||||
Dict: dict,
|
Dict: map[string]string{
|
||||||
|
"card": "Card",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
name := f.GetOriginalName("card")
|
name := f.GetOriginalName("card")
|
||||||
assert.Equal(t, "Card", name)
|
assert.Equal(t, "Card", name)
|
||||||
|
name = f.GetOriginalName("card")
|
||||||
|
assert.Equal(t, "Card", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOriginalName_BadJson(t *testing.T) {
|
func TestGetOriginalName_BadJson(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,6 @@
|
||||||
package dicttranslate
|
package dicttranslate
|
||||||
|
|
||||||
import (
|
func Find(query string, dict map[string]string, maxDist int) (string, bool) {
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
func find(query string, dict map[string]string, maxDist int) (string, bool) {
|
|
||||||
var keys []string
|
var keys []string
|
||||||
for i := range dict {
|
for i := range dict {
|
||||||
keys = append(keys, i)
|
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)
|
key, f := match(query, keys, maxDist)
|
||||||
return dict[key], f
|
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 (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindEmpty(t *testing.T) {
|
func TestFindEmpty(t *testing.T) {
|
||||||
dict := map[string]string{}
|
dict := map[string]string{}
|
||||||
_, f := find("", dict, 0)
|
_, f := Find("", dict, 0)
|
||||||
assert.False(t, f)
|
assert.False(t, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -16,18 +15,7 @@ func TestFindEntry(t *testing.T) {
|
||||||
dict := map[string]string{
|
dict := map[string]string{
|
||||||
"entry": "value",
|
"entry": "value",
|
||||||
}
|
}
|
||||||
val, f := find("entry", dict, 0)
|
val, f := Find("entry", dict, 0)
|
||||||
assert.True(t, f)
|
assert.True(t, f)
|
||||||
assert.Equal(t, "value", val)
|
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