This commit is contained in:
Matthew Dillon 2015-10-02 16:20:07 -07:00
parent a880fdea82
commit efb0cc13fa
41 changed files with 569 additions and 386 deletions

View file

@ -1,5 +1,6 @@
package types
// Claims represent an authenticated user's session.
type Claims struct {
Name string
Iss string

View file

@ -1,5 +1,6 @@
package types
// Entity is a a payload or model.
type Entity interface {
Marshal() ([]byte, error)
}

View file

@ -2,10 +2,13 @@ package types
import "encoding/json"
// ErrorJSON is an error that serializes to a JSON-encoded representation of the
// error message.
type ErrorJSON struct {
Err error
}
// Error satisfies the necessary interface to make ErrorJSON an error.
func (ej ErrorJSON) Error() string {
e, _ := json.Marshal(struct {
Err string `json:"error"`
@ -15,6 +18,7 @@ func (ej ErrorJSON) Error() string {
return string(e)
}
// AppError returns an error plus an HTTP status code.
type AppError struct {
Error error
Status int

View file

@ -6,17 +6,20 @@ import (
"encoding/json"
)
// NullBool wraps sql.NullBool so that the JSON serialization can be overridden.
type NullBool struct {
sql.NullBool
}
func (b *NullBool) MarshalJSON() ([]byte, error) {
if !b.Valid {
// MarshalJSON makes NullBool a json.Marshaller.
func (n *NullBool) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte("null"), nil
}
return json.Marshal(b.Bool)
return json.Marshal(n.Bool)
}
// UnmarshalJSON makes NullBool a json.Unmarshaller.
func (n *NullBool) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
n.Bool = false

View file

@ -6,10 +6,12 @@ import (
"encoding/json"
)
// NullFloat64 wraps sql.NullBool so that the JSON serialization can be overridden.
type NullFloat64 struct {
sql.NullFloat64
}
// MarshalJSON makes NullFloat64 a json.Marshaller.
func (f *NullFloat64) MarshalJSON() ([]byte, error) {
if !f.Valid {
return []byte("null"), nil
@ -17,6 +19,7 @@ func (f *NullFloat64) MarshalJSON() ([]byte, error) {
return json.Marshal(f.Float64)
}
// UnmarshalJSON makes NullFloat64 a json.Unmarshaller.
func (f *NullFloat64) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
f.Float64 = 0

View file

@ -6,10 +6,12 @@ import (
"encoding/json"
)
//NullInt64 wraps sql.NullInt64 so that the JSON serialization can be overridden.
type NullInt64 struct {
sql.NullInt64
}
// MarshalJSON makes NullInt64 a json.Marshaller.
func (i *NullInt64) MarshalJSON() ([]byte, error) {
if !i.Valid {
return []byte("null"), nil
@ -17,6 +19,7 @@ func (i *NullInt64) MarshalJSON() ([]byte, error) {
return json.Marshal(i.Int64)
}
// UnmarshalJSON makes NullInt64 a json.Unmarshaller.
func (i *NullInt64) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
i.Int64 = 0

View file

@ -7,12 +7,14 @@ import (
"github.com/thermokarst/bactdb/errors"
)
// NullSliceInt64 allows bactdb to read Postgres array types.
type NullSliceInt64 []int64
// Scan makes NullSliceInt64 a sql.Scanner.
func (i *NullSliceInt64) Scan(src interface{}) error {
asBytes, ok := src.([]byte)
if !ok {
return errors.SourceNotByteSlice
return errors.ErrSourceNotByteSlice
}
asString := string(asBytes)
(*i) = strToIntSlice(asString)

View file

@ -6,10 +6,12 @@ import (
"encoding/json"
)
// NullString wraps sql.NullString so that the JSON serialization can be overridden.
type NullString struct {
sql.NullString
}
// MarshalJSON makes NullString a json.Marshaller.
func (s *NullString) MarshalJSON() ([]byte, error) {
if !s.Valid {
return []byte("null"), nil
@ -17,6 +19,7 @@ func (s *NullString) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String)
}
// UnmarshalJSON makes NullString a json.Unmarshaller.
func (s *NullString) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
s.String = ""

View file

@ -8,10 +8,12 @@ import (
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/lib/pq"
)
// NullTime wraps pq.NullTime so that the JSON serialization can be overridden.
type NullTime struct {
pq.NullTime
}
// MarshalJSON makes NullTime a json.Marshaller.
func (t *NullTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
@ -19,6 +21,7 @@ func (t *NullTime) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Time)
}
// UnmarshalJSON makes NullTime a json.Unmarshaller.
func (t *NullTime) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, []byte("null")) {
var nt time.Time