-
Notifications
You must be signed in to change notification settings - Fork 0
/
doannotation.go
113 lines (96 loc) · 4.52 KB
/
doannotation.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gowords
import (
"fmt"
"strconv"
"strings"
"github.com/saleh-rahimzadeh/go-words/core"
"github.com/saleh-rahimzadeh/go-words/internal"
)
//──────────────────────────────────────────────────────────────────────────────────────────────────
// DoAnnotation utilize Words interface to provide words table and text resource and format value according to an annotation or a format specifier
type DoAnnotation struct {
Words
}
//┌ Public Methods
//└─────────────────────────────────────────────────────────────────────────────────────────────────
// GetNamed search for a name then return value if found, else return empty string.
// Format value with named annotations.
func (w DoAnnotation) GetNamed(name string, arguments map[string]any) string {
value, _ := w.FindNamed(name, arguments)
return value
}
// FindNamed search for a name then return value and `true` if found, else return empty string and `false`.
// Format value with named annotations.
func (w DoAnnotation) FindNamed(name string, arguments map[string]any) (string, bool) {
value, found := w.Words.Find(name)
if !found {
return internal.Empty, false
}
if value == internal.Empty {
return internal.Empty, true
}
return w.replacer(value, arguments), true
}
// GetIndexed search for a name then return value if found, else return empty string.
// Format value with indexed annotations.
func (w DoAnnotation) GetIndexed(name string, arguments ...any) string {
argumentMap := w.convertIndexesToMap(arguments)
value, _ := w.FindNamed(name, argumentMap)
return value
}
// FindIndexed search for a name then return value and `true` if found, else return empty string and `false`.
// Format value with indexed annotations.
func (w DoAnnotation) FindIndexed(name string, arguments ...any) (string, bool) {
argumentMap := w.convertIndexesToMap(arguments)
return w.FindNamed(name, argumentMap)
}
// GetNamed search for a name then return value if found, else return empty string.
// Format value with formatted verbs according to "https://pkg.go.dev/fmt#hdr-Printing".
func (w DoAnnotation) GetFormatted(name string, arguments ...any) string {
value, _ := w.FindFormatted(name, arguments...)
return value
}
// FindFormatted search for a name then return value and `true` if found, else return empty string and `false`.
// Format value with formatted verbs according to "https://pkg.go.dev/fmt#hdr-Printing".
func (w DoAnnotation) FindFormatted(name string, arguments ...any) (string, bool) {
value, found := w.Words.Find(name)
if !found {
return internal.Empty, false
}
if value == internal.Empty {
return internal.Empty, true
}
return fmt.Sprintf(value, arguments...), true
}
//┌ Private Methods
//└─────────────────────────────────────────────────────────────────────────────────────────────────
// replacer finds annotation tokens in input and replace with genuine value
func (w DoAnnotation) replacer(input string, arguments map[string]any) string {
if arguments == nil {
return input
}
return internal.RegexAnnotation.ReplaceAllStringFunc(input, func(token string) string {
if value, ok := arguments[strings.Trim(token, internal.AnnotationDelimiters)]; ok {
return fmt.Sprint(value)
}
return token
})
}
// convertIndexesToMap converts indexed annotations to map
func (w DoAnnotation) convertIndexesToMap(arguments []any) map[string]any {
argumentMap := map[string]any{}
for index, value := range arguments {
argumentMap[strconv.Itoa(index+1)] = value
}
return argumentMap
}
//──────────────────────────────────────────────────────────────────────────────────────────────────
// NewDoAnnotation create a new instance of NewDoAnnotation
func NewDoAnnotation(words Words) (DoAnnotation, error) {
if words == nil {
return DoAnnotation{}, core.ErrWordsNil
}
return DoAnnotation{
Words: words,
}, nil
}