From nobody Mon Feb 9 16:22:00 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562250598; cv=none; d=zoho.com; s=zohoarc; b=XDWgB8LCnRiefgczPe41K9d5LsXcJ3XvpnG+303E3JzWwLSQf353+puiGRNKILKXDv3zXHdlAu9no6tIs40blFkXNbigew2swNVnH+0lK2ak/V7glTfQwMMub3CUgCBAvmyr6jEqO9c/Vh0zP7woZTgEmk2hSg8+xRbDro8BAT8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562250598; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=j5PWkXgeQqVsMJA7XmiqCaMgMxsD8NNGHlVll9IcMec=; b=B0uXBuFVpfaTL6gWv/f+KpIkiAVXdU4NgegidWBjOe8Dm7Z+KUC+XlXxnc7Z91ve7FgrSh2qlNi1O3C/WizJK5sQND1msfE1FZH60dffR5WT2QJ1SIqvIblvSaCT7RbIZZeHBCUQ2q2/gD2+VbVhxQBmO0XqPHFjbTeUK7LlQUU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1562250598570878.7583470101895; Thu, 4 Jul 2019 07:29:58 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 005B7821C1; Thu, 4 Jul 2019 14:29:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C762D5D71B; Thu, 4 Jul 2019 14:29:49 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 69EDF1972A; Thu, 4 Jul 2019 14:29:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x64EQgB5029712 for ; Thu, 4 Jul 2019 10:26:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 173A35B77C; Thu, 4 Jul 2019 14:26:42 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6FC4D5B2D2; Thu, 4 Jul 2019 14:26:41 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 4 Jul 2019 16:26:27 +0200 Message-Id: <93d1552af1a3e340987d6f4a3b6f1d6172d5eb6c.1562249939.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Cc: Kevin Wolf Subject: [libvirt] [PATCH 03/11] util: json: Introduce helper to prepend string into a virJSONValueObject X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 04 Jul 2019 14:29:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Libvirt treats the JSON objects as lists thus the values appear in the order they were added. To avoid too much changes introduce a helper which allows to prepend a string which will allow to keep certain outputs in order. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/libvirt_private.syms | 1 + src/util/virjson.c | 50 +++++++++++++++++++++++++++++++++------- src/util/virjson.h | 1 + 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b8772d2895..d21f415f97 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2244,6 +2244,7 @@ virJSONValueObjectGetValue; virJSONValueObjectHasKey; virJSONValueObjectIsNull; virJSONValueObjectKeysNumber; +virJSONValueObjectPrependString; virJSONValueObjectRemoveKey; virJSONValueObjectStealArray; virJSONValueObjectStealObject; diff --git a/src/util/virjson.c b/src/util/virjson.c index d2664b9d57..1cf2fb1e5c 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -606,10 +606,11 @@ virJSONValueNewObject(void) } -int -virJSONValueObjectAppend(virJSONValuePtr object, +static int +virJSONValueObjectInsert(virJSONValuePtr object, const char *key, - virJSONValuePtr value) + virJSONValuePtr value, + bool prepend) { virJSONObjectPair pair =3D { NULL, value }; int ret =3D -1; @@ -628,8 +629,13 @@ virJSONValueObjectAppend(virJSONValuePtr object, if (VIR_STRDUP(pair.key, key) < 0) return -1; - ret =3D VIR_APPEND_ELEMENT(object->data.object.pairs, - object->data.object.npairs, pair); + if (prepend) { + ret =3D VIR_INSERT_ELEMENT(object->data.object.pairs, 0, + object->data.object.npairs, pair); + } else { + ret =3D VIR_APPEND_ELEMENT(object->data.object.pairs, + object->data.object.npairs, pair); + } VIR_FREE(pair.key); return ret; @@ -637,14 +643,24 @@ virJSONValueObjectAppend(virJSONValuePtr object, int -virJSONValueObjectAppendString(virJSONValuePtr object, +virJSONValueObjectAppend(virJSONValuePtr object, + const char *key, + virJSONValuePtr value) +{ + return virJSONValueObjectInsert(object, key, value, false); +} + + +static int +virJSONValueObjectInsertString(virJSONValuePtr object, const char *key, - const char *value) + const char *value, + bool prepend) { virJSONValuePtr jvalue =3D virJSONValueNewString(value); if (!jvalue) return -1; - if (virJSONValueObjectAppend(object, key, jvalue) < 0) { + if (virJSONValueObjectInsert(object, key, jvalue, prepend) < 0) { virJSONValueFree(jvalue); return -1; } @@ -652,6 +668,24 @@ virJSONValueObjectAppendString(virJSONValuePtr object, } +int +virJSONValueObjectAppendString(virJSONValuePtr object, + const char *key, + const char *value) +{ + return virJSONValueObjectInsertString(object, key, value, false); +} + + +int +virJSONValueObjectPrependString(virJSONValuePtr object, + const char *key, + const char *value) +{ + return virJSONValueObjectInsertString(object, key, value, true); +} + + int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, diff --git a/src/util/virjson.h b/src/util/virjson.h index 41404a9a3e..a5f959595f 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -126,6 +126,7 @@ int virJSONValueObjectGetBoolean(virJSONValuePtr object= , const char *key, bool * int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key); int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key= , const char *value); +int virJSONValueObjectPrependString(virJSONValuePtr object, const char *ke= y, const char *value); int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *= key, int number); int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char = *key, unsigned int number); int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char = *key, long long number); --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list