Skip to content

Commit 9a448d5

Browse files
authored
Merge pull request #149 from rst0git/2024-10-23-metadata
Recursively add metadata JSON fields to the tree
2 parents c770752 + 2e305b9 commit 9a448d5

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

internal/tree.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
335335
continue
336336
}
337337
localTree := annotationTree.AddBranch(key)
338-
for labelKey := range local {
339-
localTree.AddBranch(fmt.Sprintf("%s: %s", labelKey, local[labelKey]))
340-
}
338+
// Recursively add array/map JSON fields to the tree
339+
addMapToTree(localTree, local)
341340
case "io.kubernetes.cri-o.Volumes":
342341
// We know that some annotations contain a JSON string we can pretty print
343342
var local []mountAnnotations
@@ -359,3 +358,37 @@ func addPodInfoToTree(tree treeprint.Tree, info *checkpointInfo) {
359358
}
360359
}
361360
}
361+
362+
func addMapToTree(tree treeprint.Tree, data map[string]interface{}) {
363+
for key, value := range data {
364+
switch v := value.(type) {
365+
case map[string]interface{}:
366+
// Recursively add nested maps
367+
subTree := tree.AddBranch(fmt.Sprintf("%s:", key))
368+
addMapToTree(subTree, v)
369+
case []interface{}:
370+
// Handle arrays recursively
371+
arrayTree := tree.AddBranch(fmt.Sprintf("%s: ", key))
372+
addArrayToTree(arrayTree, v)
373+
default:
374+
tree.AddBranch(fmt.Sprintf("%s: %v", key, v))
375+
}
376+
}
377+
}
378+
379+
func addArrayToTree(tree treeprint.Tree, data []interface{}) {
380+
for i, value := range data {
381+
switch v := value.(type) {
382+
case map[string]interface{}:
383+
// Recursively add maps inside arrays
384+
subTree := tree.AddBranch(fmt.Sprintf("[%d]:", i))
385+
addMapToTree(subTree, v)
386+
case []interface{}:
387+
// Recursively add arrays inside arrays
388+
subArrayTree := tree.AddBranch(fmt.Sprintf("[%d]: ", i))
389+
addArrayToTree(subArrayTree, v)
390+
default:
391+
tree.AddBranch(fmt.Sprintf("[%d]: %v", i, v))
392+
}
393+
}
394+
}

0 commit comments

Comments
 (0)