Add UnmarshalJSON to generated ints

query-exec-mode
Jack Christensen 2022-01-01 11:25:26 -06:00
parent 0403c34ae3
commit d2cf33ed40
4 changed files with 115 additions and 0 deletions

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 {

View File

@ -94,6 +94,22 @@ func (src Int<%= pg_byte_size %>) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(int64(src.Int), 10)), nil
}
func (dst *Int<%= pg_byte_size %>) UnmarshalJSON(b []byte) error {
var n *int<%= pg_bit_size %>
err := json.Unmarshal(b, &n)
if err != nil {
return err
}
if n == nil {
*dst = Int<%= pg_byte_size %>{}
} else {
*dst = Int<%= pg_byte_size %>{Int: *n, Valid: true}
}
return nil
}
type Int<%= pg_byte_size %>Codec struct{}
func (Int<%= pg_byte_size %>Codec) FormatSupported(format int16) bool {

View File

@ -41,3 +41,44 @@ func TestInt2Codec(t *testing.T) {
{nil, new(*int16), isExpectedEq((*int16)(nil))},
})
}
func TestInt2MarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Int2
result string
}{
{source: pgtype.Int2{Int: 0}, result: "null"},
{source: pgtype.Int2{Int: 1, Valid: true}, result: "1"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
if string(r) != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
}
}
}
func TestInt2UnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Int2
}{
{source: "null", result: pgtype.Int2{Int: 0}},
{source: "1", result: pgtype.Int2{Int: 1, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Int2
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}

View File

@ -42,4 +42,45 @@ func TestInt<%= pg_byte_size %>Codec(t *testing.T) {
{nil, new(*int<%= pg_bit_size %>), isExpectedEq((*int<%= pg_bit_size %>)(nil))},
})
}
func TestInt<%= pg_byte_size %>MarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Int<%= pg_byte_size %>
result string
}{
{source: pgtype.Int<%= pg_byte_size %>{Int: 0}, result: "null"},
{source: pgtype.Int<%= pg_byte_size %>{Int: 1, Valid: true}, result: "1"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
if string(r) != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
}
}
}
func TestInt<%= pg_byte_size %>UnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Int<%= pg_byte_size %>
}{
{source: "null", result: pgtype.Int<%= pg_byte_size %>{Int: 0}},
{source: "1", result: pgtype.Int<%= pg_byte_size %>{Int: 1, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Int<%= pg_byte_size %>
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}
<% end %>