Skip to content

Commit

Permalink
empty value unmarshalling fix (#35)
Browse files Browse the repository at this point in the history
* data source links applied, type renaming

* phone parser

* unmarshalling empty value fix

* empty value unmarshalling fix

---------

Co-authored-by: ivan-lemiashonak <ivan.lemiashonak@finteqhub.com>
  • Loading branch information
ilemiashonak and ilemiashonak authored Jun 20, 2023
1 parent b53c3cb commit 5b9c66d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
6 changes: 4 additions & 2 deletions timezone/timezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func (name *Timezone) UnmarshalJSON(data []byte) error {
return err
}

if _, err := ByNameStrErr(str); err != nil {
return err
if len(str) != 0 {
if _, err := ByNameStrErr(str); err != nil {
return err
}
}

*name = Timezone(str)
Expand Down
30 changes: 28 additions & 2 deletions timezone/timezone_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package timezone

import "testing"
import (
"encoding/json"
"fmt"
"testing"
)

func TestTimezone_MappingIsCorrect(t *testing.T) {
for key, timezone := range timezonesByName {
Expand Down Expand Up @@ -33,7 +37,6 @@ func TestTimezone_Validate(t *testing.T) {
}

func TestTimezone_Lookup(t *testing.T) {

if _, ok := ByNameStr("a"); ok {
t.FailNow()
}
Expand All @@ -50,3 +53,26 @@ func TestTimezone_Lookup(t *testing.T) {
t.FailNow()
}
}

var testCases = []struct {
value Timezone
expectingUnmarshalError bool
}{
{value: "", expectingUnmarshalError: false},
{value: "Europe/Minsk", expectingUnmarshalError: false},
{value: "a", expectingUnmarshalError: true},
}

func Test_UnmarshalJSON(t *testing.T) {
for _, testCase := range testCases {
jsonStr := fmt.Sprintf(`{"timezone":"%s"}`, testCase.value)
var timezoneStruct struct {
Timezone Timezone `json:"timezone"`
}

actualErr := json.Unmarshal([]byte(jsonStr), &timezoneStruct)
if (actualErr == nil) == testCase.expectingUnmarshalError {
t.Errorf(`JsonUnmarshal: '%s'. expecting error - '%v' but was opposite`, testCase.value, testCase.expectingUnmarshalError)
}
}
}

0 comments on commit 5b9c66d

Please sign in to comment.