-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbuilder.go
101 lines (76 loc) · 2.29 KB
/
textbuilder.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
package telejoon
import "fmt"
type TextBuilder interface {
String(update *StateUpdate) string
}
type DeferredTextBuilder func(update *StateUpdate) string
func (t DeferredTextBuilder) String(update *StateUpdate) string {
return t(update)
}
type StaticTextBuilder string
func (t StaticTextBuilder) String(_ *StateUpdate) string {
return string(t)
}
type LanguageKeyTextBuilder string
func (t LanguageKeyTextBuilder) String(update *StateUpdate) string {
return update.Language().MustGet(string(t))
}
type LanguageKeyWithParamsTextBuilder struct {
key string
params map[string]interface{}
}
func (t LanguageKeyWithParamsTextBuilder) String(update *StateUpdate) string {
return update.Language().MustGetWithParams(t.key, t.params)
}
type UpdateKeyTextBuilder string
func (t UpdateKeyTextBuilder) String(update *StateUpdate) string {
data := update.Get(string(t))
if data == nil {
return ""
}
if val, ok := data.(string); ok {
return val
}
return ""
}
type TextBuilderF struct {
builders []TextBuilder
placeholder string
}
func (t TextBuilderF) String(update *StateUpdate) string {
var str []any
for _, builder := range t.builders {
str = append(str, builder.String(update))
}
return fmt.Sprintf(t.placeholder, str...)
}
// NewTextBuilderF returns a new TextBuilderF
func NewTextBuilderF(placeholder string, builders ...TextBuilder) TextBuilderF {
return TextBuilderF{
builders: builders,
placeholder: placeholder,
}
}
// NewUpdateKeyText returns a new UpdateKeyTextBuilder
func NewUpdateKeyText(key string) UpdateKeyTextBuilder {
return UpdateKeyTextBuilder(key)
}
// NewLanguageKeyWithParamsText returns a new LanguageKeyWithParamsTextBuilder
func NewLanguageKeyWithParamsText(key string, params map[string]interface{}) LanguageKeyWithParamsTextBuilder {
return LanguageKeyWithParamsTextBuilder{
key: key,
params: params,
}
}
// NewLanguageKeyText returns a new LanguageKeyTextBuilder
func NewLanguageKeyText(key string) LanguageKeyTextBuilder {
return LanguageKeyTextBuilder(key)
}
// NewStaticText returns a new StaticTextBuilder
func NewStaticText(text string) StaticTextBuilder {
return StaticTextBuilder(text)
}
// NewDeferredText returns a new DeferredTextBuilder
func NewDeferredText(text func(update *StateUpdate) string) DeferredTextBuilder {
return text
}