[pve-devel] [PATCH manager v2 1/2] ui/Parser: add generic functions property_strings

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Aug 2 12:31:59 CEST 2018


Am 08/01/2018 um 08:29 PM schrieb Stoiko Ivanov:
> Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
> ---
>  www/manager6/Parser.js | 43 +++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 43 insertions(+)
> 
> diff --git a/www/manager6/Parser.js b/www/manager6/Parser.js
> index 13dce766..df2be5dc 100644
> --- a/www/manager6/Parser.js
> +++ b/www/manager6/Parser.js
> @@ -45,6 +45,49 @@ Ext.define('PVE.Parser', { statics: {
>  	       value === 'true';
>      },
>  
> +    parsePropertyString: function(value, defaultKey) {
> +	var res = {},
> +	errors = false;
> +
> +	Ext.Array.each(value.split(','), function(p) {
> +	    var kv = p.split('=', 2);
> +	    if (Ext.isDefined(kv[1])) {
> +		res[kv[0]] = kv[1];
> +	    } else if (Ext.isDefined(defaultKey)) {
> +		if (Ext.isDefined(res[defaultKey])) {
> +		    errors = true;
> +		    return false; //break
> +		}
> +		res[defaultKey] = kv[0];
> +	    } else {
> +		errors = true;
> +		return false; // break
> +	    }
> +	});
> +
> +	if (errors) {
> +	    return;
> +	}
> +
> +	return res;
> +    },
> +
> +    printPropertyString: function(data, defaultKey) {
> +	var stringparts = [];
> +
> +	Ext.Object.each(data, function(key, value) {
> +	    var keystring = '' ;
> +	    if (key === defaultKey) {
> +		keystring = '';
> +	    } else {
> +		keystring = key + '=';
> +	    }
> +	    stringparts.push(keystring+value);
> +	});
> +
> +	return stringparts.join(',');
> +    },
> +
>      parseQemuNetwork: function(key, value) {
>  	if (!(key && value)) {
>  	    return;
> 

What about the following change on top of yours?

----8<----
diff --git a/www/manager6/Parser.js b/www/manager6/Parser.js
index df2be5dc..5621f1a6 100644
--- a/www/manager6/Parser.js
+++ b/www/manager6/Parser.js
@@ -46,8 +46,7 @@ Ext.define('PVE.Parser', { statics: {
     },

     parsePropertyString: function(value, defaultKey) {
-       var res = {},
-       errors = false;
+       var res = {};

        Ext.Array.each(value.split(','), function(p) {
            var kv = p.split('=', 2);
@@ -55,20 +54,14 @@ Ext.define('PVE.Parser', { statics: {
                res[kv[0]] = kv[1];
            } else if (Ext.isDefined(defaultKey)) {
                if (Ext.isDefined(res[defaultKey])) {
-                   errors = true;
-                   return false; //break
+                   throw 'defaultKey may be only defined once in
propertySting';
                }
                res[defaultKey] = kv[0];
            } else {
-               errors = true;
-               return false; // break
+               throw 'invalid propertyString, not a key=value pair and
no defaultKey defined';
            }
        });

-       if (errors) {
-           return;
-       }
-
        return res;
     },

@@ -76,13 +69,11 @@ Ext.define('PVE.Parser', { statics: {
        var stringparts = [];

        Ext.Object.each(data, function(key, value) {
-           var keystring = '' ;
-           if (key === defaultKey) {
-               keystring = '';
+           if (defaultKey === undefined || key !== defaultKey) {
+               stringparts.push(key + '=' + value);
            } else {
-               keystring = key + '=';
+               stringparts.unshift(value);
            }
-           stringparts.push(keystring+value);
        });

        return stringparts.join(',');





More information about the pve-devel mailing list