[Libguestfs] [libnbd PATCH 2/3] golang: Typesafe returns for REnum/RFlags

Richard W.M. Jones rjones at redhat.com
Mon Sep 7 14:14:19 UTC 2020


On Sat, Sep 05, 2020 at 08:40:59PM -0500, Eric Blake wrote:
> Now that we have a distinct branch in the generator when returning an
> enum or bitmask that cannot fail, we can use it in Golang for
> symmetry, so that the result of a get function can be plugged into a
> set function without a trip through other types.
> ---
>  generator/GoLang.ml                                  | 12 ++++++------
>  .../libnbd/libnbd_110_defaults_test.go               |  6 +++---
>  .../libnbd/libnbd_120_set_non_defaults_test.go       |  2 +-
>  3 files changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/generator/GoLang.ml b/generator/GoLang.ml
> index 732b81f..aecd2d5 100644
> --- a/generator/GoLang.ml
> +++ b/generator/GoLang.ml
> @@ -99,8 +99,8 @@ let go_ret_type = function
>     * always nil unless h is closed
>     *)
>    | RUInt -> Some "uint"
> -  | REnum (_) -> Some "uint" (* XXX return typed constant instead? *)
> -  | RFlags (_) -> Some "uint" (* XXX return typed constant instead? *)
> +  | REnum ({ enum_prefix}) -> Some (camel_case enum_prefix)
> +  | RFlags ({ flag_prefix}) -> Some (camel_case flag_prefix)
> 
>  let go_ret_error = function
>    | RErr -> None
> @@ -393,10 +393,10 @@ let print_binding (name, { args; optargs; ret; shortdesc }) =
>        pr "    return &r, nil\n"
>     | RUInt ->
>        pr "    return uint (ret), nil\n"
> -   | REnum (_) ->
> -      pr "    return uint (ret), nil\n" (* XXX Proper enum type? *)
> -   | RFlags (_) ->
> -      pr "    return uint (ret), nil\n" (* XXX Proper bitmask type? *)
> +   | REnum ({enum_prefix}) ->
> +      pr "    return %s (ret), nil\n" (camel_case enum_prefix)
> +   | RFlags ({flag_prefix}) ->
> +      pr "    return %s (ret), nil\n" (camel_case flag_prefix)
>    );
>    pr "}\n";
>    pr "\n"
> diff --git a/golang/src/libguestfs.org/libnbd/libnbd_110_defaults_test.go b/golang/src/libguestfs.org/libnbd/libnbd_110_defaults_test.go
> index 7b4ea0a..005289b 100644
> --- a/golang/src/libguestfs.org/libnbd/libnbd_110_defaults_test.go
> +++ b/golang/src/libguestfs.org/libnbd/libnbd_110_defaults_test.go
> @@ -47,7 +47,7 @@ func Test110Defaults(t *testing.T) {
>  	if err != nil {
>  		t.Fatalf("could not get tls state: %s", err)
>  	}
> -	if tls != uint(TLS_DISABLE) {
> +	if tls != TLS_DISABLE {
>  		t.Fatalf("unexpected tls state")
>  	}
> 
> @@ -63,8 +63,8 @@ func Test110Defaults(t *testing.T) {
>  	if err != nil {
>  		t.Fatalf("could not get handshake flags: %s", err)
>  	}
> -	if flags != uint(HANDSHAKE_FLAG_FIXED_NEWSTYLE |
> -			 HANDSHAKE_FLAG_NO_ZEROES) {
> +	if flags != (HANDSHAKE_FLAG_FIXED_NEWSTYLE |
> +		     HANDSHAKE_FLAG_NO_ZEROES) {
>  		t.Fatalf("unexpected handshake flags")
>  	}
> 
> diff --git a/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go b/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
> index 331304e..685604e 100644
> --- a/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
> +++ b/golang/src/libguestfs.org/libnbd/libnbd_120_set_non_defaults_test.go
> @@ -64,7 +64,7 @@ func Test120SetNonDefaults(t *testing.T) {
>  		if err != nil {
>  			t.Fatalf("could not get tls state: %s", err)
>  		}
> -		if tls != uint(TLS_ALLOW) {
> +		if tls != TLS_ALLOW {
>  			t.Fatalf("unexpected tls state")
>  		}
>  	}

Seems reasonable.  I suppose the test case would fail if it
was wrong, so ACK.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v




More information about the Libguestfs mailing list