Skip to content

Commit

Permalink
Add validate and Ptr methods to Time and adapt tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwieser committed Jan 8, 2021
1 parent eb68e02 commit 8b35f85
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
29 changes: 17 additions & 12 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (

func TestFloat_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Float64
expectErr error
name string
buf *bytes.Buffer
expect Float64
expectErr error
expectedPtrNil bool
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Float64{
Present: true,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: true,
},
{
name: "valid value",
Expand All @@ -30,21 +32,24 @@ func TestFloat_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: 1.1,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: false,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Float64{},
expectErr: nil,
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Float64{},
expectErr: nil,
expectedPtrNil: true,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: Float64{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
expectErr: &json.UnmarshalTypeError{},
expectedPtrNil: true,
},
}
for _, tt := range tests {
Expand All @@ -58,7 +63,7 @@ func TestFloat_UnmarshalJSON(t *testing.T) {
}

got := str.Value
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value {
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value || got.Ptr() == nil != tt.expectedPtrNil {
t.Errorf("expected value to be %#v got %#v", tt.expect, got)
}
})
Expand Down
29 changes: 17 additions & 12 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (

func TestInt_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Int
expectErr error
name string
buf *bytes.Buffer
expect Int
expectErr error
expectedPtrNil bool
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Int{
Present: true,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: true,
},
{
name: "valid value",
Expand All @@ -30,21 +32,24 @@ func TestInt_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: 1,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: false,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Int{},
expectErr: nil,
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Int{},
expectErr: nil,
expectedPtrNil: true,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":"wat"}`),
expect: Int{
Present: true,
},
expectErr: &json.UnmarshalTypeError{},
expectErr: &json.UnmarshalTypeError{},
expectedPtrNil: true,
},
}
for _, tt := range tests {
Expand All @@ -58,7 +63,7 @@ func TestInt_UnmarshalJSON(t *testing.T) {
}

got := str.Value
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value {
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value || got.Ptr() == nil != tt.expectedPtrNil {
t.Errorf("expected value to be %#v got %#v", tt.expect, got)
}
})
Expand Down
1 change: 1 addition & 0 deletions scripts/nullable/nullable_types_test.gotmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by scripts/models/gen_nullable_types.go

// TODO add generation of tests
17 changes: 17 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"time"

"github.com/go-openapi/strfmt"
)

// Time represents a time that may be null or not
Expand All @@ -14,6 +16,16 @@ type Time struct {
Value time.Time
}

// Returns nil if not present or valid. Otherwise it will
// return a pointer to the value.
func (t *Time) Ptr() *time.Time {
if t.Present && t.Valid {
return &t.Value
}

return nil
}

// UnmarshalJSON implements json.Marshaler interface.
func (t *Time) UnmarshalJSON(data []byte) error {
t.Present = true
Expand All @@ -29,3 +41,8 @@ func (t *Time) UnmarshalJSON(data []byte) error {
t.Valid = true
return nil
}

// Validate implements runtime.Validateable interface for go-swagger generation.
func (t *Time) Validate(formats strfmt.Registry) error {
return nil
}
29 changes: 17 additions & 12 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ var referenceDate = time.Date(1991, 5, 23, 1, 2, 3, 4, time.UTC)

func TestTime_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
buf *bytes.Buffer
expect Time
expectErr error
name string
buf *bytes.Buffer
expect Time
expectErr error
expectedPtrNil bool
}{
{
name: "null value",
buf: bytes.NewBufferString(`{"value":null}`),
expect: Time{
Present: true,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: true,
},
{
name: "valid value",
Expand All @@ -32,21 +34,24 @@ func TestTime_UnmarshalJSON(t *testing.T) {
Valid: true,
Value: referenceDate,
},
expectErr: nil,
expectErr: nil,
expectedPtrNil: false,
},
{
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Time{},
expectErr: nil,
name: "empty",
buf: bytes.NewBufferString(`{}`),
expect: Time{},
expectErr: nil,
expectedPtrNil: true,
},
{
name: "unmarshallable",
buf: bytes.NewBufferString(`{"value":42}`),
expect: Time{
Present: true,
},
expectErr: &time.ParseError{},
expectErr: &time.ParseError{},
expectedPtrNil: true,
},
}
for _, tt := range tests {
Expand All @@ -60,7 +65,7 @@ func TestTime_UnmarshalJSON(t *testing.T) {
}

got := str.Value
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value {
if got.Present != tt.expect.Present || got.Valid != tt.expect.Valid || got.Value != tt.expect.Value || got.Ptr() == nil != tt.expectedPtrNil {
t.Errorf("expected value to be %#v got %#v", tt.expect, got)
}
})
Expand Down

0 comments on commit 8b35f85

Please sign in to comment.