[pve-devel] [PATCH common] print_text_table: sort by leftmost column with no undefined value

Fabian Ebner f.ebner at proxmox.com
Mon Apr 27 13:46:11 CEST 2020


On 27.04.20 13:27, Thomas Lamprecht wrote:
> On 4/27/20 11:41 AM, Fabian Ebner wrote:
>> This restores the behavior for sort_key as it's described in the
>> comment for print_text_table.
>>
>> Fixes warnings, e.g.
>> pvesh get /nodes/<NODE>/network
>> would print "use of uninitialized"-warnings if there are inactive
>> network interfaces, because for those, 'active' is undef.
> 
> why not just handle undef in the sorter?
> 
> This adds another cycling over all data and goes from sorted but warning
> to unsorted depending on data, which seems a bit weir/nondeterministic..
> 
> 

I thought about that first, but then saw the description of sort_key. 
I'll send a v2 which sorts with potential undef's and adapt the comment 
as well.

>>
>> Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
>> ---
>>   src/PVE/CLIFormatter.pm | 19 ++++++++++++++++++-
>>   1 file changed, 18 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
>> index 0e9cbe6..9f141e1 100644
>> --- a/src/PVE/CLIFormatter.pm
>> +++ b/src/PVE/CLIFormatter.pm
>> @@ -144,6 +144,23 @@ sub data_to_text {
>>       }
>>   }
>>   
>> +sub leftmost_sortable_property {
>> +    my ($data, $properties) = @_;
>> +
>> +    foreach my $property (@{$properties}) {
>> +	my $sortable = 1;
>> +	foreach my $entry (@{$data}) {
>> +	    if (!defined($entry->{$property})) {
>> +		$sortable = 0;
>> +		last;
>> +	    }
>> +	}
>> +	return $property if $sortable eq 1;
>> +    }
>> +
>> +    return undef;
>> +}
>> +
>>   # prints a formatted table with a title row.
>>   # $data - the data to print (array of objects)
>>   # $returnprops -json schema property description
>> @@ -170,7 +187,7 @@ sub print_text_table {
>>       my $utf8 = $terminal_opts->{utf8};
>>       my $encoding = $terminal_opts->{encoding} // 'UTF-8';
>>   
>> -    $sort_key //= $props_to_print->[0];
>> +    $sort_key //= leftmost_sortable_property($data, $props_to_print);
>>   
>>       if (defined($sort_key) && $sort_key ne 0) {
>>   	my $type = $returnprops->{$sort_key}->{type} // 'string';
>>
> 




More information about the pve-devel mailing list