From 3eef62067ad71e63209f7f721e1fef2871643d3f Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 12 Dec 2017 01:05:40 +0000 Subject: [PATCH] Enable parsing times in strings which have been set using JSON If the data of a record was set using CONTENT, MERGE, or DIFF, then any Javascript Date values were treated as strings. These values are now parsed if the field is defined as a datetime field. --- util/conv/conv.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/util/conv/conv.go b/util/conv/conv.go index 3a985be6..9848b0c2 100644 --- a/util/conv/conv.go +++ b/util/conv/conv.go @@ -220,9 +220,12 @@ func ConvertToBoolean(obj interface{}) (val bool, err error) { } func ConvertToDatetime(obj interface{}) (val time.Time, err error) { - if now, ok := obj.(time.Time); ok { + switch now := obj.(type) { + case time.Time: val = now - } else { + case string: + val, err = time.Parse(time.RFC3339Nano, now) + default: err = fmt.Errorf("Expected a datetime, but found '%v'", obj) } return @@ -255,7 +258,12 @@ func ConvertToRecord(obj interface{}, tb string) (val *sql.Thing, err error) { err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj) } } else { - err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj) + switch tb { + default: + err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj) + case "": + err = fmt.Errorf("Expected a record of any type, but found '%v'", tb, obj) + } } return }