Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/norunners/vert

go 1.16

require github.com/corbym/gocrest v1.0.5
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/corbym/gocrest v1.0.5 h1:6FUvLjKkKQ2hwMel8OKIZqN3slYEOHIFY9+lVaroFnU=
github.com/corbym/gocrest v1.0.5/go.mod h1:lF3xBPnOU5DYDpa/vUq63SMxUhd5UAwGgmA8Z2pJ/Tk=
6 changes: 6 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package vert
import (
"reflect"
"syscall/js"
"time"
)

var (
Expand Down Expand Up @@ -90,6 +91,11 @@ func valueOfMap(v reflect.Value) js.Value {

// valueOfStruct returns a new object value.
func valueOfStruct(v reflect.Value) js.Value {
if t, ok := v.Interface().(time.Time); ok {
// special case: Time, instantiates a new js Date object
dateConstructor := js.Global().Get("Date")
return dateConstructor.New(t.Format(time.RFC3339))
}
t := v.Type()
s := object.New()
n := v.NumField()
Expand Down
22 changes: 22 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vert

import (
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"testing"
"time"
)

const isoStringLength = len("yyyy-MM-ddThh:mm:ss")

func TestValueOfTime(t *testing.T) {
now := time.Now()

jsVal := ValueOf(now)

// cut of the timezone information, because seconds precision is sufficient for this test
jsValIsoString := jsVal.Call("toISOString").String()
jsValIsoString = jsValIsoString[:isoStringLength]

then.AssertThat(t, jsValIsoString, is.EqualTo(now.UTC().Format(time.RFC3339)[:isoStringLength]))
}