diff mbox series

[v3,07/22] golang/xenlight: define Mac builtin type

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

Commit Message

Nick Rosbrook Dec. 10, 2019, 3:47 p.m. UTC
From: Nick Rosbrook <rosbrookn@ainfosec.com>

Define Mac as [6]byte and implement fromC, toC, and String functions.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
---
Changes in v2:
- Fix the format string in String function to use %02x.
- Use a value reciever for the toC function.
Changes in v3:
- Iterate over the indirect of mac instead of creating
  a slice from the C type.
---
 tools/golang/xenlight/xenlight.go | 33 +++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

Comments

George Dunlap Dec. 16, 2019, 5:15 p.m. UTC | #1
On 12/10/19 3:47 PM, Nick Rosbrook wrote:
> From: Nick Rosbrook <rosbrookn@ainfosec.com>
> 
> Define Mac as [6]byte and implement fromC, toC, and String functions.
> 
> Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
> Reviewed-by: George Dunlap <george.dunlap@citrix.com>
> ---
> Changes in v2:
> - Fix the format string in String function to use %02x.
> - Use a value reciever for the toC function.
> Changes in v3:
> - Iterate over the indirect of mac instead of creating
>   a slice from the C type.
> ---
>  tools/golang/xenlight/xenlight.go | 33 +++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+)
> 
> diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
> index 72afc3cf14..17d146771e 100644
> --- a/tools/golang/xenlight/xenlight.go
> +++ b/tools/golang/xenlight/xenlight.go
> @@ -181,6 +181,39 @@ func (d *Defbool) toC() (C.libxl_defbool, error) {
>  	return c, nil
>  }
>  
> +// Mac represents a libxl_mac, or simply a MAC address.
> +type Mac [6]byte
> +
> +// String formats a Mac address to string representation.
> +func (mac Mac) String() string {
> +	s := "%02x:%02x:%02x:%02x:%02x:%02x"
> +	opts := make([]interface{}, 6)
> +
> +	for i, v := range mac {
> +		opts[i] = v
> +	}
> +
> +	return fmt.Sprintf(s, opts...)
> +}
> +
> +func (mac *Mac) fromC(cmac *C.libxl_mac) error {
> +	for i := range *mac {
> +		mac[i] = byte(cmac[i])
> +	}

That's much better, thanks.

 -George
diff mbox series

Patch

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index 72afc3cf14..17d146771e 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -181,6 +181,39 @@  func (d *Defbool) toC() (C.libxl_defbool, error) {
 	return c, nil
 }
 
+// Mac represents a libxl_mac, or simply a MAC address.
+type Mac [6]byte
+
+// String formats a Mac address to string representation.
+func (mac Mac) String() string {
+	s := "%02x:%02x:%02x:%02x:%02x:%02x"
+	opts := make([]interface{}, 6)
+
+	for i, v := range mac {
+		opts[i] = v
+	}
+
+	return fmt.Sprintf(s, opts...)
+}
+
+func (mac *Mac) fromC(cmac *C.libxl_mac) error {
+	for i := range *mac {
+		mac[i] = byte(cmac[i])
+	}
+
+	return nil
+}
+
+func (mac Mac) toC() (C.libxl_mac, error) {
+	var cmac C.libxl_mac
+
+	for i, v := range mac {
+		cmac[i] = C.uint8_t(v)
+	}
+
+	return cmac, nil
+}
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream