From nobody Tue Feb 10 21:40:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1655469678; cv=none; d=zohomail.com; s=zohoarc; b=CVtxaeN4YVZkz8zrkFeirzdgSTDsPCVL8X0J0oUH9rPSFKSQnipa5QmHQ8/OtO5+2BuQwWLIvnrTEH6cZCB4rMrC7jM7CfvosyTFIiaqL77Kk4JGZG+eea/3TpdFpelaq24vAx9gptwUCiGrxL+LKPr78jwbPjflUtiTvR+1Z24= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655469678; 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; bh=JZ0vOcnsF21RFn7nIjZPVMrS8wCUCtbccTUvlstCT7Y=; b=VM/fRxYhAeK+z6Qw1v+qmn/LNRw9TOPnr3/LSmPyWUjY0Nyys/Y6R4QfM9r51lPrGAflQ6U1IDfx1EE9mQfUQQQVrJPyYkou6m36Qqnr+1ed4wt+JB/zaQWXkmw6Gsqm83aW3eE0Od+5350B3v0Ul3qfhxhwTQdvsYlfbsshBYg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1655469677996664.2309038308981; Fri, 17 Jun 2022 05:41:17 -0700 (PDT) Received: from localhost ([::1]:39208 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2BHo-0006ng-Lp for importer@patchew.org; Fri, 17 Jun 2022 08:41:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47190) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2AxA-0001p1-UX for qemu-devel@nongnu.org; Fri, 17 Jun 2022 08:19:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:36375) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2Ax6-000512-Hr for qemu-devel@nongnu.org; Fri, 17 Jun 2022 08:19:56 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-630-FxUZXtkaPA6tcTgSG_PYPw-1; Fri, 17 Jun 2022 08:19:50 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 89AC71C01B2B for ; Fri, 17 Jun 2022 12:19:50 +0000 (UTC) Received: from tapioca.redhat.com (unknown [10.40.192.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE770415F5E; Fri, 17 Jun 2022 12:19:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655468392; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JZ0vOcnsF21RFn7nIjZPVMrS8wCUCtbccTUvlstCT7Y=; b=I/bbjFZOzLC/gqTynwOv8BMXnYsOhyp3guLMzk2u2XtJeF/aLD4uEWlJ+MwWcvWAApRDtf EDLBBxCV1tH0HMKB9edR27QZ6oPoKNtNKEp82CtgKWkqD5CGB0yA+pm8VO3gnD412+VZeJ SxpF6IIwXik+y38gqAluYecozGZ0M20= X-MC-Unique: FxUZXtkaPA6tcTgSG_PYPw-1 From: Victor Toso To: qemu-devel@nongnu.org Cc: Eric Blake , Markus Armbruster , John Snow , Andrea Bolognani , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [RFC PATCH v2 7/8] qapi: golang: Add CommandResult type to Go Date: Fri, 17 Jun 2022 14:19:31 +0200 Message-Id: <20220617121932.249381-8-victortoso@redhat.com> In-Reply-To: <20220617121932.249381-1-victortoso@redhat.com> References: <20220617121932.249381-1-victortoso@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=victortoso@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1655469678740100001 Content-Type: text/plain; charset="utf-8" This patch adds a struct type in Go that will handle return values for QAPI's command types. The return value of a Command is, encouraged to be, QAPI's complex types or an Array of those. Every Command has a underlying CommandResult. The EmptyCommandReturn is for those that don't expect any data e.g: `{ "return": {} }`. All CommandReturn types implement the CommandResult interface. Example: qapi: | { 'command': 'query-sev', 'returns': 'SevInfo', | 'if': 'TARGET_I386' } go: | type QuerySevCommandReturn struct { | CommandId string `json:"id,omitempty"` | Result *SevInfo `json:"return"` | Error *QapiError `json:"error,omitempty"` | } usage: | // One can use QuerySevCommandReturn directly or | // command's interface GetReturnType() instead. | | input :=3D `{ "return": { "enabled": true, "api-major" : 0,` + | `"api-minor" : 0, "build-id" : 0,` + | `"policy" : 0, "state" : "running",` + | `"handle" : 1 } } ` | ret :=3D QuerySevCommandReturn{} | err :=3D json.Unmarshal([]byte(input), &ret) | if ret.Error !=3D nil { | // Handle command failure {"error": { ...}} | } else if ret.Result !=3D nil { | // ret.Result.Enable =3D=3D true | } Signed-off-by: Victor Toso --- scripts/qapi/golang.py | 73 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/scripts/qapi/golang.py b/scripts/qapi/golang.py index 123179cced..ab91cf124f 100644 --- a/scripts/qapi/golang.py +++ b/scripts/qapi/golang.py @@ -89,7 +89,8 @@ }} ''' =20 -# Only variable is @unm_cases to handle all command's names and associated= types. +# Only variable is @unm_cases to handle +# all command's names and associated types. TEMPLATE_COMMAND =3D ''' type Command interface {{ GetId() string @@ -145,10 +146,49 @@ }} ''' =20 +TEMPLATE_COMMAND_RETURN =3D ''' +type CommandReturn interface { + GetId() string + GetCommandName() string + GetError() error +} + +type EmptyCommandReturn struct { + CommandId string `json:"id,omitempty"` + Error *QapiError `json:"error,omitempty"` + Name string `json:"-"` +} + +func (r EmptyCommandReturn) MarshalJSON() ([]byte, error) { + return []byte(`{"return":{}}`), nil +} + +func (r *EmptyCommandReturn) GetId() string { + return r.CommandId +} + +func (r *EmptyCommandReturn) GetCommandName() string { + return r.Name +} + +func (r *EmptyCommandReturn) GetError() error { + return r.Error +} +''' + TEMPLATE_HELPER =3D ''' // Alias for go version lower than 1.18 type Any =3D interface{} =20 +type QapiError struct { + Class string `json:"class"` + Description string `json:"desc"` +} + +func (err *QapiError) Error() string { + return fmt.Sprintf("%s: %s", err.Class, err.Description) +} + // Creates a decoder that errors on unknown Fields // Returns true if successfully decoded @from string @into type // Returns false without error is failed with "unknown field" @@ -176,6 +216,7 @@ def __init__(self, prefix: str): self.schema =3D None self.events =3D {} self.commands =3D {} + self.command_results =3D {} self.golang_package_name =3D "qapi" =20 def visit_begin(self, schema): @@ -224,6 +265,7 @@ def visit_end(self): ''' self.target["command"] +=3D TEMPLATE_COMMAND.format(unm_cases=3Dun= m_cases) =20 + self.target["command"] +=3D TEMPLATE_COMMAND_RETURN =20 def visit_object_type(self: QAPISchemaGenGolangVisitor, name: str, @@ -390,6 +432,31 @@ def visit_command(self, self.commands[name] =3D type_name command_ret =3D "" init_ret_type_name =3D f'''EmptyCommandReturn {{ Name: "{name}" }}= ''' + if ret_type: + cmd_ret_name =3D qapi_to_go_type_name(name, "command return") + ret_type_name =3D qapi_schema_type_to_go_type(ret_type.name) + init_ret_type_name =3D f'''{cmd_ret_name}{{}}''' + isptr =3D "*" if ret_type_name[0] not in "*[" else "" + self.command_results[name] =3D ret_type_name + command_ret =3D f''' +type {cmd_ret_name} struct {{ + CommandId string `json:"id,omitempty"` + Result {isptr}{ret_type_name} `json:"return"` + Error *QapiError `json:"error,omitempty"` +}} + +func (r *{cmd_ret_name}) GetCommandName() string {{ + return "{name}" +}} + +func (r *{cmd_ret_name}) GetId() string {{ + return r.CommandId +}} + +func (r *{cmd_ret_name}) GetError() error {{ + return r.Error +}} +''' =20 self_contained =3D True if arg_type and arg_type.name.startswith("q_obj"): @@ -423,7 +490,7 @@ def visit_command(self, return &{init_ret_type_name} }} ''' - self.target["command"] +=3D content + methods + self.target["command"] +=3D content + methods + command_ret =20 def visit_event(self, name, info, ifcond, features, arg_type, boxed): assert name =3D=3D info.defn_name @@ -686,7 +753,7 @@ def qapi_to_go_type_name(name: str, meta: str) -> str: =20 name +=3D ''.join(word.title() for word in words[1:]) =20 - if meta in ["event", "command"]: + if meta in ["event", "command", "command return"]: name =3D name[:-3] if name.endswith("Arg") else name name +=3D meta.title().replace(" ", "") =20 --=20 2.36.1