Simplify trans API

This commit is contained in:
Kristóf Tóth 2019-10-11 01:56:22 +02:00
parent 4d035075ca
commit a6f5cd6268
2 changed files with 9 additions and 15 deletions

View File

@ -9,28 +9,22 @@ import (
type Translation struct {
job TransJob
Executor func(TransJob) ([]byte, error)
}
type TransJob struct {
word string
fromLang string
toLang string
Executor func(*Translation) ([]byte, error)
}
func New(word, fromLang, toLang string) Translation {
return Translation{
job: TransJob{
word: word,
fromLang: fromLang,
toLang: toLang,
},
word: word,
fromLang: fromLang,
toLang: toLang,
Executor: executeTransShell,
}
}
func executeTransShell(tj TransJob) ([]byte, error) {
func executeTransShell(t *Translation) ([]byte, error) {
config := []string{
"-no-ansi",
"-show-original", "n",
@ -38,14 +32,14 @@ func executeTransShell(tj TransJob) ([]byte, error) {
"-show-dictionary", "y",
"-show-languages", "n",
"-show-prompt-message", "n",
fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang),
tj.word,
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
t.word,
}
return exec.Command("trans", config...).Output()
}
func (t Translation) Translate() []string {
outBytes, err := t.Executor(t.job)
outBytes, err := t.Executor(&t)
if err != nil {
panic(err)
}

View File

@ -77,7 +77,7 @@ méltányosság
for _, data := range cases {
j := trans.New(data.job[0], data.job[1], data.job[2])
j.Executor = func(tj trans.TransJob) ([]byte, error) {
j.Executor = func(t *trans.Translation) ([]byte, error) {
return data.transOutput, nil
}
r := j.Translate()