Skip to content

Commit

Permalink
Merge pull request #12 from go-carrot/br.null
Browse files Browse the repository at this point in the history
Add TypeHandlers for guregu/null types
  • Loading branch information
BrandonRomano authored Jun 23, 2017
2 parents 141ed2c + e3cf84f commit 255013d
Show file tree
Hide file tree
Showing 6 changed files with 830 additions and 602 deletions.
71 changes: 71 additions & 0 deletions null_handlers.go
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
}
101 changes: 101 additions & 0 deletions null_handlers_test.go
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)
}
10 changes: 10 additions & 0 deletions type_handlers.go → primitive_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strconv"
"time"
)

func stringHandler(input string, value *Value) error {
Expand Down Expand Up @@ -128,6 +129,15 @@ func uint64Handler(input string, value *Value) error {
return nil
}

func timeHandler(input string, value *Value) error {
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)"))
}
*value.Result.(*time.Time) = res
return nil
}

func invalidParam(name string, mustBe string) string {
return fmt.Sprintf("Invalid `%v` parameter, `%v` must be %v", name, name, mustBe)
}
Loading

0 comments on commit 255013d

Please sign in to comment.