-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from go-carrot/br.null
Add TypeHandlers for guregu/null types
- Loading branch information
Showing
6 changed files
with
830 additions
and
602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package validator | ||
|
||
import ( | ||
"errors" | ||
"gopkg.in/guregu/null.v3" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func NullIntHandler(input string, value *Value) error { | ||
// Get int64 | ||
res, err := strconv.ParseInt(input, 10, 64) | ||
if err != nil { | ||
return errors.New(invalidParam(value.Name, "an int64")) | ||
} | ||
|
||
// Update null.Int | ||
nullInt := value.Result.(*null.Int) | ||
(*nullInt).Int64 = int64(res) | ||
(*nullInt).Valid = true | ||
return nil | ||
} | ||
|
||
func NullStringHandler(input string, value *Value) error { | ||
nullString := value.Result.(*null.String) | ||
(*nullString).String = input | ||
(*nullString).Valid = true | ||
return nil | ||
} | ||
|
||
func NullFloatHandler(input string, value *Value) error { | ||
// Get float64 | ||
res, err := strconv.ParseFloat(input, 64) | ||
if err != nil { | ||
return errors.New(invalidParam(value.Name, "a float64")) | ||
} | ||
|
||
// Update null.Float | ||
nullFloat := value.Result.(*null.Float) | ||
(*nullFloat).Float64 = res | ||
(*nullFloat).Valid = true | ||
return nil | ||
} | ||
|
||
func NullBoolHandler(input string, value *Value) error { | ||
// Get bool | ||
res, err := strconv.ParseBool(input) | ||
if err != nil { | ||
return errors.New(invalidParam(value.Name, "a bool")) | ||
} | ||
|
||
// Update null.Bool | ||
nullBool := value.Result.(*null.Bool) | ||
(*nullBool).Bool = res | ||
(*nullBool).Valid = true | ||
return nil | ||
} | ||
|
||
func NullTimeHandler(input string, value *Value) error { | ||
// Get time.Time | ||
res, err := time.Parse(time.RFC3339, input) | ||
if err != nil { | ||
return errors.New(invalidParam(value.Name, "an RFC 3339 date-time (2006-01-02T15:04:05Z07:00)")) | ||
} | ||
|
||
// Update null.Time | ||
nullTime := value.Result.(*null.Time) | ||
(*nullTime).Time = res | ||
(*nullTime).Valid = true | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package validator_test | ||
|
||
import ( | ||
v "github.com/go-carrot/validator" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/guregu/null.v3" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// TestNullInt tests handling of a null.Int as the result | ||
func TestNullInt(t *testing.T) { | ||
// Test success case | ||
var id null.Int | ||
err := v.Validate([]*v.Value{ | ||
{Result: &id, Name: "id", Input: "12", TypeHandler: v.NullIntHandler}, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(12), id.Int64) | ||
assert.True(t, id.Valid) | ||
|
||
// Test failure case | ||
var failureId null.Int | ||
err = v.Validate([]*v.Value{ | ||
{Result: &failureId, Name: "id", Input: "12a", TypeHandler: v.NullIntHandler}, | ||
}) | ||
assert.NotNil(t, err) | ||
assert.False(t, failureId.Valid) | ||
} | ||
|
||
// TestNullString tests handling of a null.String as the result | ||
func TestNullString(t *testing.T) { | ||
// Test success case | ||
var slug null.String | ||
err := v.Validate([]*v.Value{ | ||
{Result: &slug, Name: "slug", Input: "hello", TypeHandler: v.NullStringHandler}, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "hello", slug.String) | ||
assert.True(t, slug.Valid) | ||
} | ||
|
||
// TestNullFloat tests handling of a null.Float as the result | ||
func TestNullFloat(t *testing.T) { | ||
// Test success case | ||
var id null.Float | ||
err := v.Validate([]*v.Value{ | ||
{Result: &id, Name: "id", Input: "12.8", TypeHandler: v.NullFloatHandler}, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, float64(12.8), id.Float64) | ||
assert.True(t, id.Valid) | ||
|
||
// Test failure case | ||
var failureId null.Float | ||
err = v.Validate([]*v.Value{ | ||
{Result: &failureId, Name: "id", Input: "12.8a", TypeHandler: v.NullFloatHandler}, | ||
}) | ||
assert.NotNil(t, err) | ||
assert.False(t, failureId.Valid) | ||
} | ||
|
||
// TestNullBool tests handling of a null.Bool as the result | ||
func TestNullBool(t *testing.T) { | ||
// Test success case | ||
var someBool null.Bool | ||
err := v.Validate([]*v.Value{ | ||
{Result: &someBool, Name: "some_bool", Input: "true", TypeHandler: v.NullBoolHandler}, | ||
}) | ||
assert.Nil(t, err) | ||
assert.True(t, someBool.Bool) | ||
assert.True(t, someBool.Valid) | ||
|
||
// Test failure case | ||
var someOtherBool null.Bool | ||
err = v.Validate([]*v.Value{ | ||
{Result: &someOtherBool, Name: "some_other_bool", Input: "12.8a", TypeHandler: v.NullBoolHandler}, | ||
}) | ||
assert.NotNil(t, err) | ||
assert.False(t, someOtherBool.Valid) | ||
} | ||
|
||
// TestNullTime tests handling of a null.Time as the result | ||
func TestNullTime(t *testing.T) { | ||
// Test success case | ||
var successTime null.Time | ||
err := v.Validate([]*v.Value{ | ||
{Result: &successTime, Name: "time", Input: "2012-11-01T22:08:41+00:00", TypeHandler: v.NullTimeHandler}, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, successTime.Time.Year(), 2012) | ||
assert.Equal(t, successTime.Time.Month(), time.November) | ||
assert.Equal(t, successTime.Time.Day(), 1) | ||
|
||
// Test failure case | ||
var errorTime time.Time | ||
err = v.Validate([]*v.Value{ | ||
{Result: &errorTime, Name: "time", Input: "abcd", TypeHandler: v.NullTimeHandler}, | ||
}) | ||
assert.NotNil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.