Skip to content

Commit

Permalink
add default value for id
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 11, 2017
1 parent 5433679 commit d6f985c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions todoist/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (i ID) MarshalJSON() ([]byte, error) {
if IsTempID(i) {
s = `"` + s + `"`
}
if s == "0" {
s = "null"
}
return []byte(s), nil
}

Expand All @@ -42,6 +45,9 @@ func (i *ID) UnmarshalJSON(b []byte) (err error) {
if err != nil {
s = string(b) // integer id
}
if s == "null" {
s = "0"
}
id, err := NewID(s)
if err != nil {
return err
Expand Down
15 changes: 14 additions & 1 deletion todoist/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func TestID_MarshalJSON(t *testing.T) {
if err != nil || string(b) != strconv.Quote(test.s) {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}

b, err = ID("0").MarshalJSON()
if err != nil || string(b) != "null" {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}
}

func TestID_UnmarshalJSON(t *testing.T) {
Expand All @@ -61,9 +66,17 @@ func TestID_UnmarshalJSON(t *testing.T) {
if !reflect.DeepEqual(err, test.e) {
t.Errorf("Expect %s, but got %s", test.e, err)
} else if test.e == nil && v != test.v {

t.Errorf("Expect %s, but got %s", test.v, v)
}
}
var v ID
err := v.UnmarshalJSON([]byte("null"))
if err != nil {
t.Errorf("Unexpect error: %s", err)
}
if v != ID("0") {
t.Errorf("Expect %s, but got %s", ID("0"), v)
}
}

func TestIsTempID(t *testing.T) {
Expand Down

0 comments on commit d6f985c

Please sign in to comment.