Skip to content

Commit

Permalink
Adding min/max constraints to year type. Enjoying the opportunity to …
Browse files Browse the repository at this point in the history
…rename castYear to decodeYear (consistent to the rest of the code). #42
  • Loading branch information
danielfireman committed Sep 28, 2017
1 parent 05bb587 commit 817050b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
33 changes: 32 additions & 1 deletion schema/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,41 @@ func castYearMonth(value string) (time.Time, error) {
return time.Parse("2006-01", value)
}

func castYear(value string) (time.Time, error) {
func decodeYearWithoutChecks(value string) (time.Time, error) {
return time.Parse("2006", value)
}

func decodeYear(value string, c Constraints) (time.Time, error) {
y, err := decodeYearWithoutChecks(value)
if err != nil {
return time.Now(), err
}
var max, min time.Time
if c.Maximum != "" {
max, err = decodeYearWithoutChecks(c.Maximum)
if err != nil {
return time.Now(), err
}
}
if c.Minimum != "" {
min, err = decodeYearWithoutChecks(c.Minimum)
if err != nil {
return time.Now(), err
}
}
return checkConstraints(y, max, min, YearType)
}

func checkConstraints(v, max, min time.Time, t string) (time.Time, error) {
if !max.IsZero() && v.After(max) {
return time.Now(), fmt.Errorf("constraint check error: %s:%v > maximum:%v", t, v, max)
}
if !min.IsZero() && v.Before(min) {
return time.Now(), fmt.Errorf("constraint check error: %s:%v < minimum:%v", t, v, max)
}
return v, nil
}

func castDateTime(format, value string) (time.Time, error) {
return castDefaultOrCustomTime(time.RFC3339, format, value)
}
Expand Down
33 changes: 33 additions & 0 deletions schema/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ import (
"time"
)

func TestDecodeYear(t *testing.T) {
t.Run("ValidMaximum", func(t *testing.T) {
if _, err := decodeYear("2006", Constraints{Maximum: "2007"}); err != nil {
t.Fatalf("err want:nil got:%q", err)
}
})
t.Run("ValidMinimum", func(t *testing.T) {
if _, err := decodeYear("2007", Constraints{Minimum: "2006"}); err != nil {
t.Fatalf("err want:nil got:%q", err)
}
})
t.Run("Error", func(t *testing.T) {
data := []struct {
desc string
year string
constraints Constraints
}{
{"InvalidYear", "foo", Constraints{}},
{"YearBiggerThanMaximum", "2006", Constraints{Maximum: "2005"}},
{"InvalidMaximum", "2005", Constraints{Maximum: "boo"}},
{"YearSmallerThanMinimum", "2005", Constraints{Minimum: "2006"}},
{"InvalidMinimum", "2005", Constraints{Minimum: "boo"}},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
if _, err := decodeYear(d.year, d.constraints); err == nil {
t.Fatalf("err want:err got:nil")
}
})
}
})
}

func TestEncodeTime(t *testing.T) {
t.Run("Success", func(t *testing.T) {
data := []struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (f *Field) Decode(value string) (interface{}, error) {
case YearMonthType:
return castYearMonth(value)
case YearType:
return castYear(value)
return decodeYear(value, f.Constraints)
case DateTimeType:
return castDateTime(f.Format, value)
case DurationType:
Expand Down
2 changes: 1 addition & 1 deletion schema/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func findType(value string, checkOrder []string) string {
return YearMonthType
}
case YearType:
if _, err := castYear(value); err == nil {
if _, err := decodeYear(value, Constraints{}); err == nil {
return YearType
}
case DateTimeType:
Expand Down

0 comments on commit 817050b

Please sign in to comment.