Skip to content

Commit 7f3ff2b

Browse files
authored
core: add parsing for absolute time.Time (#1210)
1 parent 4216b26 commit 7f3ff2b

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

internal/args/args_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"strings"
77
"testing"
8+
"time"
89

910
"github.com/alecthomas/assert"
1011
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -35,6 +36,7 @@ type Slice struct {
3536

3637
type WellKnownTypes struct {
3738
Size scw.Size
39+
Time time.Time
3840
}
3941

4042
type Nested struct {

internal/args/marshal.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strconv"
1212
"strings"
13+
"time"
1314

1415
"github.com/dustin/go-humanize"
1516
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -29,6 +30,10 @@ var marshalFuncs = map[reflect.Type]MarshalFunc{
2930
value = strings.Replace(value, " ", "", -1)
3031
return value, nil
3132
},
33+
reflect.TypeOf((*time.Time)(nil)).Elem(): func(src interface{}) (string, error) {
34+
v := src.(*time.Time)
35+
return v.Format(time.RFC3339), nil
36+
},
3237
}
3338

3439
// MarshalStruct marshals a go struct using reflection to args like ["arg1=1", "arg2=2"].

internal/args/marshal_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package args
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/scaleway/scaleway-sdk-go/scw"
78
"github.com/stretchr/testify/assert"
@@ -120,9 +121,11 @@ func TestMarshal(t *testing.T) {
120121
t.Run("well-known-types", run(TestCase{
121122
data: &WellKnownTypes{
122123
Size: 20 * scw.GB,
124+
Time: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),
123125
},
124126
expected: []string{
125127
"size=20GB",
128+
"time=2006-01-02T15:04:05Z",
126129
},
127130
}))
128131

internal/args/unmarshal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"reflect"
1111
"strconv"
1212
"strings"
13+
"time"
1314

1415
"github.com/dustin/go-humanize"
1516
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -45,6 +46,16 @@ var unmarshalFuncs = map[reflect.Type]UnmarshalFunc{
4546
*(dest.(*io.Reader)) = strings.NewReader(value)
4647
return nil
4748
},
49+
reflect.TypeOf((*time.Time)(nil)).Elem(): func(value string, dest interface{}) error {
50+
// Handle absolute time
51+
t, err := time.Parse(time.RFC3339, value)
52+
if err != nil {
53+
return err
54+
}
55+
56+
*(dest.(*time.Time)) = t
57+
return nil
58+
},
4859
}
4960

5061
// UnmarshalStruct parses args like ["arg1=1", "arg2=2"] to a Go structure using reflection.

internal/args/unmarshal_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package args
33
import (
44
"reflect"
55
"testing"
6+
"time"
67

78
"github.com/scaleway/scaleway-sdk-go/scw"
89
"github.com/stretchr/testify/assert"
@@ -236,6 +237,15 @@ func TestUnmarshalStruct(t *testing.T) {
236237
},
237238
}))
238239

240+
t.Run("Absolute date", run(TestCase{
241+
args: []string{
242+
"time=2006-01-02T15:04:05Z",
243+
},
244+
expected: &WellKnownTypes{
245+
Time: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC),
246+
},
247+
}))
248+
239249
t.Run("nested-basic", run(TestCase{
240250
args: []string{
241251
"basic.string=test",

0 commit comments

Comments
 (0)