2
0

Add MarshalJSON to a few types

This commit is contained in:
Jack Christensen
2017-04-13 21:54:04 -05:00
parent adb54d06ce
commit fe7d9d3462
6 changed files with 58 additions and 0 deletions
+13
View File
@@ -195,3 +195,16 @@ func (src Int2) Value() (driver.Value, error) {
return nil, errUndefined
}
}
func (src Int2) MarshalJSON() ([]byte, error) {
switch src.Status {
case Present:
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
case Null:
return []byte("null"), nil
case Undefined:
return []byte("undefined"), nil
}
return nil, errBadStatus
}
+13
View File
@@ -186,3 +186,16 @@ func (src Int4) Value() (driver.Value, error) {
return nil, errUndefined
}
}
func (src Int4) MarshalJSON() ([]byte, error) {
switch src.Status {
case Present:
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
case Null:
return []byte("null"), nil
case Undefined:
return []byte("undefined"), nil
}
return nil, errBadStatus
}
+13
View File
@@ -172,3 +172,16 @@ func (src Int8) Value() (driver.Value, error) {
return nil, errUndefined
}
}
func (src Int8) MarshalJSON() ([]byte, error) {
switch src.Status {
case Present:
return []byte(strconv.FormatInt(src.Int, 10)), nil
case Null:
return []byte("null"), nil
case Undefined:
return []byte("undefined"), nil
}
return nil, errBadStatus
}
+1
View File
@@ -129,6 +129,7 @@ type TextEncoder interface {
}
var errUndefined = errors.New("cannot encode status undefined")
var errBadStatus = errors.New("invalid status")
type DataType struct {
Value Value
+14
View File
@@ -2,6 +2,7 @@ package pgtype
import (
"database/sql/driver"
"encoding/json"
"fmt"
"io"
)
@@ -134,3 +135,16 @@ func (src Text) Value() (driver.Value, error) {
return nil, errUndefined
}
}
func (src Text) MarshalJSON() ([]byte, error) {
switch src.Status {
case Present:
return json.Marshal(src.String)
case Null:
return []byte("null"), nil
case Undefined:
return []byte("undefined"), nil
}
return nil, errBadStatus
}
+4
View File
@@ -49,3 +49,7 @@ func (dst *Varchar) Scan(src interface{}) error {
func (src Varchar) Value() (driver.Value, error) {
return (Text)(src).Value()
}
func (src Varchar) MarshalJSON() ([]byte, error) {
return (Text)(src).MarshalJSON()
}