From nobody Sun Feb 8 16:05:47 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=1555363626; cv=none; d=zoho.com; s=zohoarc; b=Dnyeb/Kc1sBy+MItY2S+EBSERAOSiQ1qIesht8rZU9M0VuA8VeVxp+YI6qMI/+hgIZdQhNigSM3ugMdCE1ejl+hCjnG3Wsce/C/4IMVFNP/dYfQrpFCe+VhtD/pTWiqrhwGGjXXLmebN1oDO6YHSFQTqM19IblCclC3nEy7vy2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555363626; h=Content-Type:Content-Transfer-Encoding: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=XCDlPLgdRbUFhk3VMswvy9HXfJgKCwcy5fAdjYJetBE=; b=fMDpKJyaOFP2GmhbXGWtbwrIMar1Zx9I4EIvjBUpE+4Ak29uFR7VBXk83XC13T3N2pkIJv4U/d5HJ1/l3UTZ5qPsFiMi02kR5tgspVlqsCTxXMcGSoRbuwyh3mnRunX0xFDNSCVq9UE8YKdrrkqnp/bO+GSAY4YuzLUpms7aFAU= 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 1555363626363359.8679536998226; Mon, 15 Apr 2019 14:27:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B84CE59441; Mon, 15 Apr 2019 21:27:04 +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 91F93608C2; Mon, 15 Apr 2019 21:27:04 +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 55D803FB12; Mon, 15 Apr 2019 21:27:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x3FLQrJt032240 for ; Mon, 15 Apr 2019 17:26:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1160F608C8; Mon, 15 Apr 2019 21:26:53 +0000 (UTC) Received: from worklaptop.redhat.com (ovpn-124-108.rdu2.redhat.com [10.10.124.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88C69608C2; Mon, 15 Apr 2019 21:26:52 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Mon, 15 Apr 2019 17:26:40 -0400 Message-Id: <023e3a93a9fcab311b5db29956831c2fedd8e591.1555363505.git.crobinso@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 1/3] util: enum: Add 'label' arg to virEnum*String 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 15 Apr 2019 21:27:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This will allow VIR_ENUM_IMPL calls to register a string label which will be used to raise errors from virEnum*String functions. For example, if virDomainVirtType may use label=3D'domain type', sp virEnumFromString called with bad value=3D'zzz' will raise: invalid argument: Unknown 'domain type' value 'zzz' Signed-off-by: Cole Robinson --- src/util/virenum.c | 21 +++++++++++++++++---- src/util/virenum.h | 12 ++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/util/virenum.c b/src/util/virenum.c index 26093bd795..6ae8d9fb2a 100644 --- a/src/util/virenum.c +++ b/src/util/virenum.c @@ -18,6 +18,7 @@ #include =20 #include "virenum.h" +#include "virerror.h" #include "virstring.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE @@ -60,16 +61,22 @@ virTristateSwitchFromBool(bool val) int virEnumFromString(const char * const *types, unsigned int ntypes, - const char *type) + const char *type, + const char *label) { size_t i; if (!type) - return -1; + goto error; =20 for (i =3D 0; i < ntypes; i++) if (STREQ(types[i], type)) return i; =20 + error: + if (label) { + virReportError(VIR_ERR_INVALID_ARG, + _("Unknown '%s' value '%s'"), label, NULLSTR(type)); + } return -1; } =20 @@ -77,10 +84,16 @@ virEnumFromString(const char * const *types, const char * virEnumToString(const char * const *types, unsigned int ntypes, - int type) + int type, + const char *label) { - if (type < 0 || type >=3D ntypes) + if (type < 0 || type >=3D ntypes) { + if (label) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown '%s' internal value %d"), label, typ= e); + } return NULL; + } =20 return types[type]; } diff --git a/src/util/virenum.h b/src/util/virenum.h index 3ae1a70b72..706ae752d8 100644 --- a/src/util/virenum.h +++ b/src/util/virenum.h @@ -24,24 +24,28 @@ int virEnumFromString(const char * const *types, unsigned int ntypes, - const char *type); + const char *type, + const char *label); =20 const char * virEnumToString(const char * const *types, unsigned int ntypes, - int type); + int type, + const char *label); =20 # define VIR_ENUM_IMPL(name, lastVal, ...) \ static const char *const name ## TypeList[] =3D { __VA_ARGS__ }; \ const char *name ## TypeToString(int type) { \ return virEnumToString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, \ + NULL); \ } \ int name ## TypeFromString(const char *type) { \ return virEnumFromString(name ## TypeList, \ ARRAY_CARDINALITY(name ## TypeList), \ - type); \ + type, \ + NULL); \ } \ verify(ARRAY_CARDINALITY(name ## TypeList) =3D=3D lastVal) =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list