Csaba Henk
2020-Feb-19 13:30 UTC
[Libguestfs] [PATCH] golang: make API idiomatic so that functions return (<val>, error)
Go API functions returned (<val>, *GuestfsError) that made
code like this fail to build:
n, err := os.Stdin.Read(buf)
if err != nil {
log.Fatal(err)
}
n, err = g.Pwrite_device(dev, buf[:n], off)
...
As err should be of error (interface) type as of the stdlib call,
and should be of *GuestfsError type as of the libguestfs call.
The concrete error value that libguestfs functions return can be
a *GuestfsError, but the function signature should have (<val>, error)
as return value.
---
generator/golang.ml | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/generator/golang.ml b/generator/golang.ml
index bd09ae9cf..e11967d57 100644
--- a/generator/golang.ml
+++ b/generator/golang.ml
@@ -114,6 +114,11 @@ func (e *GuestfsError) String() string {
}
}
+/* Implement the error interface */
+func (e *GuestfsError) Error() string {
+ return e.String()
+}
+
func get_error_from_handle (g *Guestfs, op string) *GuestfsError {
// NB: DO NOT try to free c_errmsg!
c_errmsg := C.guestfs_last_error (g.g)
@@ -322,24 +327,24 @@ func return_hashtable (argv **C.char) map[string]string {
(* Return type. *)
let noreturn match ret with
- | RErr -> pr " *GuestfsError"; ""
- | RInt _ -> pr " (int, *GuestfsError)"; "0, "
- | RInt64 _ -> pr " (int64, *GuestfsError)"; "0,
"
- | RBool _ -> pr " (bool, *GuestfsError)"; "false,
"
+ | RErr -> pr " error"; ""
+ | RInt _ -> pr " (int, error)"; "0, "
+ | RInt64 _ -> pr " (int64, error)"; "0, "
+ | RBool _ -> pr " (bool, error)"; "false, "
| RConstString _
- | RString _ -> pr " (string, *GuestfsError)";
"\"\", "
- | RConstOptString _ -> pr " (*string, *GuestfsError)";
"nil, "
- | RStringList _ -> pr " ([]string, *GuestfsError)";
"nil, "
+ | RString _ -> pr " (string, error)";
"\"\", "
+ | RConstOptString _ -> pr " (*string, error)"; "nil,
"
+ | RStringList _ -> pr " ([]string, error)"; "nil,
"
| RStruct (_, sn) ->
let sn = camel_name_of_struct sn in
- pr " (*%s, *GuestfsError)" sn;
+ pr " (*%s, error)" sn;
sprintf "&%s{}, " sn
| RStructList (_, sn) ->
let sn = camel_name_of_struct sn in
- pr " (*[]%s, *GuestfsError)" sn;
+ pr " (*[]%s, error)" sn;
"nil, "
- | RHashtable _ -> pr " (map[string]string,
*GuestfsError)"; "nil, "
- | RBufferOut _ -> pr " ([]byte, *GuestfsError)";
"nil, " in
+ | RHashtable _ -> pr " (map[string]string, error)";
"nil, "
+ | RBufferOut _ -> pr " ([]byte, error)"; "nil, "
in
(* Body of the function. *)
pr " {\n";
--
2.25.1
Richard W.M. Jones
2020-Feb-19 18:26 UTC
Re: [Libguestfs] [PATCH] golang: make API idiomatic so that functions return (<val>, error)
On Wed, Feb 19, 2020 at 02:30:45PM +0100, Csaba Henk wrote:> Go API functions returned (<val>, *GuestfsError) that made > code like this fail to build: > > n, err := os.Stdin.Read(buf) > if err != nil { > log.Fatal(err) > } > n, err = g.Pwrite_device(dev, buf[:n], off) > ... > > As err should be of error (interface) type as of the stdlib call, > and should be of *GuestfsError type as of the libguestfs call. > > The concrete error value that libguestfs functions return can be > a *GuestfsError, but the function signature should have (<val>, error) > as return value.Thanks - pushed. The bindtests fail with a rather obscure error. I don't know what it means or if it's related to this patch. ============================================= libguestfs 1.41.8: golang/test-suite.log ============================================= # TOTAL: 2 # PASS: 1 # SKIP: 0 # XFAIL: 0 # FAIL: 1 # XPASS: 0 # ERROR: 0 .. contents:: :depth: 2 FAIL: run-bindtests ================== panic: runtime error: cgo argument has Go pointer to Go pointer goroutine 1 [running]: libguestfs.org/guestfs.(*Guestfs).Internal_test.func7(0xc000010028, 0x107a430, 0x1078a20, 0xc000010050, 0xc000000000, 0x0, 0x0, 0x1079ec0, 0x1075190, 0x10751b0, ...) /home/rjones/d/libguestfs/golang/src/libguestfs.org/guestfs/guestfs.go:6667 +0xf7 libguestfs.org/guestfs.(*Guestfs).Internal_test(0xc000010028, 0x4f3f43, 0x3, 0xc000063b90, 0xc000063aa0, 0x0, 0x0, 0x0, 0x0, 0x0, ...) /home/rjones/d/libguestfs/golang/src/libguestfs.org/guestfs/guestfs.go:6667 +0x3cc main.main() /home/rjones/d/libguestfs/golang/bindtests.go:77 +0x127d exit status 2 FAIL run-bindtests (exit status: 1) Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Maybe Matching Threads
- [PATCH libnbd] Add outline framework for Go language bindings (golang).
- golang: panic: runtime error: cgo argument has Go pointer to Go pointer
- More idiomatic way of doing this
- More idiomatic way of writing a filter
- looking for more idiomatic way of doing this