-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Int64Slice,StrSlice,Float64Slice and BoolSlice #27
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// BoolSlice makes easy to handle JSON encoded bool lists from/to db stored either in TEXT or BLOB. | ||
// | ||
// BoolSlice is `[]bool` type, adding `Value()` and `Scan()` methods for db access. | ||
// | ||
|
||
// BoolSlice makes easy to handle JSON encoded bool lists stored at database's text fields(like VARCHAR,CHAR,TEXT) and blob fields | ||
type BoolSlice []bool | ||
|
||
// Value returns value. | ||
// If value is invalid value, it returns an error. | ||
func (ss BoolSlice) Value() (driver.Value, error) { | ||
if ss == nil { | ||
return nil, nil | ||
} | ||
return json.Marshal(ss) | ||
} | ||
|
||
// Scan stores the value as BoolSlice. Value can be string, []byte or or nil. | ||
func (ss *BoolSlice) Scan(src interface{}) error { | ||
var source []byte | ||
switch t := src.(type) { | ||
case string: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = []byte(t) | ||
} | ||
case []byte: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = t | ||
} | ||
case nil: | ||
*ss = nil | ||
return nil | ||
default: | ||
return fmt.Errorf("Incompatible type for BoolSlice") | ||
} | ||
err := json.Unmarshal(source, ss) | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestBoolSlice(t *testing.T) { | ||
Convey("Given BoolSlice", t, func() { | ||
Convey("valid value", func() { | ||
v := []bool{true, false, true} | ||
nullBoolSlice := BoolSlice(v) | ||
So(len(nullBoolSlice), ShouldEqual, len(v)) | ||
vx, err := nullBoolSlice.Value() | ||
So(err, ShouldEqual, nil) | ||
So(string(vx.([]byte)), ShouldEqual, `[true,false,true]`) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line. |
||
}) | ||
|
||
Convey("nil value", func() { | ||
var nullBoolSlice BoolSlice | ||
err := nullBoolSlice.Scan(nil) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullBoolSlice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("invalid value", func() { | ||
var nullBoolSlice BoolSlice | ||
err := nullBoolSlice.Scan("a") | ||
So(err, ShouldNotEqual, nil) | ||
So(len(nullBoolSlice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse null", func() { | ||
var nullBoolSlice BoolSlice | ||
err := nullBoolSlice.Scan([]byte("null")) | ||
So(err, ShouldEqual, nil) | ||
So(nullBoolSlice, ShouldEqual, nil) | ||
So(len(nullBoolSlice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse from JS", func() { | ||
var nullBoolSlice BoolSlice | ||
err := nullBoolSlice.Scan([]byte(`[true,false,true]`)) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullBoolSlice), ShouldEqual, 3) | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Float64Slice makes easy to handle JSON encoded float64 lists from/to db stored either in TEXT or BLOB. | ||
// | ||
// Float64Slice is `[]float64` type, adding `Value()` and `Scan()` methods for db access. | ||
// | ||
|
||
// Float64Slice makes easy to handle JSON encoded float64 lists stored at database's text fields(like VARCHAR,CHAR,TEXT) and blob fields | ||
type Float64Slice []float64 | ||
|
||
// Value returns value. | ||
// If value is invalid value, it returns an error. | ||
func (ss Float64Slice) Value() (driver.Value, error) { | ||
if ss == nil { | ||
return nil, nil | ||
} | ||
return json.Marshal(ss) | ||
} | ||
|
||
// Scan stores the value as Float64Slice. Value can be string, []byte or or nil. | ||
func (ss *Float64Slice) Scan(src interface{}) error { | ||
var source []byte | ||
switch t := src.(type) { | ||
case string: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = []byte(t) | ||
} | ||
case []byte: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = t | ||
} | ||
case nil: | ||
*ss = nil | ||
return nil | ||
default: | ||
return fmt.Errorf("Incompatible type for Float64Slice") | ||
} | ||
err := json.Unmarshal(source, ss) | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestFloat64Slice(t *testing.T) { | ||
Convey("Given Float64Slice", t, func() { | ||
Convey("valid value", func() { | ||
v := []float64{1.1, 2.2, 3.3} | ||
nullFloat64Slice := Float64Slice(v) | ||
So(len(nullFloat64Slice), ShouldEqual, len(v)) | ||
vx, err := nullFloat64Slice.Value() | ||
So(err, ShouldEqual, nil) | ||
So(string(vx.([]byte)), ShouldEqual, `[1.1,2.2,3.3]`) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line. |
||
}) | ||
|
||
Convey("nil value", func() { | ||
var nullFloat64Slice Float64Slice | ||
err := nullFloat64Slice.Scan(nil) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullFloat64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("invalid value", func() { | ||
var nullFloat64Slice Float64Slice | ||
err := nullFloat64Slice.Scan("a") | ||
So(err, ShouldNotEqual, nil) | ||
So(len(nullFloat64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse null", func() { | ||
var nullFloat64Slice Float64Slice | ||
err := nullFloat64Slice.Scan([]byte("null")) | ||
So(err, ShouldEqual, nil) | ||
So(nullFloat64Slice, ShouldEqual, nil) | ||
So(len(nullFloat64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse from JS", func() { | ||
var nullFloat64Slice Float64Slice | ||
err := nullFloat64Slice.Scan([]byte(`[1.1,2.2,3.3]`)) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullFloat64Slice), ShouldEqual, 3) | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Int64Slice makes easy to handle JSON encoded int64 lists from/to db stored either in TEXT or BLOB. | ||
// | ||
// Int64Slice is `[]int64` type, adding `Value()` and `Scan()` methods for db access. | ||
// | ||
|
||
// Int64Slice makes easy to handle JSON encoded int64 lists stored at database's text fields(like VARCHAR,CHAR,TEXT) and blob fields | ||
type Int64Slice []int64 | ||
|
||
// Value returns value. | ||
// If value is invalid value, it returns an error. | ||
func (ss Int64Slice) Value() (driver.Value, error) { | ||
if ss == nil { | ||
return nil, nil | ||
} | ||
return json.Marshal(ss) | ||
} | ||
|
||
// Scan stores the value as Int64Slice. Value can be string, []byte or or nil. | ||
func (ss *Int64Slice) Scan(src interface{}) error { | ||
var source []byte | ||
switch t := src.(type) { | ||
case string: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = []byte(t) | ||
} | ||
case []byte: | ||
if len(t) == 0 { | ||
source = []byte("[]") | ||
} else { | ||
source = t | ||
} | ||
case nil: | ||
*ss = nil | ||
return nil | ||
default: | ||
return fmt.Errorf("Incompatible type for Int64Slice") | ||
} | ||
err := json.Unmarshal(source, ss) | ||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestInt64Slice(t *testing.T) { | ||
Convey("Given Int64Slice", t, func() { | ||
Convey("valid value", func() { | ||
v := []int64{1, 2, 3} | ||
nullInt64Slice := Int64Slice(v) | ||
So(len(nullInt64Slice), ShouldEqual, len(v)) | ||
vx, err := nullInt64Slice.Value() | ||
So(err, ShouldEqual, nil) | ||
So(string(vx.([]byte)), ShouldEqual, `[1,2,3]`) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line. |
||
}) | ||
|
||
Convey("nil value", func() { | ||
var nullInt64Slice Int64Slice | ||
err := nullInt64Slice.Scan(nil) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullInt64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("invalid value", func() { | ||
var nullInt64Slice Int64Slice | ||
err := nullInt64Slice.Scan("a") | ||
So(err, ShouldNotEqual, nil) | ||
So(len(nullInt64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse null", func() { | ||
var nullInt64Slice Int64Slice | ||
err := nullInt64Slice.Scan([]byte("null")) | ||
So(err, ShouldEqual, nil) | ||
So(nullInt64Slice, ShouldEqual, nil) | ||
So(len(nullInt64Slice), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("parse from JS", func() { | ||
var nullInt64Slice Int64Slice | ||
err := nullInt64Slice.Scan([]byte(`[1,2,3]`)) | ||
So(err, ShouldEqual, nil) | ||
So(len(nullInt64Slice), ShouldEqual, 3) | ||
}) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why using nullBoolSlice in every tests, even when there is no involved null/nil values ?
(same for all tests of all new types).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first I also added
Null*
types to these new types, but that was redundant as slices can benil
. Thosenull...
named variables are spoils from that try. All "null
" can be renamed to "v
" so it will become for examplevBoolSlice
forBoolSlice
. Thanks.