Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
32
pkg/redisv2/tool.go
Normal file
32
pkg/redisv2/tool.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package redisv2
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ErrUnEvenResults = errors.New("uneven number of results in interface array")
|
||||
|
||||
// Except an even number of results in the array
|
||||
func InterfaceArrayToStringMap(array []interface{})(result map[string]string, err error){
|
||||
if len(array) %2 != 0 {
|
||||
err = ErrUnEvenResults
|
||||
return
|
||||
}
|
||||
|
||||
result = make(map[string]string)
|
||||
for x := 0; x < len(array); x += 2 {
|
||||
key, ok := array[x].(string)
|
||||
if !ok {
|
||||
err =errors.Join(err, fmt.Errorf("failed to convert key %v to string", array[x]))
|
||||
continue
|
||||
}
|
||||
value, ok := array[x+1].(string)
|
||||
if !ok {
|
||||
err =errors.Join(err, fmt.Errorf("failed to convert value %v to string", array[x+1]))
|
||||
continue
|
||||
}
|
||||
result[key] = value
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user