Merge pull request #220 from jessevdk/#218/no-unsafe
Remove unnecessary use of unsafe
diff --git a/command.go b/command.go
index 2662843..486bacb 100644
--- a/command.go
+++ b/command.go
@@ -5,7 +5,6 @@
"sort"
"strconv"
"strings"
- "unsafe"
)
// Command represents an application command. Commands can be added to the
@@ -229,7 +228,17 @@
subcommand := mtag.Get("command")
if len(subcommand) != 0 {
- ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr()))
+ var ptrval reflect.Value
+
+ if realval.Kind() == reflect.Ptr {
+ ptrval = realval
+
+ if ptrval.IsNil() {
+ ptrval.Set(reflect.New(ptrval.Type().Elem()))
+ }
+ } else {
+ ptrval = realval.Addr()
+ }
shortDescription := mtag.Get("description")
longDescription := mtag.Get("long-description")
@@ -237,6 +246,7 @@
aliases := mtag.GetMany("alias")
subc, err := c.AddCommand(subcommand, shortDescription, longDescription, ptrval.Interface())
+
if err != nil {
return true, err
}
diff --git a/group.go b/group.go
index 6133a71..9e057ab 100644
--- a/group.go
+++ b/group.go
@@ -9,7 +9,6 @@
"reflect"
"strings"
"unicode/utf8"
- "unsafe"
)
// ErrNotPointerToStruct indicates that a provided data container is not
@@ -338,10 +337,22 @@
subgroup := mtag.Get("group")
if len(subgroup) != 0 {
- ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr()))
+ var ptrval reflect.Value
+
+ if realval.Kind() == reflect.Ptr {
+ ptrval = realval
+
+ if ptrval.IsNil() {
+ ptrval.Set(reflect.New(ptrval.Type()))
+ }
+ } else {
+ ptrval = realval.Addr()
+ }
+
description := mtag.Get("description")
group, err := g.AddGroup(subgroup, description, ptrval.Interface())
+
if err != nil {
return true, err
}
diff --git a/termsize.go b/termsize.go
index df97e7e..1ca6a85 100644
--- a/termsize.go
+++ b/termsize.go
@@ -1,4 +1,4 @@
-// +build !windows,!plan9,!solaris
+// +build !windows,!plan9,!solaris,!appengine
package flags
diff --git a/termsize_nosysioctl.go b/termsize_nosysioctl.go
index 2a9bbe0..3d5385b 100644
--- a/termsize_nosysioctl.go
+++ b/termsize_nosysioctl.go
@@ -1,4 +1,4 @@
-// +build windows plan9 solaris
+// +build windows plan9 solaris appengine
package flags
diff --git a/termsize_unix.go b/tiocgwinsz_bsdish.go
similarity index 100%
rename from termsize_unix.go
rename to tiocgwinsz_bsdish.go
diff --git a/termsize_linux.go b/tiocgwinsz_linux.go
similarity index 100%
rename from termsize_linux.go
rename to tiocgwinsz_linux.go
diff --git a/termsize_other.go b/tiocgwinsz_other.go
similarity index 100%
rename from termsize_other.go
rename to tiocgwinsz_other.go