mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-04 14:01:30 +02:00
Introduce service accounts and bootstrap tokens (#22)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package structpath
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Lookup(target interface{}, path []string) (s string, ok bool) {
|
||||
currentValue := reflect.ValueOf(target)
|
||||
|
||||
for _, pathElement := range path {
|
||||
pathElement := pathElement
|
||||
|
||||
field := currentValue.FieldByNameFunc(func(fieldName string) bool {
|
||||
return strings.EqualFold(fieldName, pathElement)
|
||||
})
|
||||
|
||||
if !field.IsValid() {
|
||||
return "", false
|
||||
}
|
||||
|
||||
currentValue = field
|
||||
}
|
||||
|
||||
result, ok := currentValue.Interface().(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return result, true
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package structpath_test
|
||||
|
||||
import (
|
||||
"github.com/cirruslabs/orchard/internal/structpath"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStructPath(t *testing.T) {
|
||||
target := struct {
|
||||
Name string
|
||||
Aliases []string
|
||||
}{
|
||||
Name: "Test",
|
||||
Aliases: []string{"Check-up", "Evaluation"},
|
||||
}
|
||||
|
||||
t.Run("normal scenario", func(t *testing.T) {
|
||||
result, ok := structpath.Lookup(target, []string{"name"})
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "Test", result)
|
||||
})
|
||||
|
||||
t.Run("non-existent field", func(t *testing.T) {
|
||||
_, ok := structpath.Lookup(target, []string{"non-existent"})
|
||||
require.False(t, ok)
|
||||
})
|
||||
|
||||
t.Run("non-string field", func(t *testing.T) {
|
||||
_, ok := structpath.Lookup(target, []string{"aliases"})
|
||||
require.False(t, ok)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user