From 62da169c991ab88d1dcf54b25194393b1f9b2eca Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 5 Oct 2023 10:09:34 -0500 Subject: [PATCH] code refactoring, fix error --- field/composite.go | 6 +----- field/string.go | 2 +- message.go | 6 +----- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/field/composite.go b/field/composite.go index 6ec6eba..28df3ce 100644 --- a/field/composite.go +++ b/field/composite.go @@ -206,14 +206,10 @@ func (f *Composite) Unmarshal(v interface{}) error { return fmt.Errorf("failed to get data from field %s: %w", indexOrTag, err) } default: // Native types - // using new reflect value that has pointer instead of input data field - dummy := reflect.New(dataField.Type()).Elem() - err = messageField.Unmarshal(dummy) + err = messageField.Unmarshal(dataField) if err != nil { return fmt.Errorf("failed to get data from field %s: %w", indexOrTag, err) } - - dataField.Set(dummy) } } diff --git a/field/string.go b/field/string.go index 2e88199..b528e49 100644 --- a/field/string.go +++ b/field/string.go @@ -153,7 +153,7 @@ func (f *String) Unmarshal(v interface{}) error { } func (f *String) Marshal(v interface{}) error { - if v == nil || reflect.ValueOf(v).IsZero() { + if v == nil || (!reflect.ValueOf(v).CanInt() && reflect.ValueOf(v).IsZero()) { f.value = "" return nil } diff --git a/message.go b/message.go index 2823c59..17cbd74 100644 --- a/message.go +++ b/message.go @@ -513,14 +513,10 @@ func (m *Message) Unmarshal(v interface{}) error { return fmt.Errorf("failed to get value from field %d: %w", fieldIndex, err) } default: // Native types - // using new reflect value that has pointer instead of input data field - dummy := reflect.New(dataField.Type()).Elem() - err = messageField.Unmarshal(dummy) + err = messageField.Unmarshal(dataField) if err != nil { return fmt.Errorf("failed to get value from field %d: %w", fieldIndex, err) } - - dataField.Set(dummy) } }