From 9e5847b66d7209c66539ef70a9adddb3ea397c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 7 Feb 2021 12:37:51 +0100 Subject: [PATCH] fix: json marshalling of custom time (resolve #117) --- models/shared.go | 5 +++++ models/user.go | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/models/shared.go b/models/shared.go index 2cde688..ecd6b94 100644 --- a/models/shared.go +++ b/models/shared.go @@ -2,6 +2,7 @@ package models import ( "database/sql/driver" + "encoding/json" "errors" "fmt" "gorm.io/gorm" @@ -31,6 +32,10 @@ type Interval struct { type CustomTime time.Time +func (j *CustomTime) MarshalJSON() ([]byte, error) { + return json.Marshal(j.String()) +} + func (j *CustomTime) UnmarshalJSON(b []byte) error { s := strings.Replace(strings.Trim(string(b), "\""), ".", "", 1) i, err := strconv.ParseInt(s, 10, 64) diff --git a/models/user.go b/models/user.go index 17e3dd3..81fee75 100644 --- a/models/user.go +++ b/models/user.go @@ -34,7 +34,7 @@ type CredentialsReset struct { type TimeByUser struct { User string - Time CustomTime `swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` + Time CustomTime } func (c *CredentialsReset) IsValid() bool {