Skip to content

Commit

Permalink
Generic Convert handles conversion to time
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 28, 2017
1 parent c1fd00c commit 2083747
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 67 deletions.
19 changes: 4 additions & 15 deletions filters/filters.go
Expand Up @@ -23,23 +23,12 @@ func DefineStandardFilters() {
})

// dates
expressions.DefineFilter("date", func(in, iformat interface{}) interface{} {
format, ok := iformat.(string)
expressions.DefineFilter("date", func(value time.Time, format interface{}) interface{} {
form, ok := format.(string)
if !ok {
format = "%a, %b %d, %y"
}
switch date := in.(type) {
case string:
d, err := generics.ParseTime(date)
if err != nil {
panic(err)
}
return timeutil.Strftime(&d, format)
case time.Time:
return timeutil.Strftime(&date, format)
default:
panic(expressions.UnimplementedError(fmt.Sprintf("date conversion from %v", date)))
form = "%a, %b %d, %y"
}
return timeutil.Strftime(&value, form)
})

// lists
Expand Down
2 changes: 1 addition & 1 deletion generics/call.go
Expand Up @@ -33,7 +33,7 @@ func convertArguments(fn reflect.Value, in []interface{}) []reflect.Value {
if arg == nil {
out[i] = reflect.Zero(rt.In(i))
} else {
out[i] = convertType(arg, rt.In(i))
out[i] = Convert(arg, rt.In(i))
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions generics/convert.go
@@ -0,0 +1,62 @@
package generics

import (
"reflect"
"time"
)

var timeType = reflect.TypeOf(time.Now())

// Convert value to the type. This is a more aggressive conversion, that will
// recursively create new map and slice values as necessary. It doesn't
// handle circular references.
func Convert(value interface{}, t reflect.Type) reflect.Value {
r := reflect.ValueOf(value)
if r.Type().ConvertibleTo(t) {
return r.Convert(t)
}
if reflect.PtrTo(r.Type()) == t {
return reflect.ValueOf(&value)
}
if r.Kind() == reflect.String && t == timeType {
v, err := ParseTime(value.(string))
if err != nil {
panic(err)
}
return reflect.ValueOf(v)
}
switch t.Kind() {
case reflect.Slice:
if r.Kind() != reflect.Array && r.Kind() != reflect.Slice {
break
}
x := reflect.MakeSlice(t, 0, r.Len())
for i := 0; i < r.Len(); i++ {
c := Convert(r.Index(i).Interface(), t.Elem())
x = reflect.Append(x, c)
}
return x
}
panic(genericErrorf("generic.Convert can't convert %#v<%s> to %v", value, r.Type(), t))
}

var dateLayouts = []string{
"2006-01-02 15:04:05 -07:00",
"January 2, 2006",
"2006-01-02",
}

// ParseTime tries a few heuristics to parse a date from a string
func ParseTime(value string) (time.Time, error) {
if value == "now" {
return time.Now(), nil
}
for _, layout := range dateLayouts {
// fmt.Println(layout, time.Now().Format(layout), value)
time, err := time.Parse(layout, value)
if err == nil {
return time, nil
}
}
return time.Now(), genericErrorf("can't convert %s to a time", value)
}
51 changes: 0 additions & 51 deletions generics/generics.go
Expand Up @@ -3,7 +3,6 @@ package generics
import (
"fmt"
"reflect"
"time"
)

// GenericError is an error regarding generic conversion.
Expand All @@ -15,35 +14,6 @@ func genericErrorf(format string, a ...interface{}) error {
return GenericError(fmt.Sprintf(format, a...))
}

// Convert val to the type. This is a more aggressive conversion, that will
// recursively create new map and slice values as necessary. It doesn't
// handle circular references.
func convertType(val interface{}, t reflect.Type) reflect.Value {
r := reflect.ValueOf(val)
if r.Type().ConvertibleTo(t) {
return r.Convert(t)
}
if reflect.PtrTo(r.Type()) == t {
return reflect.ValueOf(&val)
}
// if r.Kind() == reflect.String && t.Name() == "time.Time" {
// fmt.Println("ok")
// }
switch t.Kind() {
case reflect.Slice:
if r.Kind() != reflect.Array && r.Kind() != reflect.Slice {
break
}
x := reflect.MakeSlice(t, 0, r.Len())
for i := 0; i < r.Len(); i++ {
c := convertType(r.Index(i).Interface(), t.Elem())
x = reflect.Append(x, c)
}
return x
}
panic(genericErrorf("convertType: can't convert %#v<%s> to %v", val, r.Type(), t))
}

// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
func IsEmpty(value interface{}) bool {
if value == nil {
Expand All @@ -64,24 +34,3 @@ func IsEmpty(value interface{}) bool {
func IsTrue(value interface{}) bool {
return value != nil && value != false
}

var dateLayouts = []string{
"2006-01-02 15:04:05 -07:00",
"January 2, 2006",
"2006-01-02",
}

// ParseTime tries a few heuristics to parse a date from a string
func ParseTime(value string) (time.Time, error) {
if value == "now" {
return time.Now(), nil
}
for _, layout := range dateLayouts {
// fmt.Println(layout, time.Now().Format(layout), value)
time, err := time.Parse(layout, value)
if err == nil {
return time, nil
}
}
return time.Now(), genericErrorf("can't convert %s to a time", value)
}

0 comments on commit 2083747

Please sign in to comment.