Skip to content

Commit

Permalink
Fixed linter errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Feb 19, 2019
1 parent c660ae2 commit 472ccd6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 69 deletions.
36 changes: 19 additions & 17 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,52 @@ import (
"encoding/json"
)

// Nullable boolean type based on sql.NullBool, that supports parsing to/from JSON.
// Bool is a nullable boolean type based on sql.NullBool, that supports parsing to/from JSON.
type Bool struct {
sql.NullBool
}

// Returns a new nullable Bool object.
// NewBool returns a new nullable Bool object.
// This is equivalent to `null.Bool{sql.NullBool{Bool: b, Valid: valid}}`.
func NewBool(b, valid bool) Bool {
return Bool{sql.NullBool{Bool: b, Valid: valid}}
}

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

return json.Marshal(nil)
}

func (this *Bool) UnmarshalJSON(data []byte) error {
// UnmarshalJSON implements the encoding json interface.
func (b *Bool) UnmarshalJSON(data []byte) error {
var value *bool

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

if value != nil {
this.Valid = true
this.Bool = *value
b.Valid = true
b.Bool = *value
} else {
this.Valid = false
b.Valid = false
}

return nil
}

// Sets the value and valid to true.
func (this *Bool) SetValid(b bool) {
this.Bool = b
this.Valid = true
// SetValid sets the value and valid to true.
func (b *Bool) SetValid(value bool) {
b.Bool = value
b.Valid = true
}

// Sets the value to default and valid to false.
func (this *Bool) SetNil() {
this.Bool = false
this.Valid = false
// SetNil sets the value to default and valid to false.
func (b *Bool) SetNil() {
b.Bool = false
b.Valid = false
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package "null" provides nullable types (based on database/sql) that can be parsed to and from JSON.
// Package null provides nullable types (based on database/sql) that can be parsed to and from JSON.
package null
36 changes: 19 additions & 17 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,52 @@ import (
"encoding/json"
)

// Nullable float64 type based on sql.NullFloat64, that supports parsing to/from JSON.
// Float64 is a nullable float64 type based on sql.NullFloat64, that supports parsing to/from JSON.
type Float64 struct {
sql.NullFloat64
}

// Returns a new nullable Float64 object.
// NewFloat64 returns a new nullable Float64 object.
// This is equivalent to `null.Float64{sql.NullFloat64{Float64: f, Valid: valid}}`.
func NewFloat64(f float64, valid bool) Float64 {
return Float64{sql.NullFloat64{Float64: f, Valid: valid}}
}

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

return json.Marshal(nil)
}

func (this *Float64) UnmarshalJSON(data []byte) error {
// UnmarshalJSON implements the encoding json interface.
func (f *Float64) UnmarshalJSON(data []byte) error {
var value *float64

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

if value != nil {
this.Valid = true
this.Float64 = *value
f.Valid = true
f.Float64 = *value
} else {
this.Valid = false
f.Valid = false
}

return nil
}

// Sets the value and valid to true.
func (this *Float64) SetValid(f float64) {
this.Float64 = f
this.Valid = true
// SetValid sets the value and valid to true.
func (f *Float64) SetValid(value float64) {
f.Float64 = value
f.Valid = true
}

// Sets the value to default and valid to false.
func (this *Float64) SetNil() {
this.Float64 = 0
this.Valid = false
// SetNil sets the value to default and valid to false.
func (f *Float64) SetNil() {
f.Float64 = 0
f.Valid = false
}
36 changes: 19 additions & 17 deletions int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,52 @@ import (
"encoding/json"
)

// Nullable int64 type based on sql.NullInt64, that supports parsing to/from JSON.
// Int64 is a nullable int64 type based on sql.NullInt64, that supports parsing to/from JSON.
type Int64 struct {
sql.NullInt64
}

// Returns a new nullable Int64 object.
// NewInt64 returns a new nullable Int64 object.
// This is equivalent to `null.Int64{sql.NullInt64{Int64: i, Valid: valid}}`.
func NewInt64(i int64, valid bool) Int64 {
return Int64{sql.NullInt64{Int64: i, Valid: valid}}
}

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

return json.Marshal(nil)
}

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

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

if value != nil {
this.Valid = true
this.Int64 = *value
i.Valid = true
i.Int64 = *value
} else {
this.Valid = false
i.Valid = false
}

return nil
}

// Sets the value and valid to true.
func (this *Int64) SetValid(i int64) {
this.Int64 = i
this.Valid = true
// SetValid sets the value and valid to true.
func (i *Int64) SetValid(value int64) {
i.Int64 = value
i.Valid = true
}

// Sets the value to default and valid to false.
func (this *Int64) SetNil() {
this.Int64 = 0
this.Valid = false
// SetNil sets the value to default and valid to false.
func (i *Int64) SetNil() {
i.Int64 = 0
i.Valid = false
}
36 changes: 19 additions & 17 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,52 @@ import (
"encoding/json"
)

// Nullable string type based on sql.NullString, that supports parsing to/from JSON.
// String is a nullable string type based on sql.NullString, that supports parsing to/from JSON.
type String struct {
sql.NullString
}

// Returns a new nullable String object.
// NewString returns a new nullable String object.
// This is equivalent to `null.String{sql.NullString{String: s, Valid: valid}}`.
func NewString(s string, valid bool) String {
return String{sql.NullString{String: s, Valid: valid}}
}

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

return json.Marshal(nil)
}

func (this *String) UnmarshalJSON(data []byte) error {
// UnmarshalJSON implements the encoding json interface.
func (s *String) UnmarshalJSON(data []byte) error {
var value *string

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

if value != nil {
this.Valid = true
this.String = *value
s.Valid = true
s.String = *value
} else {
this.Valid = false
s.Valid = false
}

return nil
}

// Sets the value and valid to true.
func (this *String) SetValid(s string) {
this.String = s
this.Valid = true
// SetValid sets the value and valid to true.
func (s *String) SetValid(value string) {
s.String = value
s.Valid = true
}

// Sets the value to default and valid to false.
func (this *String) SetNil() {
this.String = ""
this.Valid = false
// SetNil sets the value to default and valid to false.
func (s *String) SetNil() {
s.String = ""
s.Valid = false
}

0 comments on commit 472ccd6

Please sign in to comment.