diff mbox series

[v2,11/22] golang/xenlight: re-factor Uuid type implementation

Message ID 1869a722c33ba47ab86c1acec399de3c2f3e58e3.1573840474.git.rosbrookn@ainfosec.com (mailing list archive)
State Superseded
Headers show
Series generated Go libxl bindings using IDL | expand

Commit Message

Nick Rosbrook Nov. 15, 2019, 7:44 p.m. UTC
From: Nick Rosbrook <rosbrookn@ainfosec.com>

Re-define Uuid as [16]byte and implement fromC, toC, and String functions.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
---
 tools/golang/xenlight/xenlight.go | 37 +++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

Comments

George Dunlap Dec. 4, 2019, 5:02 p.m. UTC | #1
On 11/15/19 7:44 PM, Nick Rosbrook wrote:
> From: Nick Rosbrook <rosbrookn@ainfosec.com>
> 
> Re-define Uuid as [16]byte and implement fromC, toC, and String functions.
> 
> Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
> ---
>  tools/golang/xenlight/xenlight.go | 37 +++++++++++++++++++++++++++++--
>  1 file changed, 35 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
> index 6f0a9278ad..67c1bb1225 100644
> --- a/tools/golang/xenlight/xenlight.go
> +++ b/tools/golang/xenlight/xenlight.go
> @@ -86,7 +86,40 @@ type Devid int
>  
>  type MemKB uint64
>  
> -type Uuid C.libxl_uuid
> +// Uuid is a domain UUID.
> +type Uuid [16]byte
> +
> +// String formats a Uuid in the form "xxxx-xx-xx-xx-xxxxxx".
> +func (u Uuid) String() string {
> +	s := "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x"
> +	opts := make([]interface{}, 16)
> +
> +	for i, v := range u {
> +		opts[i] = v
> +	}
> +
> +	return fmt.Sprintf(s, opts...)
> +}
> +
> +func (u *Uuid) fromC(c *C.libxl_uuid) error {
> +	b := (*[16]C.uint8_t)(unsafe.Pointer(&c.uuid[0]))
> +
> +	for i, v := range b {
> +		u[i] = byte(v)
> +	}

Same thing here.  (Actually I c&p'd the wrong code in my previous reply;
but you get the idea.)

Everything else looks good.

 -George
diff mbox series

Patch

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index 6f0a9278ad..67c1bb1225 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -86,7 +86,40 @@  type Devid int
 
 type MemKB uint64
 
-type Uuid C.libxl_uuid
+// Uuid is a domain UUID.
+type Uuid [16]byte
+
+// String formats a Uuid in the form "xxxx-xx-xx-xx-xxxxxx".
+func (u Uuid) String() string {
+	s := "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x"
+	opts := make([]interface{}, 16)
+
+	for i, v := range u {
+		opts[i] = v
+	}
+
+	return fmt.Sprintf(s, opts...)
+}
+
+func (u *Uuid) fromC(c *C.libxl_uuid) error {
+	b := (*[16]C.uint8_t)(unsafe.Pointer(&c.uuid[0]))
+
+	for i, v := range b {
+		u[i] = byte(v)
+	}
+
+	return nil
+}
+
+func (u *Uuid) toC() (C.libxl_uuid, error) {
+	var c C.libxl_uuid
+
+	for i, v := range u {
+		c.uuid[i] = C.uint8_t(v)
+	}
+
+	return c, nil
+}
 
 // defboolVal represents a defbool value.
 type defboolVal int
@@ -499,7 +532,7 @@  type Dominfo struct {
 func (cdi *C.libxl_dominfo) toGo() (di *Dominfo) {
 
 	di = &Dominfo{}
-	di.Uuid = Uuid(cdi.uuid)
+	di.Uuid.fromC(&cdi.uuid)
 	di.Domid = Domid(cdi.domid)
 	di.Ssidref = uint32(cdi.ssidref)
 	di.SsidLabel = C.GoString(cdi.ssid_label)