Skip to content

Commit

Permalink
Added int32 type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Sep 4, 2019
1 parent b55870b commit ce7f402
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
55 changes: 55 additions & 0 deletions int32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package null

import (
"database/sql"
"encoding/json"
)

// Int32 is a nullable int32 type based on sql.NullInt32, that supports parsing to/from JSON.
type Int32 struct {
sql.NullInt32
}

// NewInt32 returns a new nullable Int32 object.
// This is equivalent to `null.Int32{sql.NullInt32{Int32: i, Valid: valid}}`.
func NewInt32(i int32, valid bool) Int32 {
return Int32{sql.NullInt32{Int32: i, Valid: valid}}
}

// MarshalJSON implements the encoding json interface.
func (i Int32) MarshalJSON() ([]byte, error) {
if i.Valid {
return json.Marshal(i.Int32)
}

return json.Marshal(nil)
}

// UnmarshalJSON implements the encoding json interface.
func (i *Int32) UnmarshalJSON(data []byte) error {
var value *int32

if err := json.Unmarshal(data, &value); err != nil {
return err
}

if value != nil {
i.SetValid(*value)
} else {
i.SetNil()
}

return nil
}

// SetValid sets the value and valid to true.
func (i *Int32) SetValid(value int32) {
i.Int32 = value
i.Valid = true
}

// SetNil sets the value to default and valid to false.
func (i *Int32) SetNil() {
i.Int32 = 0
i.Valid = false
}
71 changes: 71 additions & 0 deletions int32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package null

import (
"database/sql"
"encoding/json"
"testing"
)

type testInt32 struct {
Value Int32 `json:"value"`
}

func TestNewInt32(t *testing.T) {
value := NewInt32(123, true)

if value.Int32 != 123 || !value.Valid {
t.Fatal("New Int32 must have value and be valid")
}
}

func TestMarshalInt32(t *testing.T) {
value := Int32{sql.NullInt32{Int32: 123, Valid: true}}

if data, err := json.Marshal(value); err != nil || string(data) != "123" {
t.Fatalf("Int32 must be marshalled to value, but was %v %v", err, string(data))
}

value.Valid = false

if data, err := json.Marshal(value); err != nil || string(data) != "null" {
t.Fatalf("Int32 must be marshalled to null, but was %v %v", err, string(data))
}
}

func TestUnmarshalInt32(t *testing.T) {
str := `{"value": 123}`
var value testInt32

if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("Int32 must be unmarshalled to value, but was %v", err)
}

if !value.Value.Valid || value.Value.Int32 != 123 {
t.Fatalf("Unmarshalled null Int32 must be valid, but was %v", value.Value)
}

str = `{"value": null}`

if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("Int32 must be unmarshalled to null, but was %v", err)
}

if value.Value.Valid {
t.Fatal("Unmarshalled null Int32 must be invalid")
}
}

func TestGettersSettersInt32(t *testing.T) {
value := NewInt32(123, true)
value.SetNil()

if value.Int32 != 0 || value.Valid {
t.Fatal("Int32 must be nil")
}

value.SetValid(123)

if value.Int32 != 123 || !value.Valid {
t.Fatal("Int32 must be valid")
}
}

0 comments on commit ce7f402

Please sign in to comment.