1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-25 23:02:22 +02:00

сделал MassFrom_Map()

This commit is contained in:
Nikitin Aleksandr
2024-10-28 11:07:06 +03:00
parent 32875646d5
commit 3010f5a868
2 changed files with 92 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"github.com/google/uuid"
"golang.org/x/exp/constraints"
"hash/fnv"
"os/exec"
"reflect"
@@ -1152,3 +1153,56 @@ func TimeMin(x time.Time, y ...time.Time) time.Time {
func Show_Version(Version string) {
println("Service version: ", Version)
}
// MassFrom_MapString - сортирует map по названию колонок и возвращает слайс
func MassFrom_MapString[V any](Map map[string]V) []V {
Otvet := make([]V, 0)
//сортировка по названию колонок
keys := make([]string, 0, len(Map))
for k := range Map {
keys = append(keys, k)
}
sort.Strings(keys)
//
for _, key1 := range keys {
Value, ok := Map[key1]
if ok == false {
fmt.Printf("Map[%s] not found\n", key1)
}
Otvet = append(Otvet, Value)
}
return Otvet
}
// SortMass - сортирует слайс
func SortMass[T constraints.Ordered](s []T) {
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
}
// MassFrom_Map - сортирует map по названию колонок и возвращает слайс
func MassFrom_Map[C constraints.Ordered, V any](Map map[C]V) []V {
Otvet := make([]V, 0)
//сортировка по названию колонок
keys := make([]C, 0, len(Map))
for k := range Map {
keys = append(keys, k)
}
SortMass(keys)
//
for _, key1 := range keys {
Value, ok := Map[key1]
if ok == false {
fmt.Printf("Map[%v] not found\n", key1)
}
Otvet = append(Otvet, Value)
}
return Otvet
}

View File

@@ -1150,3 +1150,41 @@ func TestShow_Repository_Code_ModifiedTime(t *testing.T) {
func TestShow_Version(t *testing.T) {
Show_Version("1")
}
func TestMassFrom_MapString(t *testing.T) {
// Test case for sorting a map by column names
t.Run("Sort map by keys", func(t *testing.T) {
input := map[string]int{"c": 3, "a": 1, "b": 2}
expected := []int{1, 2, 3}
result := MassFrom_MapString(input)
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
})
// Test case for handling a key not found in the map
t.Run("Key not found", func(t *testing.T) {
input := map[string]string{"a": "apple", "b": "banana"}
_ = MassFrom_MapString(input) // This call should print an error message for the missing key
// You can capture the output of fmt.Printf using https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string
})
}
func TestMassFrom_Map(t *testing.T) {
// Test case for sorting a map by column names
t.Run("Sort map by keys", func(t *testing.T) {
input := map[string]int{"c": 3, "a": 1, "b": 2}
expected := []int{1, 2, 3}
result := MassFrom_Map(input)
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
})
// Test case for handling a key not found in the map
t.Run("Key not found", func(t *testing.T) {
input := map[string]string{"a": "apple", "b": "banana"}
_ = MassFrom_Map(input) // This call should print an error message for the missing key
// You can capture the output of fmt.Printf using https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string
})
}