[Libguestfs] [libnbd PATCH 2/7] golang: Export meta-context constants

Eric Blake eblake at redhat.com
Wed Jul 26 17:29:45 UTC 2023


Go insists that a module's members are not public unless they start
with a capital letter.  Our attempt at defining constants
'namespace_base' and 'context_base_allocation' were thus only visible
to our in-package tests, and not for external clients.

Although this patch does not completely address gofmt complaints,
adding some strategic grouping comments inside the const() block has
an additional benefit of reducing the amount of whitespace that gofmt
wants to inject into unrelated lines.

The change to generated golang/bindigs.go is:

| --- golang/bindings.go.bak	2023-07-26 12:06:47.618324437 -0500
| +++ golang/bindings.go	2023-07-26 12:06:55.521385295 -0500
| @@ -111,14 +111,18 @@
|      READ_DATA uint32 = 1
|      READ_HOLE uint32 = 2
|      READ_ERROR uint32 = 3
| -    namespace_base = "base:"
| -    context_base_allocation = "base:allocation"
| +    /* Meta-context namespace "base" */
| +    NAMESPACE_BASE = "base:"
| +    CONTEXT_BASE_ALLOCATION = "base:allocation"
| +    /* Defined bits in "base:allocation" */
|      STATE_HOLE uint32 = 1
|      STATE_ZERO uint32 = 2
| -    namespace_qemu = "qemu:"
| -    context_qemu_dirty_bitmap = "qemu:dirty-bitmap:"
| +    /* Meta-context namespace "qemu" */
| +    NAMESPACE_QEMU = "qemu:"
| +    CONTEXT_QEMU_DIRTY_BITMAP = "qemu:dirty-bitmap:"
| +    /* Defined bits in "qemu:dirty-bitmap:" */
|      STATE_DIRTY uint32 = 1
| -    context_qemu_allocation_depth = "qemu:allocation-depth"
| +    CONTEXT_QEMU_ALLOCATION_DEPTH = "qemu:allocation-depth"
|  )
|
|  /* SetDebug: set or clear the debug flag */

Signed-off-by: Eric Blake <eblake at redhat.com>

---

RFC: Do we want MixedCase rather than ALL_CAPS names?  If so, this
patch needs to be rethought a bit, as it would impact API for existing
public constants as well as the ones changed here.  But we don't
promise API stability for Go.
---
 generator/GoLang.ml                           | 10 +++++---
 golang/libnbd_230_opt_info_test.go            | 24 +++++++++----------
 golang/libnbd_240_opt_list_meta_test.go       | 14 +++++------
 .../libnbd_245_opt_list_meta_queries_test.go  |  4 ++--
 golang/libnbd_250_opt_set_meta_test.go        | 20 ++++++++--------
 .../libnbd_255_opt_set_meta_queries_test.go   | 12 +++++-----
 golang/libnbd_460_block_status_test.go        |  4 ++--
 7 files changed, 46 insertions(+), 42 deletions(-)

diff --git a/generator/GoLang.ml b/generator/GoLang.ml
index 7a7e7f4b..82d73ed6 100644
--- a/generator/GoLang.ml
+++ b/generator/GoLang.ml
@@ -469,11 +469,15 @@ let
   ) constants;
   List.iter (
     fun (ns, ctxts) ->
-      pr "    namespace_%s = \"%s:\"\n" ns ns;
+      let ns_upper = String.uppercase_ascii ns in
+      pr "    /* Meta-context namespace \"%s\" */\n" ns;
+      pr "    NAMESPACE_%s = \"%s:\"\n" ns_upper ns;
       List.iter (
         fun (ctxt, consts) ->
-          let ctxt_macro = macro_name ctxt in
-          pr "    context_%s_%s = \"%s:%s\"\n" ns ctxt_macro ns ctxt;
+          let ctxt_macro = String.uppercase_ascii (macro_name ctxt) in
+          pr "    CONTEXT_%s_%s = \"%s:%s\"\n" ns_upper ctxt_macro ns ctxt;
+          if consts <> [] then
+            pr "    /* Defined bits in \"%s:%s\" */\n" ns ctxt;
           List.iter (fun (n, v) ->
               pr "    %s uint32 = %d\n" n v
           ) consts
diff --git a/golang/libnbd_230_opt_info_test.go b/golang/libnbd_230_opt_info_test.go
index cd00be9e..cc7c2d91 100644
--- a/golang/libnbd_230_opt_info_test.go
+++ b/golang/libnbd_230_opt_info_test.go
@@ -45,7 +45,7 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("could not connect: %s", err)
 	}

-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not add meta context: %s", err)
 	}
@@ -59,7 +59,7 @@ func Test230OptInfo(t *testing.T) {
 	if err == nil {
 		t.Fatalf("expected error")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -83,7 +83,7 @@ func Test230OptInfo(t *testing.T) {
 	if !ro {
 		t.Fatalf("unexpected readonly")
 	}
-	meta, err := h.CanMetaContext(context_base_allocation)
+	meta, err := h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta failed unexpectedly: %s", err)
 	}
@@ -104,7 +104,7 @@ func Test230OptInfo(t *testing.T) {
 	if err == nil {
 		t.Fatalf("expected error")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -151,7 +151,7 @@ func Test230OptInfo(t *testing.T) {
 	if ro {
 		t.Fatalf("unexpected readonly")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -177,7 +177,7 @@ func Test230OptInfo(t *testing.T) {
 	if err == nil {
 		t.Fatalf("expected error")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -205,7 +205,7 @@ func Test230OptInfo(t *testing.T) {
 	if !ro {
 		t.Fatalf("unexpected readonly")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta failed unexpectedly: %s", err)
 	}
@@ -236,7 +236,7 @@ func Test230OptInfo(t *testing.T) {
 	if size != 4 {
 		t.Fatalf("unexpected size")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta failed unexpectedly: %s", err)
 	}
@@ -270,7 +270,7 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("could not add meta context: %s", err)
 	}

-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -278,7 +278,7 @@ func Test230OptInfo(t *testing.T) {
 	if err != nil {
 		t.Fatalf("opt_info failed unexpectedly: %s", err)
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta failed unexpectedly: %s", err)
 	}
@@ -290,7 +290,7 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("set request meta context failed unexpectedly: %s", err)
 	}
 	/* Adding to the request list now won't matter */
-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not add meta context: %s", err)
 	}
@@ -298,7 +298,7 @@ func Test230OptInfo(t *testing.T) {
 	if err != nil {
 		t.Fatalf("opt_go failed unexpectedly: %s", err)
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta failed unexpectedly: %s", err)
 	}
diff --git a/golang/libnbd_240_opt_list_meta_test.go b/golang/libnbd_240_opt_list_meta_test.go
index 0235fe3f..011b5704 100644
--- a/golang/libnbd_240_opt_list_meta_test.go
+++ b/golang/libnbd_240_opt_list_meta_test.go
@@ -31,7 +31,7 @@ func listmetaf(user_data int, name string) int {
 		panic("expected user_data == 42")
 	}
 	list_count++
-	if (name == context_base_allocation) {
+	if (name == CONTEXT_BASE_ALLOCATION) {
 		list_seen = true
 	}
 	return 0
@@ -92,7 +92,7 @@ func Test240OptListMeta(t *testing.T) {
 	/* Third pass: specific query should have one match. */
 	list_count = 0
 	list_seen = false
-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
@@ -107,7 +107,7 @@ func Test240OptListMeta(t *testing.T) {
 	if err != nil {
 		t.Fatalf("could not request get_meta_context: %s", err)
 	}
-	if *tmp != context_base_allocation {
+	if *tmp != CONTEXT_BASE_ALLOCATION {
 		t.Fatalf("wrong result of get_meta_context: %s", *tmp)
 	}
 	r, err = h.OptListMetaContext(func(name string) int {
@@ -129,7 +129,7 @@ func Test240OptListMeta(t *testing.T) {
 	if err == nil {
 		t.Fatalf("expected error")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -144,7 +144,7 @@ func Test240OptListMeta(t *testing.T) {
         if size != 1048576 {
 		t.Fatalf("get_size gave wrong size")
         }
-	meta, err := h.CanMetaContext(context_base_allocation)
+	meta, err := h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta_context failed unexpectedly: %s", err)
 	}
@@ -175,7 +175,7 @@ func Test240OptListMeta(t *testing.T) {
         if size != 1048576 {
 		t.Fatalf("get_size gave wrong size")
         }
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("can_meta_context failed unexpectedly: %s", err)
 	}
@@ -187,7 +187,7 @@ func Test240OptListMeta(t *testing.T) {
 	/* Final pass: "base:" query should get at least "base:allocation" */
 	list_count = 0
 	list_seen = false
-	err = h.AddMetaContext("base:")
+	err = h.AddMetaContext(NAMESPACE_BASE)
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
diff --git a/golang/libnbd_245_opt_list_meta_queries_test.go b/golang/libnbd_245_opt_list_meta_queries_test.go
index fc1ab60c..3ae2d854 100644
--- a/golang/libnbd_245_opt_list_meta_queries_test.go
+++ b/golang/libnbd_245_opt_list_meta_queries_test.go
@@ -28,7 +28,7 @@ func listmetaqf(user_data int, name string) int {
 		panic("expected user_data == 42")
 	}
 	listq_count++
-	if (name == context_base_allocation) {
+	if (name == CONTEXT_BASE_ALLOCATION) {
 		listq_seen = true
 	}
 	return 0
@@ -97,7 +97,7 @@ func(name string) int {
 	listq_count = 0
 	listq_seen = false
 	r, err = h.OptListMetaContextQueries([]string{
-		"x-nosuch:", context_base_allocation },
+		"x-nosuch:", CONTEXT_BASE_ALLOCATION },
 		func(name string) int {
 	        return listmetaqf(42, name)
 		})
diff --git a/golang/libnbd_250_opt_set_meta_test.go b/golang/libnbd_250_opt_set_meta_test.go
index 6d27fa22..4263ede5 100644
--- a/golang/libnbd_250_opt_set_meta_test.go
+++ b/golang/libnbd_250_opt_set_meta_test.go
@@ -28,7 +28,7 @@ func setmetaf(user_data int, name string) int {
 		panic("expected user_data == 42")
 	}
 	set_count++
-	if (name == context_base_allocation) {
+	if (name == CONTEXT_BASE_ALLOCATION) {
 		set_seen = true
 	}
 	return 0
@@ -67,18 +67,18 @@ func Test250OptSetMeta(t *testing.T) {
 	if sr {
 		t.Fatalf("unexpected structured replies state")
 	}
-	meta, err := h.CanMetaContext(context_base_allocation)
+	meta, err := h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
 	if meta {
 		t.Fatalf("unexpected can meta context state")
 	}
-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -109,7 +109,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if !sr {
 		t.Fatalf("unexpected structured replies state")
 	}
-	_, err = h.CanMetaContext(context_base_allocation)
+	_, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err == nil {
 		t.Fatalf("expected error")
 	}
@@ -151,7 +151,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if r != set_count || r != 0 || set_seen {
 		t.Fatalf("unexpected set_count after opt_set_meta_context")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
@@ -166,7 +166,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
@@ -183,7 +183,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if r != 1 || r != set_count || !set_seen {
 		t.Fatalf("unexpected set_count after opt_set_meta_context")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
@@ -204,7 +204,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if err != nil {
 		t.Fatalf("could not request opt_go: %s", err)
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
@@ -224,7 +224,7 @@ func Test250OptSetMeta(t *testing.T) {
 	if set_count != 0 || set_seen {
 		t.Fatalf("unexpected set_count after opt_set_meta_context")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
diff --git a/golang/libnbd_255_opt_set_meta_queries_test.go b/golang/libnbd_255_opt_set_meta_queries_test.go
index cca25ae7..232560bc 100644
--- a/golang/libnbd_255_opt_set_meta_queries_test.go
+++ b/golang/libnbd_255_opt_set_meta_queries_test.go
@@ -28,7 +28,7 @@ func setmetaqf(user_data int, name string) int {
 		panic("expected user_data == 42")
 	}
 	setq_count++
-	if (name == context_base_allocation) {
+	if (name == CONTEXT_BASE_ALLOCATION) {
 		setq_seen = true
 	}
 	return 0
@@ -74,7 +74,7 @@ func(name string) int {
 	 */
 	setq_count = 0
 	setq_seen = false
-	err = h.AddMetaContext(context_base_allocation)
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not request add_meta_context: %s", err)
 	}
@@ -87,7 +87,7 @@ func(name string) int {
 	if r != setq_count || r != 0 || setq_seen {
 		t.Fatalf("unexpected set_count after opt_set_meta_context_queries")
 	}
-	meta, err := h.CanMetaContext(context_base_allocation)
+	meta, err := h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
@@ -99,7 +99,7 @@ func(name string) int {
 	setq_count = 0
 	setq_seen = false
 	r, err = h.OptSetMetaContextQueries([]string{
-		"x-nosuch:context", context_base_allocation},
+		"x-nosuch:context", CONTEXT_BASE_ALLOCATION},
 		func(name string) int {
 	        return setmetaqf(42, name)
 		})
@@ -109,7 +109,7 @@ func(name string) int {
 	if r != 1 || r != setq_count || !setq_seen {
 		t.Fatalf("unexpected set_count after opt_set_meta_context_queries")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
@@ -126,7 +126,7 @@ func(name string) int {
 	if err != nil {
 		t.Fatalf("could not request opt_go: %s", err)
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
+	meta, err = h.CanMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("could not check can meta context: %s", err)
 	}
diff --git a/golang/libnbd_460_block_status_test.go b/golang/libnbd_460_block_status_test.go
index dca0d1b8..d2d11e9c 100644
--- a/golang/libnbd_460_block_status_test.go
+++ b/golang/libnbd_460_block_status_test.go
@@ -31,7 +31,7 @@ func mcf(metacontext string, offset uint64, e []uint32, error *int) int {
 	if *error != 0 {
 		panic("expected *error == 0")
 	}
-	if metacontext == "base:allocation" {
+	if metacontext == CONTEXT_BASE_ALLOCATION {
 		entries = e
 	}
 	return 0
@@ -69,7 +69,7 @@ func Test460BlockStatus(t *testing.T) {
 	}
 	defer h.Close()

-	err = h.AddMetaContext("base:allocation")
+	err = h.AddMetaContext(CONTEXT_BASE_ALLOCATION)
 	if err != nil {
 		t.Fatalf("%s", err)
 	}
-- 
2.41.0



More information about the Libguestfs mailing list