-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8fff5c
commit 08514fd
Showing
9 changed files
with
92 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2014, Greg Roseberry | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package null | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
// Time is a nullable time.Time. | ||
// JSON marshals to the zero value for time.Time if null. | ||
// Considered to be null to SQL if zero. | ||
type Time struct { | ||
Time time.Time | ||
Valid bool | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
// It will encode null if this time is null. | ||
func (t Time) MarshalJSON() ([]byte, error) { | ||
if !t.Valid { | ||
return []byte("null"), nil | ||
} | ||
return t.Time.MarshalJSON() | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler. | ||
// It supports string, object (e.g. pq.NullTime and friends) | ||
// and null input. | ||
func (t *Time) UnmarshalJSON(data []byte) error { | ||
var err error | ||
var v interface{} | ||
if err = json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
switch v.(type) { | ||
case string: | ||
err = t.Time.UnmarshalJSON(data) | ||
case nil: | ||
t.Valid = false | ||
return nil | ||
default: | ||
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name()) | ||
} | ||
t.Valid = err == nil | ||
return err | ||
} | ||
|
||
// ValueOrZero returns the inner value if valid, otherwise zero. | ||
func (t Time) ValueOrZero() time.Time { | ||
if !t.Valid { | ||
return time.Time{} | ||
} | ||
return t.Time | ||
} | ||
|
||
// IsZero returns true for null or zero Times, for | ||
// potential future omitempty support. | ||
func (t Time) IsZero() bool { | ||
return !t.Valid || t.Time.IsZero() | ||
} |