Skip to content

Commit

Permalink
checking nil or zero data
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Oct 2, 2023
1 parent 90dd1d9 commit 8d7a072
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 76 deletions.
28 changes: 5 additions & 23 deletions field/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,49 +145,31 @@ func (f *Binary) Unmarshal(v interface{}) error {
}

func (f *Binary) Marshal(v interface{}) error {
if v == nil || reflect.ValueOf(v).IsZero() {
f.value = nil
return nil
}

switch v := v.(type) {
case *Binary:
if v == nil {
return nil
}
f.value = v.value
case string:
if v == "" {
f.value = nil
return nil
}

buf, err := hex.DecodeString(v)
if err != nil {
return fmt.Errorf("failed to convert string to byte: %w", err)
}

f.value = buf
case *string:
if v == nil {
f.value = nil
return nil
}

buf, err := hex.DecodeString(*v)
if err != nil {
return fmt.Errorf("failed to convert string to byte: %w", err)
}

f.value = buf
case []byte:
if v == nil || len(v) == 0 {
f.value = nil
return nil
}

f.SetBytes(v)
case *[]byte:
if v == nil {
f.value = nil
return nil
}

f.SetBytes(*v)
default:
return fmt.Errorf("data does not match required *Binary or (string, *string, []byte, *[]byte) type")
Expand Down
3 changes: 3 additions & 0 deletions field/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func TestBinaryFieldMarshal(t *testing.T) {
require.Equal(t, testValue, str.Value())

err = str.Marshal(nil)
require.NoError(t, err)

err = str.Marshal(123456)
require.Error(t, err)
require.Equal(t, "data does not match required *Binary or (string, *string, []byte, *[]byte) type", err.Error())
}
27 changes: 5 additions & 22 deletions field/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,38 +158,21 @@ func (f *Hex) Unmarshal(v interface{}) error {
}

func (f *Hex) Marshal(v interface{}) error {
if v == nil || reflect.ValueOf(v).IsZero() {
f.value = ""
return nil
}

switch v := v.(type) {
case *Hex:
if v == nil {
return nil
}
f.value = v.value
case string:
if v == "" {
f.value = ""
return nil
}

f.value = v
case *string:
if v == nil {
f.value = ""
return nil
}

f.value = *v
case []byte:
if v == nil || len(v) == 0 {
f.value = ""
return nil
}

f.SetBytes(v)
case *[]byte:
if v == nil {
f.value = ""
return nil
}
f.SetBytes(*v)
default:
return fmt.Errorf("data does not match required *Hex or (string, *string, []byte, *[]byte) type")
Expand Down
3 changes: 3 additions & 0 deletions field/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func TestHexFieldMarshal(t *testing.T) {
require.Equal(t, testValue, buf)

err = str.Marshal(nil)
require.NoError(t, err)

err = str.Marshal(123456)
require.Error(t, err)
require.Equal(t, "data does not match required *Hex or (string, *string, []byte, *[]byte) type", err.Error())
}
26 changes: 7 additions & 19 deletions field/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,26 @@ func (f *Numeric) Unmarshal(v interface{}) error {
return nil
}

func (f *Numeric) Marshal(data interface{}) error {
switch v := data.(type) {
func (f *Numeric) Marshal(v interface{}) error {
if v == nil || reflect.ValueOf(v).IsZero() {
f.value = 0
return nil
}

switch v := v.(type) {
case *Numeric:
if v == nil {
f.value = 0
return nil
}
f.value = v.value
case int:
f.value = v
case *int:
if v == nil {
f.value = 0
return nil
}
f.value = *v
case string:
if v == "" {
f.value = 0
return nil
}
val, err := strconv.Atoi(v)
if err != nil {
return utils.NewSafeError(err, "failed to convert sting value into number")
}
f.value = val
case *string:
if v == nil {
f.value = 0
return nil
}

val, err := strconv.Atoi(*v)
if err != nil {
return utils.NewSafeError(err, "failed to convert sting value into number")
Expand Down
3 changes: 3 additions & 0 deletions field/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ func TestNumericFieldMarshal(t *testing.T) {
require.Equal(t, 123456, vNumeric.Value())

err := str.Marshal(nil)
require.NoError(t, err)

err = str.Marshal([]byte("123456"))
require.Error(t, err)
require.Equal(t, "data does not match require *Numeric or (int, *int, string, *string) type", err.Error())
}
Expand Down
17 changes: 5 additions & 12 deletions field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,17 @@ func (f *String) Unmarshal(v interface{}) error {
}

func (f *String) Marshal(v interface{}) error {
if v == nil || reflect.ValueOf(v).IsZero() {
f.value = ""
return nil
}

switch v := v.(type) {
case *String:
if v == nil {
f.value = ""
return nil
}
f.value = v.value
case string:
if v == "" {
f.value = ""
return nil
}
f.value = v
case *string:
if v == nil {
f.value = ""
return nil
}
f.value = *v
case int:
f.value = strconv.FormatInt(int64(v), 10)
Expand Down
3 changes: 3 additions & 0 deletions field/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func TestStringFieldMarshal(t *testing.T) {
require.Equal(t, "123456", str.Value())

err := str.Marshal(nil)
require.NoError(t, err)

err = str.Marshal([]byte("123456"))
require.Error(t, err)
require.Equal(t, "data does not match required *String or (string, *string, int, *int) type", err.Error())
}
Expand Down

0 comments on commit 8d7a072

Please sign in to comment.