From: Victor Toso <victortoso@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Andrea Bolognani <abologna@redhat.com>,
qemu-devel@nongnu.org, Eric Blake <eblake@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
John Snow <jsnow@redhat.com>
Subject: Re: [RFC PATCH v2 4/8] qapi: golang: Generate qapi's union types in Go
Date: Fri, 19 Aug 2022 09:20:52 +0200 [thread overview]
Message-ID: <20220819072052.yl4gvepa5ectlvci@tapioca> (raw)
In-Reply-To: <20220817162556.fqjq74dtgi2uuyla@tapioca>
[-- Attachment #1.1: Type: text/plain, Size: 2214 bytes --]
Hi,
On Wed, Aug 17, 2022 at 06:25:56PM +0200, Victor Toso wrote:
> On Wed, Jul 06, 2022 at 10:48:06AM +0100, Daniel P. Berrangé wrote:
> > On Wed, Jul 06, 2022 at 10:37:54AM +0100, Daniel P. Berrangé wrote:
> > > On Wed, Jul 06, 2022 at 04:28:16AM -0500, Andrea Bolognani wrote:
> > > > You're right, that is undesirable. What about something like this?
> > > >
> > > > type GuestPanicInformation struct {
> > > > HyperV *GuestPanicInformationHyperV
> > > > S390 *GuestPanicInformationS390
> > > > }
> > > >
> > > > type jsonGuestPanicInformation struct {
> > > > Discriminator string `json:"type"`
> > > > HyperV *GuestPanicInformationHyperV `json:"hyper-v"`
> > > > S390 *GuestPanicInformationS390 `json:"s390"`
> > > > }
> > >
> > > It can possibly be even simpler with just embedding the real
> > > struct
> > >
> > > type jsonGuestPanicInformation struct {
> > > Discriminator string
> > > GuestPanicInformation
> > > }
>
> Similar to what I said in previous email (same thread) to Andrea,
> this would not work because the end result does not match with
> QAPI spec, where HyperV or S390 fields should be at the same
> level as 'type'.
>
> If we embed either HyperV or S390, then it should work, like:
>
> tmp := struct {
> GuestPanicInformationHyperV
> Discriminator string "type"
> }{}
>
> But I intend to try the json.RawMessage too as with description
> it seems like we can avoid looking the whole json data twice.
For the same reason, I could not use json.RawMessage, sadly.
As Andrea pointed out before, json.RawMessage is just an alias
for []byte. We need a field for that (so, it can't be embed) and
the contents of the JSON need to match that field's name.
I've removed the string manipulation and used a few anonymous
structs instead. I'm attaching a main.go program that tests this
behavior together with input checks that Andrea suggested. This
is more or less how the generated code will look like in the next
iteration but in case one want's to find a nicer/shorter
solution, I'm all ears :)
Cheers,
Victor
[-- Attachment #1.2: main.go --]
[-- Type: text/plain, Size: 3182 bytes --]
package main
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
type QCryptoBlockOptionsQCow struct {
KeySecret *string `json:"key-secret,omitempty"`
}
type QCryptoBlockOptionsLUKS struct {
KeySecret *string `json:"key-secret,omitempty"`
}
type QCryptoBlockOpenOptions struct {
// Variants fields
Qcow *QCryptoBlockOptionsQCow `json:"-"`
Luks *QCryptoBlockOptionsLUKS `json:"-"`
}
func (s QCryptoBlockOpenOptions) MarshalJSON() ([]byte, error) {
var bytes []byte
var err error
if s.Qcow != nil {
tmp := struct {
QCryptoBlockOptionsQCow
Discriminator string `json:"format"`
}{
QCryptoBlockOptionsQCow: *s.Qcow,
Discriminator: "qcow",
}
if bytes, err = json.Marshal(tmp); err != nil {
return nil, err
}
}
if s.Luks != nil {
if len(bytes) != 0 {
return nil, errors.New(`multiple fields set for QCryptoBlockOpenOptions`)
}
tmp := struct {
QCryptoBlockOptionsLUKS
Discriminator string `json:"format"`
}{
QCryptoBlockOptionsLUKS: *s.Luks,
Discriminator: "luks",
}
if bytes, err = json.Marshal(tmp); err != nil {
return nil, err
}
}
if len(bytes) == 0 {
return nil, errors.New(`null not supported for QCryptoBlockOpenOptions`)
}
return bytes, nil
}
func (s *QCryptoBlockOpenOptions) UnmarshalJSON(data []byte) error {
tmp := struct {
Discriminator string `json:"format"`
}{}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
switch tmp.Discriminator {
case "qcow":
s.Qcow = &QCryptoBlockOptionsQCow{}
if err := json.Unmarshal(data, s.Qcow); err != nil {
s.Qcow = nil
return err
}
case "luks":
s.Luks = &QCryptoBlockOptionsLUKS{}
if err := json.Unmarshal(data, s.Luks); err != nil {
s.Luks = nil
return err
}
}
return nil
}
func main() {
jsonLuks := `{"key-secret":"my luks secret is here","format":"luks"}`
jsonQcow := `{"key-secret":"my qcow secret is here","format":"qcow"}`
r := QCryptoBlockOpenOptions{}
if err := json.Unmarshal([]byte(jsonLuks), &r); err != nil {
panic(err)
} else if r.Luks == nil || r.Qcow != nil {
panic(fmt.Sprintf("Wrong: %v", r))
} else if b, err := json.Marshal(&r); err != nil {
panic(err)
} else if string(b) != jsonLuks {
panic(string(b))
}
r = QCryptoBlockOpenOptions{}
if err := json.Unmarshal([]byte(jsonQcow), &r); err != nil {
panic(err)
} else if r.Luks != nil || r.Qcow == nil {
panic(fmt.Sprintf("Wrong: %v", r))
} else if b, err := json.Marshal(&r); err != nil {
panic(err)
} else if string(b) != jsonQcow {
panic(string(b))
}
r = QCryptoBlockOpenOptions{}
if _, err := json.Marshal(&r); err == nil {
panic("No fields set should be an error")
} else if !strings.Contains(err.Error(), "null not supported") {
panic(err)
}
qcowSecret := "my-qcow-secret-is-here"
luksSecret := "my-luks-secret-is-here"
r = QCryptoBlockOpenOptions{
Qcow: &QCryptoBlockOptionsQCow{
KeySecret: &qcowSecret,
},
Luks: &QCryptoBlockOptionsLUKS{
KeySecret: &luksSecret,
},
}
if _, err := json.Marshal(&r); err == nil {
panic("multiple fields set should be an error")
} else if !strings.Contains(err.Error(), "multiple fields set for") {
panic(err)
}
}
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-08-19 7:28 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-17 12:19 [RFC PATCH v2 0/8] qapi: add generator for Golang interface Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 1/8] qapi: golang: Generate qapi's enum types in Go Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 2/8] qapi: golang: Generate qapi's alternate " Victor Toso
2022-07-05 15:45 ` Andrea Bolognani
2022-08-17 14:04 ` Victor Toso
2022-08-19 16:27 ` Andrea Bolognani
2022-08-22 6:59 ` Victor Toso
2022-08-29 11:27 ` Markus Armbruster
2022-08-29 13:31 ` Victor Toso
2022-09-02 14:49 ` Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 3/8] qapi: golang: Generate qapi's struct " Victor Toso
2022-06-17 14:41 ` Daniel P. Berrangé
2022-06-17 15:23 ` Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 4/8] qapi: golang: Generate qapi's union " Victor Toso
2022-07-05 15:45 ` Andrea Bolognani
2022-07-05 16:35 ` Daniel P. Berrangé
2022-07-06 9:28 ` Andrea Bolognani
2022-07-06 9:37 ` Daniel P. Berrangé
2022-07-06 9:48 ` Daniel P. Berrangé
2022-07-06 12:20 ` Andrea Bolognani
2022-08-17 16:25 ` Victor Toso
2022-08-19 7:20 ` Victor Toso [this message]
2022-08-19 15:25 ` Andrea Bolognani
2022-08-22 6:33 ` Victor Toso
2022-08-17 16:06 ` Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 5/8] qapi: golang: Generate qapi's event " Victor Toso
2022-07-05 15:45 ` Andrea Bolognani
2022-07-05 16:47 ` Daniel P. Berrangé
2022-07-06 14:53 ` Andrea Bolognani
2022-07-06 15:07 ` Daniel P. Berrangé
2022-07-06 16:22 ` Andrea Bolognani
2022-08-18 7:55 ` Victor Toso
2022-08-18 7:47 ` Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 6/8] qapi: golang: Generate qapi's command " Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 7/8] qapi: golang: Add CommandResult type to Go Victor Toso
2022-07-05 15:46 ` Andrea Bolognani
2022-07-05 16:49 ` Daniel P. Berrangé
2022-08-17 15:00 ` Victor Toso
2022-06-17 12:19 ` [RFC PATCH v2 8/8] qapi: golang: document skip function visit_array_types Victor Toso
2022-06-27 7:15 ` [RFC PATCH v2 0/8] qapi: add generator for Golang interface Markus Armbruster
2022-06-27 12:48 ` Victor Toso
2022-06-27 15:29 ` Markus Armbruster
2022-08-18 8:04 ` Victor Toso
2022-07-05 15:46 ` Andrea Bolognani
2022-08-17 14:24 ` Victor Toso
2022-08-29 11:53 ` Markus Armbruster
2022-08-29 14:05 ` Victor Toso
2024-11-07 10:43 ` Daniel P. Berrangé
2024-11-07 12:36 ` Markus Armbruster
2024-11-07 13:06 ` Daniel P. Berrangé
2024-11-07 13:35 ` Daniel P. Berrangé
2024-11-07 14:18 ` Markus Armbruster
2024-11-08 9:43 ` Victor Toso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220819072052.yl4gvepa5ectlvci@tapioca \
--to=victortoso@redhat.com \
--cc=abologna@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=eblake@redhat.com \
--cc=jsnow@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).