[pve-devel] [PATCH pve-common] api_dump_remove_refs: prepare API tree for use with to_json($tree)

Dietmar Maurer dietmar at proxmox.com
Fri May 18 09:18:41 CEST 2018


We want to use this with the extractapi.pl helper (pve-docs, pve-api-client).

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 src/PVE/RESTHandler.pm | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/PVE/RESTHandler.pm b/src/PVE/RESTHandler.pm
index 3f5c732..5c64a20 100644
--- a/src/PVE/RESTHandler.pm
+++ b/src/PVE/RESTHandler.pm
@@ -139,6 +139,39 @@ sub api_dump_cleanup_tree {
 
 }
 
+# api_dump_remove_refs: prepare API tree for use with to_json($tree)
+sub api_dump_remove_refs {
+    my ($tree) = @_;
+
+    my $class = ref($tree);
+    return $tree if !$class;
+
+    if ($class eq 'ARRAY') {
+	my $res = [];
+	foreach my $el (@$tree) {
+	    push @$res, api_dump_remove_refs($el);
+	}
+	return $res;
+    } elsif ($class eq 'HASH') {
+	my $res = {};
+	foreach my $k (keys %$tree) {
+	    if (my $class = ref($tree->{$k})) {
+		if ($class eq 'CODE') {
+		    next if $k eq 'completion';
+		}
+		$res->{$k} = api_dump_remove_refs($tree->{$k});
+	    } else {
+		$res->{$k} = $tree->{$k};
+	    }
+	}
+	return $res;
+    } elsif ($class eq 'Regexp') {
+	return "$tree"; # return string representation
+    } else {
+	die "unknown class '$class'\n";
+    }
+}
+
 sub api_dump {
     my ($class, $prefix) = @_;
 
-- 
2.11.0




More information about the pve-devel mailing list