-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
74 lines (60 loc) · 2.69 KB
/
repository.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
62
63
64
65
66
67
68
69
70
71
72
73
74
package gowords
import (
"fmt"
"github.com/saleh-rahimzadeh/go-words/core"
"github.com/saleh-rahimzadeh/go-words/internal"
)
//──────────────────────────────────────────────────────────────────────────────────────────────────
// WordsRepository provide words table and text resource with accepting string source and storing in array
type WordsRepository struct {
repository []string
separator rune
}
//──────────────────────────────────────────────────────────────────────────────────────────────────
// Get search for a name then return value if found, else return empty string
func (w WordsRepository) Get(name string) string {
value, _ := w.Find(name)
return value
}
// Find search for a name then return value and `true` if found, else return empty string and `false`
func (w WordsRepository) Find(name string) (string, bool) {
name, ok := internal.ValidationName(name)
if !ok {
return internal.Empty, false
}
var separator = string(w.separator)
for _, line := range w.repository {
if value, found := internal.Extract(line, name, separator); found {
return value, true
}
}
return internal.Empty, false
}
//──────────────────────────────────────────────────────────────────────────────────────────────────
// NewWordsRepository create a new instance of WordsRepository
func NewWordsRepository(source string, separator rune, comment rune) (WordsRepository, error) {
var (
separatorCharacter string = string(separator)
commentCharacter string = string(comment)
err error
)
err = internal.ValidationSource(source)
if err != nil {
return WordsRepository{}, err
}
err = internal.ValidationDelimiters(separatorCharacter, commentCharacter)
if err != nil {
return WordsRepository{}, err
}
repository, err := internal.Normalization(source, separatorCharacter, commentCharacter)
if err != nil {
return WordsRepository{}, err
}
if duplicated, name := internal.CheckDuplication(repository, separatorCharacter); duplicated {
return WordsRepository{}, fmt.Errorf("%w, name '%s'", core.ErrNameDuplicated, name)
}
return WordsRepository{
repository: repository,
separator: separator,
}, nil
}