Remove use of unsafe where not necessary
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 51b4704..26a42d1 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
@@ -334,10 +333,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
}