Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support root level nested value lookups #16

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions altsrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ func tracef(format string, a ...any) {
)
}

// NestedVal checks if the name has '.' delimiters.
// If so, it tries to traverse the tree by the '.' delimited sections to find
// NestedVal returns a value from the given map. The lookup name may be a dot-separated path into the map.
// If that is the case, it will recursively traverse the map based on the '.' delimited sections to find
// a nested value for the key.
func NestedVal(name string, tree map[any]any) (any, bool) {
if sections := strings.Split(name, "."); len(sections) > 1 {
node := tree
sections := strings.Split(name, ".")
if name == "" || len(sections) == 0 {
return nil, false
}

node := tree

// traverse into the map based on the dot-separated sections
if len(sections) >= 2 { // the last section is the value we want, we will return it directly at the end
for _, section := range sections[:len(sections)-1] {
child, ok := node[section]
if !ok {
Expand All @@ -66,10 +73,10 @@ func NestedVal(name string, tree map[any]any) (any, bool) {
return nil, false
}
}
if val, ok := node[sections[len(sections)-1]]; ok {
return val, true
}
}

if val, ok := node[sections[len(sections)-1]]; ok {
return val, true
}
return nil, false
}
42 changes: 30 additions & 12 deletions altsrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var (

func TestNestedVal(t *testing.T) {
tests := []struct {
name string
key string
m map[any]any
val any
b bool
name string
key string
m map[any]any
val any
found bool
}{
{
name: "No map no key",
Expand All @@ -48,6 +48,15 @@ func TestNestedVal(t *testing.T) {
"foo": 10,
},
},
{
name: "Level 1",
key: "foobar",
m: map[any]any{
"foobar": 10,
},
val: 10,
found: true,
},
{
name: "Level 2",
key: "foo.bar",
Expand All @@ -56,8 +65,8 @@ func TestNestedVal(t *testing.T) {
"bar": 10,
},
},
val: 10,
b: true,
val: 10,
found: true,
},
{
name: "Level 2 invalid key",
Expand All @@ -68,6 +77,15 @@ func TestNestedVal(t *testing.T) {
},
},
},
{
name: "Level 2 string map type",
key: "foo.bar1",
m: map[any]any{
"foo": map[string]any{
"bar": "10",
},
},
},
{
name: "Level 3 no entry",
key: "foo.bar.t",
Expand All @@ -87,8 +105,8 @@ func TestNestedVal(t *testing.T) {
},
},
},
val: "sss",
b: true,
val: "sss",
found: true,
},
{
name: "Level 3 invalid key",
Expand Down Expand Up @@ -124,15 +142,15 @@ func TestNestedVal(t *testing.T) {
},
},
},
val: []int{10},
b: true,
val: []int{10},
found: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
val, b := NestedVal(test.key, test.m)
if !test.b {
if !test.found {
assert.False(t, b)
} else {
assert.True(t, b)
Expand Down
7 changes: 6 additions & 1 deletion uri_source_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func TestReadURI(t *testing.T) {
{
name: "Invalid http URL",
path: "http://foo",
err: "no such host",

// locally we get: "dial tcp: lookup foo: no such host"
// but on CI local networks are disabled,
// so the error is: "dial tcp: lookup foo on 127.0.0.11:53: server misbehaving"
// therefore let's check for the "lookup foo", which is in both errors
err: "lookup foo",
},
{
name: "valid http URL",
Expand Down