]> xenbits.xensource.com Git - people/liuw/xen.git/commitdiff
golang/xenlight: define Defbool builtin type
authorNick Rosbrook <rosbrookn@ainfosec.com>
Mon, 16 Dec 2019 18:07:59 +0000 (18:07 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 16 Dec 2019 18:07:59 +0000 (18:07 +0000)
Define Defbool as struct analagous to the C type, and define the type
'defboolVal' that represent true, false, and default defbool values.

Implement Set, Unset, SetIfDefault, IsDefault, Val, and String functions
on Defbool so that the type can be used in Go analagously to how its
used in C.

Finally, implement fromC and toC functions.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
tools/golang/xenlight/xenlight.go

index 89ed439fd0e7615ab056e122cb4b4f617e915f5a..640d82f35f12c227e30dc7c9ab9385c17407c134 100644 (file)
@@ -85,6 +85,99 @@ type MemKB uint64
 
 type Uuid C.libxl_uuid
 
+// defboolVal represents a defbool value.
+type defboolVal int
+
+const (
+       defboolDefault defboolVal = 0
+       defboolFalse   defboolVal = -1
+       defboolTrue    defboolVal = 1
+)
+
+// Defbool represents a libxl_defbool.
+type Defbool struct {
+       val defboolVal
+}
+
+func (d Defbool) String() string {
+       switch d.val {
+       case defboolDefault:
+               return "<default>"
+       case defboolFalse:
+               return "False"
+       case defboolTrue:
+               return "True"
+       }
+
+       return ""
+}
+
+// Set sets the value of the Defbool.
+func (d *Defbool) Set(b bool) {
+       if b {
+               d.val = defboolTrue
+               return
+       }
+       d.val = defboolFalse
+}
+
+// Unset resets the Defbool to default value.
+func (d *Defbool) Unset() {
+       d.val = defboolDefault
+}
+
+// SetIfDefault sets the value of Defbool only if
+// its current value is default.
+func (d *Defbool) SetIfDefault(b bool) {
+       if d.IsDefault() {
+               d.Set(b)
+       }
+}
+
+// IsDefault returns true if the value of Defbool
+// is default, returns false otherwise.
+func (d *Defbool) IsDefault() bool {
+       return d.val == defboolDefault
+}
+
+// Val returns the boolean value associated with the
+// Defbool value. An error is returned if the value
+// is default.
+func (d *Defbool) Val() (bool, error) {
+       if d.IsDefault() {
+               return false, fmt.Errorf("%v: cannot take value of default defbool", ErrorInval)
+       }
+
+       return (d.val > 0), nil
+}
+
+func (d *Defbool) fromC(c *C.libxl_defbool) error {
+       if C.libxl_defbool_is_default(*c) {
+               d.val = defboolDefault
+               return nil
+       }
+
+       if C.libxl_defbool_val(*c) {
+               d.val = defboolTrue
+               return nil
+       }
+
+       d.val = defboolFalse
+
+       return nil
+}
+
+func (d *Defbool) toC() (C.libxl_defbool, error) {
+       var c C.libxl_defbool
+
+       if !d.IsDefault() {
+               val, _ := d.Val()
+               C.libxl_defbool_set(&c, C.bool(val))
+       }
+
+       return c, nil
+}
+
 type Context struct {
        ctx    *C.libxl_ctx
        logger *C.xentoollog_logger_stdiostream