Skip to content

Commit 0ebe65d

Browse files
authored
♻️ refact(state): Use GetState to reduce duplicate code (#3542)
♻️ refact: use GetState to reduce duplicate code
1 parent a258661 commit 0ebe65d

1 file changed

Lines changed: 30 additions & 132 deletions

File tree

state.go

Lines changed: 30 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -135,205 +135,103 @@ func GetStateWithDefault[T any](s *State, key string, defaultVal T) T {
135135
// GetString retrieves a string value from the State.
136136
// It returns the string and a boolean indicating successful type assertion.
137137
func (s *State) GetString(key string) (string, bool) {
138-
dep, ok := s.Get(key)
139-
if ok {
140-
depString, okCast := dep.(string)
141-
return depString, okCast
142-
}
143-
144-
return "", false
138+
return GetState[string](s, key)
145139
}
146140

147141
// GetInt retrieves an integer value from the State.
148142
// It returns the int and a boolean indicating successful type assertion.
149143
func (s *State) GetInt(key string) (int, bool) {
150-
dep, ok := s.Get(key)
151-
if ok {
152-
depInt, okCast := dep.(int)
153-
return depInt, okCast
154-
}
155-
156-
return 0, false
144+
return GetState[int](s, key)
157145
}
158146

159147
// GetBool retrieves a boolean value from the State.
160148
// It returns the bool and a boolean indicating successful type assertion.
161149
func (s *State) GetBool(key string) (value, ok bool) { //nolint:nonamedreturns // Better idea to use named returns here
162-
dep, ok := s.Get(key)
163-
if ok {
164-
depBool, okCast := dep.(bool)
165-
return depBool, okCast
166-
}
167-
168-
return false, false
150+
return GetState[bool](s, key)
169151
}
170152

171153
// GetFloat64 retrieves a float64 value from the State.
172154
// It returns the float64 and a boolean indicating successful type assertion.
173155
func (s *State) GetFloat64(key string) (float64, bool) {
174-
dep, ok := s.Get(key)
175-
if ok {
176-
depFloat64, okCast := dep.(float64)
177-
return depFloat64, okCast
178-
}
179-
180-
return 0, false
156+
return GetState[float64](s, key)
181157
}
182158

183159
// GetUint retrieves a uint value from the State.
184-
// It returns the float64 and a boolean indicating successful type assertion.
160+
// It returns the uint and a boolean indicating successful type assertion.
185161
func (s *State) GetUint(key string) (uint, bool) {
186-
dep, ok := s.Get(key)
187-
if ok {
188-
if depUint, okCast := dep.(uint); okCast {
189-
return depUint, true
190-
}
191-
}
192-
return 0, false
162+
return GetState[uint](s, key)
193163
}
194164

195165
// GetInt8 retrieves an int8 value from the State.
196-
// It returns the float64 and a boolean indicating successful type assertion.
166+
// It returns the int8 and a boolean indicating successful type assertion.
197167
func (s *State) GetInt8(key string) (int8, bool) {
198-
dep, ok := s.Get(key)
199-
if ok {
200-
if depInt8, okCast := dep.(int8); okCast {
201-
return depInt8, true
202-
}
203-
}
204-
return 0, false
168+
return GetState[int8](s, key)
205169
}
206170

207171
// GetInt16 retrieves an int16 value from the State.
208-
// It returns the float64 and a boolean indicating successful type assertion.
172+
// It returns the int16 and a boolean indicating successful type assertion.
209173
func (s *State) GetInt16(key string) (int16, bool) {
210-
dep, ok := s.Get(key)
211-
if ok {
212-
if depInt16, okCast := dep.(int16); okCast {
213-
return depInt16, true
214-
}
215-
}
216-
return 0, false
174+
return GetState[int16](s, key)
217175
}
218176

219177
// GetInt32 retrieves an int32 value from the State.
220-
// It returns the float64 and a boolean indicating successful type assertion.
178+
// It returns the int32 and a boolean indicating successful type assertion.
221179
func (s *State) GetInt32(key string) (int32, bool) {
222-
dep, ok := s.Get(key)
223-
if ok {
224-
if depInt32, okCast := dep.(int32); okCast {
225-
return depInt32, true
226-
}
227-
}
228-
return 0, false
180+
return GetState[int32](s, key)
229181
}
230182

231183
// GetInt64 retrieves an int64 value from the State.
232-
// It returns the float64 and a boolean indicating successful type assertion.
184+
// It returns the int64 and a boolean indicating successful type assertion.
233185
func (s *State) GetInt64(key string) (int64, bool) {
234-
dep, ok := s.Get(key)
235-
if ok {
236-
if depInt64, okCast := dep.(int64); okCast {
237-
return depInt64, true
238-
}
239-
}
240-
return 0, false
186+
return GetState[int64](s, key)
241187
}
242188

243189
// GetUint8 retrieves a uint8 value from the State.
244-
// It returns the float64 and a boolean indicating successful type assertion.
190+
// It returns the uint8 and a boolean indicating successful type assertion.
245191
func (s *State) GetUint8(key string) (uint8, bool) {
246-
dep, ok := s.Get(key)
247-
if ok {
248-
if depUint8, okCast := dep.(uint8); okCast {
249-
return depUint8, true
250-
}
251-
}
252-
return 0, false
192+
return GetState[uint8](s, key)
253193
}
254194

255195
// GetUint16 retrieves a uint16 value from the State.
256-
// It returns the float64 and a boolean indicating successful type assertion.
196+
// It returns the uint16 and a boolean indicating successful type assertion.
257197
func (s *State) GetUint16(key string) (uint16, bool) {
258-
dep, ok := s.Get(key)
259-
if ok {
260-
if depUint16, okCast := dep.(uint16); okCast {
261-
return depUint16, true
262-
}
263-
}
264-
return 0, false
198+
return GetState[uint16](s, key)
265199
}
266200

267201
// GetUint32 retrieves a uint32 value from the State.
268-
// It returns the float64 and a boolean indicating successful type assertion.
202+
// It returns the uint32 and a boolean indicating successful type assertion.
269203
func (s *State) GetUint32(key string) (uint32, bool) {
270-
dep, ok := s.Get(key)
271-
if ok {
272-
if depUint32, okCast := dep.(uint32); okCast {
273-
return depUint32, true
274-
}
275-
}
276-
return 0, false
204+
return GetState[uint32](s, key)
277205
}
278206

279207
// GetUint64 retrieves a uint64 value from the State.
280-
// It returns the float64 and a boolean indicating successful type assertion.
208+
// It returns the uint64 and a boolean indicating successful type assertion.
281209
func (s *State) GetUint64(key string) (uint64, bool) {
282-
dep, ok := s.Get(key)
283-
if ok {
284-
if depUint64, okCast := dep.(uint64); okCast {
285-
return depUint64, true
286-
}
287-
}
288-
return 0, false
210+
return GetState[uint64](s, key)
289211
}
290212

291213
// GetUintptr retrieves a uintptr value from the State.
292-
// It returns the float64 and a boolean indicating successful type assertion.
214+
// It returns the uintptr and a boolean indicating successful type assertion.
293215
func (s *State) GetUintptr(key string) (uintptr, bool) {
294-
dep, ok := s.Get(key)
295-
if ok {
296-
if depUintptr, okCast := dep.(uintptr); okCast {
297-
return depUintptr, true
298-
}
299-
}
300-
return 0, false
216+
return GetState[uintptr](s, key)
301217
}
302218

303219
// GetFloat32 retrieves a float32 value from the State.
304-
// It returns the float64 and a boolean indicating successful type assertion.
220+
// It returns the float32 and a boolean indicating successful type assertion.
305221
func (s *State) GetFloat32(key string) (float32, bool) {
306-
dep, ok := s.Get(key)
307-
if ok {
308-
if depFloat32, okCast := dep.(float32); okCast {
309-
return depFloat32, true
310-
}
311-
}
312-
return 0, false
222+
return GetState[float32](s, key)
313223
}
314224

315225
// GetComplex64 retrieves a complex64 value from the State.
316-
// It returns the float64 and a boolean indicating successful type assertion.
226+
// It returns the complex64 and a boolean indicating successful type assertion.
317227
func (s *State) GetComplex64(key string) (complex64, bool) {
318-
dep, ok := s.Get(key)
319-
if ok {
320-
if depComplex64, okCast := dep.(complex64); okCast {
321-
return depComplex64, true
322-
}
323-
}
324-
return 0, false
228+
return GetState[complex64](s, key)
325229
}
326230

327231
// GetComplex128 retrieves a complex128 value from the State.
328-
// It returns the float64 and a boolean indicating successful type assertion.
232+
// It returns the complex128 and a boolean indicating successful type assertion.
329233
func (s *State) GetComplex128(key string) (complex128, bool) {
330-
dep, ok := s.Get(key)
331-
if ok {
332-
if depComplex128, okCast := dep.(complex128); okCast {
333-
return depComplex128, true
334-
}
335-
}
336-
return 0, false
234+
return GetState[complex128](s, key)
337235
}
338236

339237
// serviceKey returns a key for a service in the State.

0 commit comments

Comments
 (0)