-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom NullTime. upgrade go -> 1.18 (#16)
Co-authored-by: alexey.sergey <alexey.sergey@softswiss.com>
- Loading branch information
Showing
7 changed files
with
330 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package time | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"fmt" | ||
"gopkg.in/guregu/null.v4" | ||
"time" | ||
) | ||
|
||
type Time null.Time | ||
|
||
func NullTime(t time.Time) Time { | ||
return Time(null.TimeFrom(t)) | ||
} | ||
|
||
func (t *Time) UnmarshalJSON(data []byte) error { | ||
var str string | ||
err := json.Unmarshal(data, &str) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return t.parseTimeFromString(str) | ||
} | ||
|
||
func (t Time) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(t.format()) | ||
} | ||
|
||
func (t *Time) UnmarshalXML(decoder *xml.Decoder, element xml.StartElement) error { | ||
var str string | ||
err := decoder.DecodeElement(&str, &element) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return t.parseTimeFromString(str) | ||
} | ||
|
||
func (t Time) MarshalXML(decoder *xml.Encoder, element xml.StartElement) error { | ||
return decoder.EncodeElement(t.format(), element) | ||
} | ||
|
||
func (t Time) String() string { | ||
var str = t.format() | ||
if str != nil { | ||
return *str | ||
} | ||
return "" | ||
} | ||
|
||
func (t Time) format() *string { | ||
if t.Valid { | ||
var str = t.Time.Format(time.RFC3339) | ||
return &str | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *Time) parseTimeFromString(value string) (err error) { | ||
if value == "" { | ||
return nil | ||
} | ||
|
||
var newTime time.Time | ||
if newTime, err = time.Parse(time.RFC3339, value); err != nil { | ||
if errTime, ok := err.(*time.ParseError); ok { | ||
return fmt.Errorf("cannot parse time '%s' invalid format '%s'", errTime.Value, errTime.Layout) | ||
} | ||
return err | ||
} | ||
|
||
*t = NullTime(newTime) | ||
return nil | ||
} |
Oops, something went wrong.