Skip to content
Snippets Groups Projects
Commit 63123b95 authored by Niall Sheridan's avatar Niall Sheridan
Browse files

override the json marshaller's time format

parent 8b63d262
No related branches found
No related tags found
No related merge requests found
package store package store
import ( import (
"encoding/json"
"time" "time"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
...@@ -43,6 +44,20 @@ type CertRecord struct { ...@@ -43,6 +44,20 @@ type CertRecord struct {
Raw string `json:"-" db:"raw_key"` Raw string `json:"-" db:"raw_key"`
} }
func (c *CertRecord) MarshalJSON() ([]byte, error) {
type Alias CertRecord
f := "2006-01-02 15:04:05 -0700"
return json.Marshal(&struct {
*Alias
CreatedAt string `json:"created_at"`
Expires string `json:"expires"`
}{
Alias: (*Alias)(c),
CreatedAt: c.CreatedAt.Format(f),
Expires: c.Expires.Format(f),
})
}
func parseTime(t uint64) time.Time { func parseTime(t uint64) time.Time {
return time.Unix(int64(t), 0) return time.Unix(int64(t), 0)
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"database/sql" "database/sql"
"encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"os/user" "os/user"
...@@ -160,3 +161,20 @@ func TestSQLiteStore(t *testing.T) { ...@@ -160,3 +161,20 @@ func TestSQLiteStore(t *testing.T) {
} }
testStore(t, db) testStore(t, db)
} }
func TestMarshalCert(t *testing.T) {
a := assert.New(t)
c := &CertRecord{
KeyID: "id",
Principals: []string{"user"},
CreatedAt: time.Date(2017, time.April, 10, 13, 0, 0, 0, time.UTC),
Expires: time.Date(2017, time.April, 11, 10, 0, 0, 0, time.UTC),
Raw: "ABCDEF",
}
b, err := json.Marshal(c)
if err != nil {
t.Error(err)
}
want := `{"key_id":"id","principals":["user"],"revoked":false,"created_at":"2017-04-10 13:00:00 +0000","expires":"2017-04-11 10:00:00 +0000"}`
a.JSONEq(want, string(b))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment