Initialize custom marshaler pointer as needed
Fixes #216.
diff --git a/convert.go b/convert.go
index 938c3ac..984aac8 100644
--- a/convert.go
+++ b/convert.go
@@ -154,6 +154,13 @@
func convertUnmarshal(val string, retval reflect.Value) (bool, error) {
if retval.Type().NumMethod() > 0 && retval.CanInterface() {
if unmarshaler, ok := retval.Interface().(Unmarshaler); ok {
+ if retval.IsNil() {
+ retval.Set(reflect.New(retval.Type().Elem()))
+
+ // Re-assign from the new value
+ unmarshaler = retval.Interface().(Unmarshaler)
+ }
+
return true, unmarshaler.UnmarshalFlag(val)
}
}
diff --git a/pointer_test.go b/pointer_test.go
index c5ef60a..dc779c7 100644
--- a/pointer_test.go
+++ b/pointer_test.go
@@ -62,6 +62,64 @@
}
}
+type marshalledString string
+
+func (m *marshalledString) UnmarshalFlag(value string) error {
+ *m = marshalledString(value)
+ return nil
+}
+
+func (m marshalledString) MarshalFlag() (string, error) {
+ return string(m), nil
+}
+
+func TestPointerStringMarshalled(t *testing.T) {
+ var opts = struct {
+ Value *marshalledString `short:"v"`
+ }{}
+
+ ret := assertParseSuccess(t, &opts, "-v", "value")
+
+ assertStringArray(t, ret, []string{})
+
+ if opts.Value == nil {
+ t.Error("Expected value not to be nil")
+ return
+ }
+
+ assertString(t, string(*opts.Value), "value")
+}
+
+type marshalledStruct struct {
+ Value string
+}
+
+func (m *marshalledStruct) UnmarshalFlag(value string) error {
+ m.Value = value
+ return nil
+}
+
+func (m marshalledStruct) MarshalFlag() (string, error) {
+ return m.Value, nil
+}
+
+func TestPointerStructMarshalled(t *testing.T) {
+ var opts = struct {
+ Value *marshalledStruct `short:"v"`
+ }{}
+
+ ret := assertParseSuccess(t, &opts, "-v", "value")
+
+ assertStringArray(t, ret, []string{})
+
+ if opts.Value == nil {
+ t.Error("Expected value not to be nil")
+ return
+ }
+
+ assertString(t, opts.Value.Value, "value")
+}
+
type PointerGroup struct {
Value bool `short:"v"`
}