From patchwork Wed May 9 16:55:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 10390287 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 53A5560318 for ; Wed, 9 May 2018 17:21:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4347A2852E for ; Wed, 9 May 2018 17:21:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 37588285C4; Wed, 9 May 2018 17:21:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D0BF52852E for ; Wed, 9 May 2018 17:21:10 +0000 (UTC) Received: from localhost ([::1]:57893 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGSm5-0007p8-Sh for patchwork-qemu-devel@patchwork.kernel.org; Wed, 09 May 2018 13:21:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGSNb-0003bl-51 for qemu-devel@nongnu.org; Wed, 09 May 2018 12:55:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGSNa-0006nu-D0 for qemu-devel@nongnu.org; Wed, 09 May 2018 12:55:51 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45174 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fGSNX-0006i0-CP; Wed, 09 May 2018 12:55:47 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7FBA181A8B93; Wed, 9 May 2018 16:55:46 +0000 (UTC) Received: from localhost (unknown [10.40.205.23]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2C031215CDA7; Wed, 9 May 2018 16:55:46 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 9 May 2018 18:55:24 +0200 Message-Id: <20180509165530.29561-8-mreitz@redhat.com> In-Reply-To: <20180509165530.29561-1-mreitz@redhat.com> References: <20180509165530.29561-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Wed, 09 May 2018 16:55:46 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Wed, 09 May 2018 16:55:46 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mreitz@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 07/13] qdict: Add qdict_stringify_for_keyval() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Markus Armbruster , Michael Roth , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The purpose of this function is to prepare a QDict for consumption by the keyval visitor, which only accepts strings and QNull. Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- include/block/qdict.h | 2 ++ qobject/qdict.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/block/qdict.h b/include/block/qdict.h index 825e096a72..05a24677b5 100644 --- a/include/block/qdict.h +++ b/include/block/qdict.h @@ -32,4 +32,6 @@ typedef struct QDictRenames { } QDictRenames; bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp); +void qdict_stringify_for_keyval(QDict *qdict); + #endif diff --git a/qobject/qdict.c b/qobject/qdict.c index 0554c64553..83678c4951 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -1087,3 +1087,60 @@ bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp) } return true; } + +/** + * Convert all values in a QDict so it can be consumed by the keyval + * input visitor. QNull values are left as-is, all other values are + * converted to strings. + * + * @qdict must be flattened, i.e. it may not contain any nested QDicts + * or QLists. + */ +void qdict_stringify_for_keyval(QDict *qdict) +{ + const QDictEntry *e; + + for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { + QString *new_value = NULL; + + switch (qobject_type(e->value)) { + case QTYPE_QNULL: + /* leave as-is */ + break; + + case QTYPE_QNUM: { + char *str = qnum_to_string(qobject_to(QNum, e->value)); + new_value = qstring_from_str(str); + g_free(str); + break; + } + + case QTYPE_QSTRING: + /* leave as-is */ + break; + + case QTYPE_QDICT: + case QTYPE_QLIST: + /* @qdict must be flattened */ + abort(); + + case QTYPE_QBOOL: + if (qbool_get_bool(qobject_to(QBool, e->value))) { + new_value = qstring_from_str("on"); + } else { + new_value = qstring_from_str("off"); + } + break; + + case QTYPE_NONE: + case QTYPE__MAX: + abort(); + } + + if (new_value) { + QDictEntry *mut_e = (QDictEntry *)e; + qobject_unref(mut_e->value); + mut_e->value = QOBJECT(new_value); + } + } +}