From d0e5f62cf27d3e5c81385342c63d9f42c2eb7e2f Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Mon, 6 Aug 2018 00:25:26 +0100 Subject: Move StringSlice into the store package --- server/store/store.go | 15 +++++++-------- server/store/store_test.go | 3 +-- server/store/string_slice.go | 37 +++++++++++++++++++++++++++++++++++++ server/store/types/string_slice.go | 37 ------------------------------------- 4 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 server/store/string_slice.go delete mode 100644 server/store/types/string_slice.go diff --git a/server/store/store.go b/server/store/store.go index 4863ff0..b620e6d 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -9,7 +9,6 @@ import ( "github.com/nsheridan/cashier/lib" "github.com/nsheridan/cashier/server/config" - "github.com/nsheridan/cashier/server/store/types" ) // New returns a new configured database. @@ -37,12 +36,12 @@ type CertStorer interface { // A CertRecord is a representation of a ssh certificate used by a CertStorer. type CertRecord struct { - KeyID string `json:"key_id" db:"key_id"` - Principals types.StringSlice `json:"principals" db:"principals"` - CreatedAt time.Time `json:"created_at" db:"created_at"` - Expires time.Time `json:"expires" db:"expires_at"` - Revoked bool `json:"revoked" db:"revoked"` - Raw string `json:"-" db:"raw_key"` + KeyID string `json:"key_id" db:"key_id"` + Principals StringSlice `json:"principals" db:"principals"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + Expires time.Time `json:"expires" db:"expires_at"` + Revoked bool `json:"revoked" db:"revoked"` + Raw string `json:"-" db:"raw_key"` } // MarshalJSON implements the json.Marshaler interface for the CreatedAt and @@ -69,7 +68,7 @@ func parseTime(t uint64) time.Time { func parseCertificate(cert *ssh.Certificate) *CertRecord { return &CertRecord{ KeyID: cert.KeyId, - Principals: types.StringSlice(cert.ValidPrincipals), + Principals: StringSlice(cert.ValidPrincipals), CreatedAt: parseTime(cert.ValidAfter), Expires: parseTime(cert.ValidBefore), Raw: string(lib.GetPublicKey(cert)), diff --git a/server/store/store_test.go b/server/store/store_test.go index d9ae325..3fd900c 100644 --- a/server/store/store_test.go +++ b/server/store/store_test.go @@ -10,7 +10,6 @@ import ( "testing" "time" - "github.com/nsheridan/cashier/server/store/types" "github.com/nsheridan/cashier/testdata" "github.com/stretchr/testify/assert" @@ -24,7 +23,7 @@ func TestParseCertificate(t *testing.T) { pub, _ := ssh.NewPublicKey(r.Public()) c := &ssh.Certificate{ KeyId: "id", - ValidPrincipals: types.StringSlice{"principal"}, + ValidPrincipals: StringSlice{"principal"}, ValidBefore: now, CertType: ssh.UserCert, Key: pub, diff --git a/server/store/string_slice.go b/server/store/string_slice.go new file mode 100644 index 0000000..ac86360 --- /dev/null +++ b/server/store/string_slice.go @@ -0,0 +1,37 @@ +package store + +import ( + "database/sql/driver" + "encoding/json" +) + +// StringSlice is a []string which will be stored in a database as a JSON array. +type StringSlice []string + +var _ driver.Valuer = (*StringSlice)(nil) + +// Value implements the driver.Valuer interface, marshalling the raw value to +// a JSON array. +func (s StringSlice) Value() (driver.Value, error) { + v, err := json.Marshal(s) + if err != nil { + return nil, err + } + return string(v), err +} + +// Scan implements the sql.Scanner interface, unmarshalling the value coming +// off the wire and storing the result in the StringSlice. +func (s *StringSlice) Scan(value interface{}) error { + if value == nil { + s = &StringSlice{} + return nil + } + var err error + if v, err := driver.String.ConvertValue(value); err == nil { + if v, ok := v.([]byte); ok { + err = json.Unmarshal(v, s) + } + } + return err +} diff --git a/server/store/types/string_slice.go b/server/store/types/string_slice.go deleted file mode 100644 index 81b38c3..0000000 --- a/server/store/types/string_slice.go +++ /dev/null @@ -1,37 +0,0 @@ -package types - -import ( - "database/sql/driver" - "encoding/json" -) - -// StringSlice is a []string which will be stored in a database as a JSON array. -type StringSlice []string - -var _ driver.Valuer = (*StringSlice)(nil) - -// Value implements the driver.Valuer interface, marshalling the raw value to -// a JSON array. -func (s StringSlice) Value() (driver.Value, error) { - v, err := json.Marshal(s) - if err != nil { - return nil, err - } - return string(v), err -} - -// Scan implements the sql.Scanner interface, unmarshalling the value coming -// off the wire and storing the result in the StringSlice. -func (s *StringSlice) Scan(value interface{}) error { - if value == nil { - s = &StringSlice{} - return nil - } - var err error - if v, err := driver.String.ConvertValue(value); err == nil { - if v, ok := v.([]byte); ok { - err = json.Unmarshal(v, s) - } - } - return err -} -- cgit v1.2.3