From patchwork Mon Apr 15 21:26:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cole Robinson X-Patchwork-Id: 162283 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp3422458jan; Mon, 15 Apr 2019 14:27:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqwC4Z3ji5vQZXQJ9OtYoxvwD0tsGLzX4viB5HDuVIvP+LU2YJRK9LS7IUsy6B2aQ7GfiqX2 X-Received: by 2002:ac8:36c9:: with SMTP id b9mr61519285qtc.281.1555363626030; Mon, 15 Apr 2019 14:27:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555363626; cv=none; d=google.com; s=arc-20160816; b=govvHVLVZ4v0IS6REZJfL++OGh3axwub2Oxox8V4N6nVdDZCNjcLxp3vfC9oeQ5nez abfzXriVBso3j+jDo0PN+i6v4xtsGHvosnEgnjJ7GXpeR8dSos81lK4luzVnjYOi/cTQ 5/Jo/aReKXKHPzMyMV+FD/F3etOy+YcsyOEt1NBPM5Nc6UNl9AF8ZahLHnLz7zApRVFz HXY9D6la1ZThroraB/xJd5Ds/z2yWRcq0W36Q+efI85iCQu3SLagUim7Rbz0o2KX/Vj/ v31Jw+8MlFflowRjlv4RR6M5d2OfgOzET7BkD1CpLLRPo0PWD3YJpWlAU09lgVGEOBR+ aEUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :delivered-to; bh=XCDlPLgdRbUFhk3VMswvy9HXfJgKCwcy5fAdjYJetBE=; b=DqWPvs3RYiyiwXWO4hBdvpUgvQ+ewBjZI1X9Pcm6QWxk0ME5kqieK4FUXpobGWkECo z7zCKXTbljmHq7T5maZguzWnnFYduk8QeuMKrVlvfnSNC67+etG7InrMAOj2z3jZW72i rC1tr4w6vHy/PJLuTUu+dT0HSOWepeII5tvKCArxjNuomU+wwLnCAU5iReb+JGBZxBvS h+iTsFhJ8kmhGIw1bTmQIbK42C5s2VnbByxQ8BElziYA3QUnQdpdk07nXatoNiN1GTSa HEYKaoCccXa/GkvmTCLwPpV6bQInFQfSzGC8K3xSLMYwuRN/7r5oAZ9p68GOazAt3FSA YYYw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id y10si2977303qkl.86.2019.04.15.14.27.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Apr 2019 14:27:06 -0700 (PDT) Received-SPF: pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libvir-list-bounces@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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) Delivered-To: libvirt-list@redhat.com 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: , 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) 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='domain type', sp virEnumFromString called with bad value='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(-) -- 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list 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 #include "virenum.h" +#include "virerror.h" #include "virstring.h" #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; for (i = 0; i < ntypes; i++) if (STREQ(types[i], type)) return i; + error: + if (label) { + virReportError(VIR_ERR_INVALID_ARG, + _("Unknown '%s' value '%s'"), label, NULLSTR(type)); + } return -1; } @@ -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 >= ntypes) + if (type < 0 || type >= ntypes) { + if (label) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown '%s' internal value %d"), label, type); + } return NULL; + } 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); const char * virEnumToString(const char * const *types, unsigned int ntypes, - int type); + int type, + const char *label); # define VIR_ENUM_IMPL(name, lastVal, ...) \ static const char *const name ## TypeList[] = { __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) == lastVal)