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

Peter Krempa pkrempa at redhat.com
Tue Aug 28 11:05:40 UTC 2018


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 at 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 at 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 at redhat.com
> > https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180828/b72f43bc/attachment-0001.sig>


More information about the libvir-list mailing list