2
0

Add UnmarshalJSON to generated ints

This commit is contained in:
Jack Christensen
2022-01-01 11:25:26 -06:00
parent 0403c34ae3
commit d2cf33ed40
4 changed files with 115 additions and 0 deletions
+17
View File
@@ -4,6 +4,7 @@ package pgtype
import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"strconv"
@@ -92,6 +93,22 @@ func (src Int2) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
}
func (dst *Int2) UnmarshalJSON(b []byte) error {
var n *int16
err := json.Unmarshal(b, &n)
if err != nil {
return err
}
if n == nil {
*dst = Int2{}
} else {
*dst = Int2{Int: *n, Valid: true}
}
return nil
}
type Int2Codec struct{}
func (Int2Codec) FormatSupported(format int16) bool {