From: Wolfgang Bumiller Date: Thu, 1 Oct 2015 08:36:29 +0000 (+0200) Subject: added JSONSchema::print_property_string X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=94dd44353294418b389414c260e1e1c4b991950c;hp=b944a22a6d12e9d1d1052855ae9727b77afe7edb added JSONSchema::print_property_string This will be used to format comma-separated property list strings. --- diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm index b9ef26a..0cb281d 100644 --- a/src/PVE/JSONSchema.pm +++ b/src/PVE/JSONSchema.pm @@ -532,6 +532,74 @@ sub parse_property_string { return $res; } +sub print_property_string { + my ($data, $format, $skip, $path) = @_; + + if (ref($format) ne 'HASH') { + my $schema = $format_list->{$format}; + die "not a valid format: $format" if !$schema; + $format = $schema; + } + + my $errors = {}; + check_object($path, $format, $data, undef, $errors); + if (scalar(%$errors)) { + raise "format error", errors => $errors; + } + + my $default_key; + my %skipped = map { $_ => 1 } @$skip; + my %allowed; + my %required; # this is a set, all present keys are required regardless of value + foreach my $key (keys %$format) { + $allowed{$key} = 1; + if (!$format->{$key}->{optional} && !$skipped{$key}) { + $required{$key} = 1; + } + + # Skip default keys + if ($format->{$key}->{default_key}) { + if ($default_key) { + warn "multiple default keys in schema ($default_key, $key)"; + } else { + $default_key = $key; + $skipped{$key} = 1; + } + } + } + + my ($text, $comma); + if ($default_key) { + $text = "$data->{$default_key}"; + $comma = ','; + } else { + $text = ''; + $comma = ''; + } + + foreach my $key (sort keys %$data) { + die "invalid key: $key" if !$allowed{$key}; + delete $required{$key}; + next if $skipped{$key}; + + my $type = $format->{$key}->{type}; + my $value = $data->{$key}; + $text .= $comma; + $comma = ','; + if ($type eq 'disk-size') { + $text .= "$key=" . format_size($value); + } else { + $text .= "$key=$value"; + } + } + + if (my $missing = join(',', keys %required)) { + die "missing properties: $missing"; + } + + return $text; +} + sub add_error { my ($errors, $path, $msg) = @_;