mirror of https://github.com/jackc/pgx.git
Return error and make sure they are unit tested
parent
42d3d00734
commit
5424d3c873
|
@ -105,19 +105,23 @@ func (ts *Timestamp) UnmarshalJSON(b []byte) error {
|
||||||
case "-infinity":
|
case "-infinity":
|
||||||
*ts = Timestamp{Valid: true, InfinityModifier: -Infinity}
|
*ts = Timestamp{Valid: true, InfinityModifier: -Infinity}
|
||||||
default:
|
default:
|
||||||
|
// Parse time with or without timezonr
|
||||||
tss := *s
|
tss := *s
|
||||||
// PostgreSQL uses ISO 8601 without timezone for to_json function and casting from a string to timestampt
|
// PostgreSQL uses ISO 8601 without timezone for to_json function and casting from a string to timestampt
|
||||||
|
|
||||||
tim, err := time.Parse(time.RFC3339Nano, tss)
|
tim, err := time.Parse(time.RFC3339Nano, tss)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
*ts = Timestamp{Time: tim, Valid: true}
|
*ts = Timestamp{Time: tim, Valid: true}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
tim, err = time.ParseInLocation(jsonISO8601, tss, time.UTC)
|
tim, err = time.ParseInLocation(jsonISO8601, tss, time.UTC)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
*ts = Timestamp{Time: tim, Valid: true}
|
*ts = Timestamp{Time: tim, Valid: true}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
ts.Valid = false
|
||||||
|
return fmt.Errorf("cannot unmarshal %s to timestamp with layout %s or %s (%w)",
|
||||||
|
*s, time.RFC3339Nano, jsonISO8601, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,18 @@ func TestTimestampMarshalJSON(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTimestampUnmarshalJSONErrors(t *testing.T) {
|
||||||
|
tsStruct := struct {
|
||||||
|
TS pgtype.Timestamp `json:"ts"`
|
||||||
|
}{}
|
||||||
|
goodJson1 := []byte(`{"ts":"2012-03-29T10:05:45"}`)
|
||||||
|
assert.NoError(t, json.Unmarshal(goodJson1, &tsStruct))
|
||||||
|
goodJson2 := []byte(`{"ts":"2012-03-29T10:05:45Z"}`)
|
||||||
|
assert.NoError(t, json.Unmarshal(goodJson2, &tsStruct))
|
||||||
|
badJson := []byte(`{"ts":"2012-03-29"}`)
|
||||||
|
assert.Error(t, json.Unmarshal(badJson, &tsStruct))
|
||||||
|
}
|
||||||
|
|
||||||
func TestTimestampUnmarshalJSON(t *testing.T) {
|
func TestTimestampUnmarshalJSON(t *testing.T) {
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source string
|
source string
|
||||||
|
|
Loading…
Reference in New Issue