Skip to content

Commit 5a2c2e2

Browse files
Added metadata map support
2 parents 9df7cbe + d7b872b commit 5a2c2e2

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

maps/maps.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func (s StateMap) SetState(stateType string, value bool) {
1313
s[stateType] = value
1414
}
1515

16+
// ToggleState toggles the value of the state with the provided key in the StateMap.
17+
func (s StateMap) ToggleState(stateType string) {
18+
s[stateType] = !s[stateType]
19+
}
20+
1621
// IsState return the value of the state provided from StateMap
1722
func (s StateMap) IsState(stateType string) bool {
1823
if ok, v := s[stateType]; ok {
@@ -28,3 +33,31 @@ func (s StateMap) HasState(stateType string) bool {
2833

2934
return ok
3035
}
36+
37+
type Metadata map[string]string
38+
39+
func NewMetadata() Metadata {
40+
return make(map[string]string)
41+
}
42+
43+
func (m Metadata) Update(key, value string) {
44+
if m == nil {
45+
m = NewMetadata()
46+
}
47+
48+
m[key] = value
49+
}
50+
51+
func (m Metadata) Has(key string) bool {
52+
_, ok := m[key]
53+
54+
return ok
55+
}
56+
57+
func (m Metadata) Value(key string) string {
58+
if !m.Has(key) {
59+
return ""
60+
}
61+
62+
return m[key]
63+
}

maps/maps_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,140 @@ func TestStateMap_HasState(t *testing.T) {
6565
})
6666
}
6767
}
68+
69+
func TestStateMap_ToggleState(t *testing.T) {
70+
type args struct {
71+
stateType string
72+
}
73+
tests := []struct {
74+
name string
75+
args args
76+
want bool
77+
}{
78+
{
79+
name: "success - toggled the state in the state map",
80+
args: args{stateType: "key1"},
81+
want: false,
82+
},
83+
{
84+
name: "success - toggled the state in the state map",
85+
args: args{stateType: "key2"},
86+
want: true,
87+
}}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
state := NewStateMap()
91+
state.SetState(tt.args.stateType, !tt.want)
92+
93+
state.ToggleState(tt.args.stateType)
94+
95+
got := state.IsState(tt.args.stateType)
96+
if got != tt.want {
97+
t.Errorf("ToggleState() : %v, want : %v", got, tt.want)
98+
}
99+
})
100+
}
101+
}
102+
103+
func TestMetadata_Update(t *testing.T) {
104+
type args struct {
105+
key string
106+
value string
107+
}
108+
tests := []struct {
109+
name string
110+
m Metadata
111+
args args
112+
}{
113+
{
114+
name: "success - update key and value",
115+
m: map[string]string{"key1": "value2"},
116+
args: args{key: "key1", value: "value1"},
117+
},
118+
{
119+
name: "success - add a new key and value",
120+
m: map[string]string{"key1": "value2"},
121+
args: args{key: "key2", value: "value1"},
122+
},
123+
{
124+
name: "success - update key and value with nil map",
125+
m: nil,
126+
args: args{key: "key1", value: "value1"},
127+
},
128+
}
129+
for _, tt := range tests {
130+
t.Run(tt.name, func(t *testing.T) {
131+
tt.m.Update(tt.args.key, tt.args.value)
132+
})
133+
}
134+
}
135+
136+
func TestMetadata_Has(t *testing.T) {
137+
type args struct {
138+
key string
139+
}
140+
tests := []struct {
141+
name string
142+
m Metadata
143+
args args
144+
want bool
145+
}{
146+
{
147+
name: "success - key exist",
148+
m: map[string]string{"key1": "value1"},
149+
args: args{key: "key1"},
150+
want: true,
151+
},
152+
{
153+
name: "success - key does not exist",
154+
m: map[string]string{"key1": "value1"},
155+
args: args{key: "key2"},
156+
want: false,
157+
},
158+
{
159+
name: "success - check in nil map",
160+
m: nil,
161+
args: args{key: "key2"},
162+
want: false,
163+
},
164+
}
165+
for _, tt := range tests {
166+
t.Run(tt.name, func(t *testing.T) {
167+
if got := tt.m.Has(tt.args.key); got != tt.want {
168+
t.Errorf("Has() = %v, want %v", got, tt.want)
169+
}
170+
})
171+
}
172+
}
173+
174+
func TestMetadata_Value(t *testing.T) {
175+
type args struct {
176+
key string
177+
}
178+
tests := []struct {
179+
name string
180+
m Metadata
181+
args args
182+
want string
183+
}{
184+
{
185+
name: "success - get a value of the key",
186+
m: map[string]string{"key1": "value1"},
187+
args: args{key: "key1"},
188+
want: "value1",
189+
},
190+
{
191+
name: "success - get a value of the non existing key",
192+
m: map[string]string{"key1": "value1"},
193+
args: args{key: "key2"},
194+
want: "",
195+
},
196+
}
197+
for _, tt := range tests {
198+
t.Run(tt.name, func(t *testing.T) {
199+
if got := tt.m.Value(tt.args.key); got != tt.want {
200+
t.Errorf("Value() = %v, want %v", got, tt.want)
201+
}
202+
})
203+
}
204+
}

strings/strings.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Package strings defines strings helpers.
44
package strings
55

66
import (
7-
"golang.org/x/text/language"
87
"regexp"
98
"strings"
109

1110
"golang.org/x/text/cases"
11+
"golang.org/x/text/language"
1212
)
1313

14+
var c = cases.Title(language.English)
15+
1416
// SubstringSearchOptions contains options for substring search.
1517
type SubstringSearchOptions struct {
1618
CaseInsensitive bool // Perform case-insensitive search
@@ -51,10 +53,7 @@ func SubstringSearch(input, substring string, options SubstringSearchOptions) []
5153

5254
// Title return string in title case with English language-specific title
5355
func Title(input string) string {
54-
c := cases.Title(language.English)
55-
5656
return c.String(input)
57-
5857
}
5958

6059
// ToTitle converts a string to title case, capitalizing the first letter of each word.

0 commit comments

Comments
 (0)