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

Thomas Lamprecht t.lamprecht at proxmox.com
Fri May 18 09:56:39 CEST 2018


two comments inline, which can be probably ignored...

On 5/18/18 9:18 AM, Dietmar Maurer wrote:
> 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;

above 4 lines could be written as:

	return [ map { api_dump_remove_refs($_) } @$tree ];

> +    } elsif ($class eq 'HASH') {
> +	my $res = {};
> +	foreach my $k (keys %$tree) {
> +	    if (my $class = ref($tree->{$k})) {

You're redeclaring $class here, not a problem per se but may be confused
with the above declaration or could lead to subtle bugs if this gets
refactored or copied and adapted for somewhere else.

As said, just two small nits, the code looks actually good!

Reviewed-by: Thomas Lamprecht <t.lamprecht at proxmox.com>

> +		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) = @_;
>  
> 





More information about the pve-devel mailing list