/
WordService.go
61 lines (52 loc) · 1.24 KB
/
WordService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// WordService.go
package main
import (
"fmt"
"github.com/ThatRubenAguilar/words"
"net"
)
type WordService struct {
listener net.Listener
word_dictionary *words.WordDictionary
}
type LookupResponse struct {
Info words.WordInfo
Error error
}
type AddWordsResponse struct {
Error error
}
func (ws *WordService) Setup() (err error) {
if ws.word_dictionary == nil {
filename := "words.txt"
word_dict := new(words.WordDictionary)
err = word_dict.AddWordsFromFile(filename)
if err != nil {
fmt.Println(err)
return
}
ws.word_dictionary = word_dict
} else {
copy_dict := ws.word_dictionary.Copy()
ws.word_dictionary = copy_dict
}
return
}
func (ws *WordService) Lookup(Word string) LookupResponse {
response := LookupResponse{}
var lookup_dict *words.WordDictionary
lookup_dict = ws.word_dictionary
if response.Info, response.Error = lookup_dict.Lookup(Word); response.Error != nil {
fmt.Println(response.Error)
}
return response
}
func (ws *WordService) AddWords(Words []string) AddWordsResponse {
response := AddWordsResponse{}
copy_dict := ws.word_dictionary.Copy()
if response.Error = copy_dict.AddWords(Words); response.Error != nil {
return response
}
ws.word_dictionary = copy_dict
return response
}