Skip to content

Commit f876c69

Browse files
Refactoring Date type
1 parent 4438eff commit f876c69

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

values-date.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package openbd
22

33
import (
4-
"fmt"
4+
"strconv"
55
"strings"
66
"time"
77

@@ -34,7 +34,10 @@ var timeTemplate3 = []string{
3434

3535
//UnmarshalJSON returns result of Unmarshal for json.Unmarshal()
3636
func (t *Date) UnmarshalJSON(b []byte) error {
37-
s := strings.Trim(string(b), "\"")
37+
s := string(b)
38+
if ss, err := strconv.Unquote(s); err == nil {
39+
s = ss
40+
}
3841
if len(s) == 0 || strings.ToLower(s) == "null" {
3942
*t = Date{time.Time{}}
4043
return nil
@@ -44,44 +47,41 @@ func (t *Date) UnmarshalJSON(b []byte) error {
4447
if strings.Contains(s, ":") {
4548
for _, tmplt := range timeTemplate1 {
4649
if tm, err := time.Parse(tmplt, s); err != nil {
47-
lastErr = err
50+
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
4851
} else {
4952
*t = Date{tm}
5053
return nil
5154
}
5255
}
53-
return errs.Wrap(lastErr, "error in Date.UnmarshalJSON() function")
56+
return lastErr
5457
}
5558
for _, tmplt := range timeTemplate2 {
5659
if tm, err := time.Parse(tmplt, s); err != nil {
57-
lastErr = err
60+
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
5861
} else {
5962
*t = Date{tm}
6063
return nil
6164
}
6265
}
63-
return errs.Wrap(lastErr, "error in Date.UnmarshalJSON() function")
66+
return lastErr
6467
}
6568
for _, tmplt := range timeTemplate3 {
6669
if tm, err := time.Parse(tmplt, s); err != nil {
67-
lastErr = err
70+
lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt))
6871
} else {
6972
*t = Date{tm}
7073
return nil
7174
}
7275
}
73-
return errs.Wrap(lastErr, "error in Date.UnmarshalJSON() function")
76+
return lastErr
7477
}
7578

7679
//MarshalJSON returns time string with RFC3339 format
7780
func (t *Date) MarshalJSON() ([]byte, error) {
78-
if t == nil {
79-
return []byte("\"\""), nil
80-
}
81-
if t.IsZero() {
81+
if t == nil || t.IsZero() {
8282
return []byte("\"\""), nil
8383
}
84-
return []byte(fmt.Sprintf("\"%v\"", t)), nil
84+
return []byte(strconv.Quote(t.String())), nil
8585
}
8686

8787
func (t Date) String() string {

values-date_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ func TestUnmarshal(t *testing.T) {
5151
}
5252

5353
func TestUnmarshalErr(t *testing.T) {
54-
data := `{"date_taken": "2005/03/26"}`
55-
tst := &ForTestStruct{}
56-
if err := json.Unmarshal([]byte(data), tst); err == nil {
57-
t.Error("Unmarshal() error = nil, not want nil.")
58-
} else {
59-
fmt.Printf("Info: %+v\n", err)
54+
testCases := []string{
55+
`{"date_taken": "2005/03/26"}`,
56+
`{"date_taken": "2005-03-26 00:00"}`,
57+
`{"date_taken": "2005-03-26+0900"}`,
58+
}
59+
for _, tcStr := range testCases {
60+
tst := &ForTestStruct{}
61+
if err := json.Unmarshal([]byte(tcStr), tst); err == nil {
62+
t.Error("Unmarshal() error = nil, not want nil.")
63+
} else {
64+
fmt.Printf("Info: %+v\n", err)
65+
}
6066
}
6167
}
6268

0 commit comments

Comments
 (0)