1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-05 05:45:22 +02:00

fix(code): remove code that is no longer necessary EE-6078 (#10256)

This commit is contained in:
andres-portainer 2023-09-05 22:35:16 -03:00 committed by GitHub
parent c748385879
commit 4a39122415
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 33 additions and 224 deletions

View file

@ -1,34 +0,0 @@
package maps
import "strings"
// Get a key from a nested map. Not support array for the moment
func Get(mapObj map[string]interface{}, path string, key string) interface{} {
if path == "" {
return mapObj[key]
}
paths := strings.Split(path, ".")
v := mapObj
for _, p := range paths {
if p == "" {
continue
}
value, ok := v[p].(map[string]interface{})
if ok {
v = value
} else {
return ""
}
}
return v[key]
}
// Copy copies all key/value pairs in src adding them to dst.
// When a key in src is already present in dst,
// the value in dst will be overwritten by the value associated
// with the key in src.
func Copy[M ~map[K]V, K comparable, V any](dst, src M) {
for k, v := range src {
dst[k] = v
}
}

View file

@ -1,38 +0,0 @@
package maps
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
t.Run("xx", func(t *testing.T) {
jsonStr := "{\"data\":{\"yesterday\":{\"sunrise\":\"06:19\"}}}"
data := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil {
fmt.Printf("error: %s", err)
return
}
result := Get(data, "data.yesterday", "sunrise")
fmt.Printf("result: %s\n", result)
expected := "06:19"
assert.Equal(t, expected, result)
})
t.Run("xx", func(t *testing.T) {
jsonStr := "{\"data\":{\"yesterday\": \"hahaha\"}}"
data := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonStr), &data)
if err != nil {
fmt.Printf("error: %s", err)
return
}
result := Get(data, "data.yesterday", "sunrise")
fmt.Printf("result: %s\n", result)
expected := ""
assert.Equal(t, expected, result)
})
}

View file

@ -1,69 +1,5 @@
package slices
// Contains is a generic function that returns true if the element is contained within the slice
func Contains[T comparable](elems []T, v T) bool {
return ContainsFunc(elems, func(s T) bool {
return s == v
})
}
// Contains is a generic function that returns true if the element is contained within the slice
func ContainsFunc[T any](elems []T, f func(T) bool) bool {
for _, s := range elems {
if f(s) {
return true
}
}
return false
}
func Find[T any](elems []T, f func(T) bool) (T, bool) {
for _, s := range elems {
if f(s) {
return s, true
}
}
// return default value
var result T
return result, false
}
// IndexFunc returns the first index i satisfying f(s[i]),
// or -1 if none do.
func IndexFunc[E any](s []E, f func(E) bool) int {
for i, v := range s {
if f(v) {
return i
}
}
return -1
}
// RemoveItem removes the first element from the slice that satisfies the given predicate
func RemoveItem[E comparable](s []E, predicate func(E) bool) []E {
index := IndexFunc(s, predicate)
if index == -1 {
return s
}
return RemoveIndex(s, index)
}
// RemoveIndex removes the element at the given index from the slice
func RemoveIndex[T any](s []T, index int) []T {
if len(s) == 0 {
return s
}
if index < 0 || index >= len(s) {
return s
}
s[index] = s[len(s)-1]
return s[:len(s)-1]
}
// Map applies the given function to each element of the slice and returns a new slice with the results
func Map[T, U any](s []T, f func(T) U) []U {
result := make([]U, len(s))