[pve-devel] [PATCH pve-common 1/4] Support named formats in typetext generation

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Oct 1 10:04:53 CEST 2015


Now that generate_typetext doesn't need to be accessed
anymore it made sense to move it to PodParser.pm as this is
the only place that uses it now.
PodParser now needs access to JSONSchema's $format_list, so
a JSONSchema::get_format was added.
---
 src/PVE/JSONSchema.pm | 46 +++++-----------------------------------------
 src/PVE/PodParser.pm  | 49 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 44 deletions(-)

diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm
index 3afc37a..51dfc89 100644
--- a/src/PVE/JSONSchema.pm
+++ b/src/PVE/JSONSchema.pm
@@ -101,6 +101,11 @@ sub register_format {
     $format_list->{$format} = $code;
 }
 
+sub get_format {
+    my ($format) = @_;
+    return $format_list->{$format};
+}
+
 # register some common type for pve
 
 register_format('string', sub {}); # allow format => 'string-list'
@@ -1341,45 +1346,4 @@ sub dump_config {
     return $data;
 }
 
-sub generate_typetext {
-    my ($schema) = @_;
-    my $typetext = '';
-    my (@optional, @required);
-    foreach my $key (sort keys %$schema) {
-	next if !$schema->{$key}->{format_description} &&
-	        !$schema->{$key}->{typetext};
-	if ($schema->{$key}->{optional}) {
-	    push @optional, $key;
-	} else {
-	    push @required, $key;
-	}
-    }
-    my ($pre, $post) = ('', '');
-    my $add = sub {
-	my ($key) = @_;
-	$typetext .= $pre;
-	my $entry = $schema->{$key};
-	if (my $desc = $entry->{format_description}) {
-	    $typetext .= $entry->{default_key} ? "[$key=]" : "$key=";
-	    $typetext .= "<$desc>";
-	} elsif (my $text = $entry->{typetext}) {
-	    $typetext .= $text;
-	} else {
-	    die "internal error: neither format_description nor typetext found";
-	}
-	$typetext .= $post;
-    };
-    foreach my $key (@required) {
-	&$add($key);
-	$pre = ', ';
-    }
-    $pre = $pre ? ' [,' : '[';
-    $post = ']';
-    foreach my $key (@optional) {
-	&$add($key);
-	$pre = ' [,';
-    }
-    return $typetext;
-}
-
 1;
diff --git a/src/PVE/PodParser.pm b/src/PVE/PodParser.pm
index 3cb3c56..eb443bd 100644
--- a/src/PVE/PodParser.pm
+++ b/src/PVE/PodParser.pm
@@ -44,6 +44,47 @@ sub command {
 
 # helpers used to generate our manual pages
 
+sub generate_typetext {
+    my ($schema) = @_;
+    my $typetext = '';
+    my (@optional, @required);
+    foreach my $key (sort keys %$schema) {
+	next if !$schema->{$key}->{format_description} &&
+	        !$schema->{$key}->{typetext};
+	if ($schema->{$key}->{optional}) {
+	    push @optional, $key;
+	} else {
+	    push @required, $key;
+	}
+    }
+    my ($pre, $post) = ('', '');
+    my $add = sub {
+	my ($key) = @_;
+	$typetext .= $pre;
+	my $entry = $schema->{$key};
+	if (my $desc = $entry->{format_description}) {
+	    $typetext .= $entry->{default_key} ? "[$key=]" : "$key=";
+	    $typetext .= "<$desc>";
+	} elsif (my $text = $entry->{typetext}) {
+	    $typetext .= $text;
+	} else {
+	    die "internal error: neither format_description nor typetext found";
+	}
+	$typetext .= $post;
+    };
+    foreach my $key (@required) {
+	&$add($key);
+	$pre = ', ';
+    }
+    $pre = $pre ? ' [,' : '[';
+    $post = ']';
+    foreach my $key (@optional) {
+	&$add($key);
+	$pre = ' [,';
+    }
+    return $typetext;
+}
+
 sub schema_get_type_text {
     my ($phash) = @_;
 
@@ -62,9 +103,11 @@ sub schema_get_type_text {
 	    return "$phash->{type} (-N - $phash->{maximum})";
 	}
     } elsif ($phash->{type} eq 'string') {
-	my $format = $phash->{format};
-	if ($format && ref($format) eq 'HASH') {
-	    return PVE::JSONSchema::generate_typetext($format);
+	if (my $format = $phash->{format}) {
+	    $format = PVE::JSONSchema::get_format($format) if ref($format) ne 'HASH';
+	    if (ref($format) eq 'HASH') {
+		return generate_typetext($format);
+	    }
 	}
     }
 
-- 
2.1.4





More information about the pve-devel mailing list