Skip to content

fix: let Attributes.Dig traverse nested Attributes values - #45

Open
tas50 wants to merge 1 commit into
mainfrom
fix/dig-nested-attributes
Open

tas50 wants to merge 1 commit into
mainfrom
fix/dig-nested-attributes

Conversation

@tas50

@tas50 tas50 commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Attributes.Dig asserted each level to map[string]any. A Go type assertion matches the dynamic type exactly, so it never matched an Attributes value — even though the two share an underlying type and convert freely.

Dig therefore only worked at the top level, where it converts explicitly (var cur any = map[string]any(a)). One level down:

n := &Node{Normal: Attributes{"network": Attributes{"gw": "10.0.0.1"}}}
n.Attribute("network.gw")   // → nil, false

JSON-decoded nodes were unaffected, because encoding/json only ever produces map[string]any — which is exactly why the existing tests missed it. But any attribute tree built in Go silently reported "not found", through Dig, GetString, Attribute and AttributeString alike.

Fixed by unwrapping both shapes in one helper.

Test plan

  • TestDig_TraversesNestedAttributes — two levels of Attributes, via Dig and GetString
  • TestNodeAttribute_TraversesNestedAttributes — via the Node precedence lookup
  • Both failed before the change (Dig = <nil>, false)
  • Existing JSON-decoded attribute tests still pass, so the map[string]any path is unchanged
  • go vet ./... clean
  • go test ./... -race -count=2
  • No public surface change, so no README Status update needed

@tas50
tas50 force-pushed the fix/dig-nested-attributes branch 3 times, most recently from 38459c2 to f31fe84 Compare September 8, 2026 16:53
A Go type assertion matches the dynamic type exactly, so asserting to
map[string]any never matched an Attributes value even though the two share
an underlying type. Dig only worked at the top level, where it converts
explicitly; one level down it gave up:

	n := &Node{Normal: Attributes{"network": Attributes{"gw": "10.0.0.1"}}}
	n.Attribute("network.gw")  // nil, false

JSON-decoded nodes were unaffected - encoding/json only ever produces
map[string]any - which is why no test caught it. Any attribute tree built
in Go silently reported "not found", through Dig, GetString, Attribute and
AttributeString alike.

Unwrap both shapes.

Signed-off-by: Tim Smith <tim@mondoo.com>
@tas50
tas50 force-pushed the fix/dig-nested-attributes branch from f31fe84 to ab91c24 Compare September 10, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant