[libvirt] [PATCH] util: json: Allow converting a virTristate(Bool|Switch) into JSON

Peter Krempa posted 1 patch 5 years, 7 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/9c4ed4cc393cf33c1548cbe1a2d40288bbc3f542.1535386625.git.pkrempa@redhat.com
Test syntax-check passed
src/util/virjson.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
[libvirt] [PATCH] util: json: Allow converting a virTristate(Bool|Switch) into JSON
Posted by Peter Krempa 5 years, 7 months ago
Add a new modifier letter for virJSONValueObjectAddVArgs which will add
a boolean value with our tristate semantics. The value is omitted when
the _ABSENT value is used.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/util/virjson.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 29530dcb15..e45f1fab06 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -142,6 +142,8 @@ virJSONValueGetType(const virJSONValue *value)
  *
  * b: boolean value
  * B: boolean value, omitted if false
+ * T: boolean value specified by a virTristate(Bool|Switch) value, omitted on
+ * the _ABSENT value
  *
  * d: double precision floating point number
  * n: json null value
@@ -266,12 +268,23 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
         }   break;

         case 'B':
+        case 'T':
         case 'b': {
             int val = va_arg(args, int);

             if (!val && type == 'B')
                 continue;

+            if (type == 'T') {
+                if (val == VIR_TRISTATE_SWITCH_ABSENT)
+                    continue;
+
+                if (val == VIR_TRISTATE_BOOL_NO)
+                    val = 0;
+                else
+                    val = 1;
+            }
+
             rc = virJSONValueObjectAppendBoolean(obj, key, val);
         }   break;

-- 
2.16.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] util: json: Allow converting a virTristate(Bool|Switch) into JSON
Posted by Erik Skultety 5 years, 7 months ago
On Mon, Aug 27, 2018 at 06:17:05PM +0200, Peter Krempa wrote:
> Add a new modifier letter for virJSONValueObjectAddVArgs which will add
> a boolean value with our tristate semantics. The value is omitted when
> the _ABSENT value is used.
>
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/util/virjson.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/src/util/virjson.c b/src/util/virjson.c
> index 29530dcb15..e45f1fab06 100644
> --- a/src/util/virjson.c
> +++ b/src/util/virjson.c
> @@ -142,6 +142,8 @@ virJSONValueGetType(const virJSONValue *value)
>   *
>   * b: boolean value
>   * B: boolean value, omitted if false
> + * T: boolean value specified by a virTristate(Bool|Switch) value, omitted on
> + * the _ABSENT value
>   *
>   * d: double precision floating point number
>   * n: json null value
> @@ -266,12 +268,23 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
>          }   break;
>
>          case 'B':
> +        case 'T':
>          case 'b': {
>              int val = va_arg(args, int);
>
>              if (!val && type == 'B')
>                  continue;
>
> +            if (type == 'T') {
> +                if (val == VIR_TRISTATE_SWITCH_ABSENT)

Just curious, was ^this on purpose to emphasize the fact that it would really
work with TristateSwitch since both _ABSENTs == 0 or just a coincidence? I'd
probably prefer _SWITCH_ABSENT slightly more to be consistent with the enum
being tested below.

Reviewed-by: Erik Skultety <eskultet@redhat.com>

> +                    continue;
> +
> +                if (val == VIR_TRISTATE_BOOL_NO)
> +                    val = 0;
> +                else
> +                    val = 1;
> +            }
> +
>              rc = virJSONValueObjectAppendBoolean(obj, key, val);
>          }   break;
>
> --
> 2.16.2
>
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] util: json: Allow converting a virTristate(Bool|Switch) into JSON
Posted by Peter Krempa 5 years, 7 months ago
On Tue, Aug 28, 2018 at 12:45:27 +0200, Erik Skultety wrote:
> On Mon, Aug 27, 2018 at 06:17:05PM +0200, Peter Krempa wrote:
> > Add a new modifier letter for virJSONValueObjectAddVArgs which will add
> > a boolean value with our tristate semantics. The value is omitted when
> > the _ABSENT value is used.
> >
> > Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> > ---
> >  src/util/virjson.c | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> >
> > diff --git a/src/util/virjson.c b/src/util/virjson.c
> > index 29530dcb15..e45f1fab06 100644
> > --- a/src/util/virjson.c
> > +++ b/src/util/virjson.c
> > @@ -142,6 +142,8 @@ virJSONValueGetType(const virJSONValue *value)
> >   *
> >   * b: boolean value
> >   * B: boolean value, omitted if false
> > + * T: boolean value specified by a virTristate(Bool|Switch) value, omitted on
> > + * the _ABSENT value
> >   *
> >   * d: double precision floating point number
> >   * n: json null value
> > @@ -266,12 +268,23 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
> >          }   break;
> >
> >          case 'B':
> > +        case 'T':
> >          case 'b': {
> >              int val = va_arg(args, int);
> >
> >              if (!val && type == 'B')
> >                  continue;
> >
> > +            if (type == 'T') {
> > +                if (val == VIR_TRISTATE_SWITCH_ABSENT)
> 
> Just curious, was ^this on purpose to emphasize the fact that it would really
> work with TristateSwitch since both _ABSENTs == 0 or just a coincidence? I'd
> probably prefer _SWITCH_ABSENT slightly more to be consistent with the enum
> being tested below.

No, that was a mistake. I'll fix it before pushing. I'll also order the
'B' and 'b' cases together.

> 
> Reviewed-by: Erik Skultety <eskultet@redhat.com>
> 
> > +                    continue;
> > +
> > +                if (val == VIR_TRISTATE_BOOL_NO)
> > +                    val = 0;
> > +                else
> > +                    val = 1;
> > +            }
> > +
> >              rc = virJSONValueObjectAppendBoolean(obj, key, val);
> >          }   break;
> >
> > --
> > 2.16.2
> >
> > --
> > libvir-list mailing list
> > libvir-list@redhat.com
> > https://www.redhat.com/mailman/listinfo/libvir-list
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list