[libvirt] [PATCH v2 01/15] util: json: add helper to iterate and steal members of json array

Pavel Hrdina phrdina at redhat.com
Wed Nov 9 14:22:22 UTC 2016


On Wed, Nov 02, 2016 at 05:13:33PM +0100, Peter Krempa wrote:
> Simplifies cases where JSON array members need to be transferred to a
> different structure.
> ---
> 
> Notes:
>     v2:
>     - iterate from beginning
>     - add option to keep array member in the array without breaking iteration
>     - documented callback return codes
> 
>  src/libvirt_private.syms |  1 +
>  src/util/virjson.c       | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
>  src/util/virjson.h       |  6 ++++++
>  3 files changed, 61 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 162fda5..4c08508 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1764,6 +1764,7 @@ virISCSIScanTargets;
> 
>  # util/virjson.h
>  virJSONValueArrayAppend;
> +virJSONValueArrayForeachSteal;
>  virJSONValueArrayGet;
>  virJSONValueArraySize;
>  virJSONValueArraySteal;
> diff --git a/src/util/virjson.c b/src/util/virjson.c
> index 1d8e6d5..d55600a 100644
> --- a/src/util/virjson.c
> +++ b/src/util/virjson.c
> @@ -949,6 +949,60 @@ virJSONValueArraySteal(virJSONValuePtr array,
>  }
> 
> 
> +/**
> + * virJSONValueArrayForeachSteal:
> + * @array: array to iterate
> + * @cb: callback called on every member of the array
> + * @opaque: custom data for the callback
> + *
> + * Iterates members of the array and calls the callback on every single member.
> + * The return codes of the callback are interpreted as follows:
> + * 0: callback claims ownership of the array element and is responsible for
> + *    freeing it

Maybe mention that the iteration continues in this case too.

> + * 1: callback does not claim the ownership, but the iteration continues
> + * -1: callback doesn't claim ownership and iteration does not continue

Just my OCD kicking in, use "does not" or "doesn't" in both cases :)

> + *
> + * Returns 0 if all members were iterated and/or stolen by the callback; -1
> + * on callback failure or if the JSON value object is not an array.
> + * The rest of the members stay in possession of the array and it's condensed.
> + */
> +int
> +virJSONValueArrayForeachSteal(virJSONValuePtr array,
> +                              virJSONArrayIteratorFunc cb,
> +                              void *opaque)

ACK
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20161109/1d38fe5e/attachment-0001.sig>


More information about the libvir-list mailing list