Skip to content

Commit

Permalink
feat: add MustHaveValue helper
Browse files Browse the repository at this point in the history
  • Loading branch information
joshiste committed Jun 18, 2024
1 parent 4c00fb5 commit 3475e85
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
29 changes: 25 additions & 4 deletions extutil/extutil.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022 Steadybit GmbH
// SPDX-FileCopyrightText: 2024 Steadybit GmbH

// Package extutil contains a variety of util functions that were identified as common code duplication.
// More specialized packages exist for groups of use cases (extlogging and exthttp) for example.
Expand All @@ -8,6 +8,7 @@ package extutil
import (
"fmt"
"github.com/steadybit/extension-kit/extconversion"
"reflect"
"strconv"
"strings"
)
Expand Down Expand Up @@ -170,16 +171,36 @@ func ToUInt(val interface{}) uint {
}
}

// MustHaveValue panics if the given key is not present in the map or the value is nil or empty.
func MustHaveValue[T any, K comparable](m map[K]T, key K) T {
val, ok := m[key]
if !ok {
panic(fmt.Sprintf("missing value for '%v'", key))
}

kind := reflect.TypeOf(val).Kind()
if kind == reflect.Array || kind == reflect.Chan || kind == reflect.Map || kind == reflect.Slice || kind == reflect.String {
if reflect.ValueOf(val).Len() == 0 {
panic(fmt.Sprintf("value for '%v' is empty ", key))
}
} else if kind == reflect.Ptr {
if reflect.ValueOf(val).IsNil() {
panic(fmt.Sprintf("value for '%v' is nil ", key))
}
}
return val
}

func ToStringArray(s interface{}) []string {
if s == nil {
return nil
}

strings := make([]string, len(s.([]interface{})))
tokens := make([]string, len(s.([]interface{})))
for i, v := range s.([]interface{}) {
strings[i] = v.(string)
tokens[i] = v.(string)
}
return strings
return tokens
}

func JsonMangle[T any](in T) T {
Expand Down
26 changes: 26 additions & 0 deletions extutil/extutil_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Steadybit GmbH

package extutil

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
Expand Down Expand Up @@ -237,3 +241,25 @@ func TestMaskString(t *testing.T) {
})
}
}

func TestMustHaveValue(t *testing.T) {
assert.Equal(t, 0, MustHaveValue(map[string]int{"key": 0}, "key"))
assert.Panics(t, func() {
MustHaveValue(map[string]int{"key": 0}, "missing")
})

assert.Equal(t, "value", MustHaveValue(map[string]string{"key": "value"}, "key"))
assert.Panics(t, func() {
MustHaveValue(map[string]string{"key": "value"}, "missing")
})

assert.Equal(t, Ptr("value"), MustHaveValue(map[string]*string{"key": Ptr("value")}, "key"))
assert.Panics(t, func() {
MustHaveValue(map[string]*string{"empty": nil}, "empty")
})

assert.Equal(t, []string{"value"}, MustHaveValue(map[string][]string{"key": {"value"}}, "key"))
assert.Panics(t, func() {
MustHaveValue(map[string][]string{"empty": {}}, "empty")
})
}

0 comments on commit 3475e85

Please sign in to comment.