-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(values): adds support for time.Time as value
- Loading branch information
Showing
7 changed files
with
392 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package values | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/values/pb" | ||
) | ||
|
||
type Time struct { | ||
Underlying time.Time | ||
} | ||
|
||
func NewTime(t time.Time) *Time { | ||
return &Time{Underlying: t} | ||
} | ||
|
||
func (t *Time) UnwrapTo(to any) error { | ||
if t == nil { | ||
return errors.New("could not unwrap nil values.Time") | ||
} | ||
|
||
switch tt := to.(type) { | ||
case *time.Time: | ||
if tt == nil { | ||
return errors.New("cannot unwrap to nil pointer") | ||
} | ||
*tt = t.Underlying | ||
case *any: | ||
if tt == nil { | ||
return errors.New("cannot unwrap to nil pointer") | ||
} | ||
*tt = t.Underlying | ||
default: | ||
rto := reflect.ValueOf(to) | ||
if rto.CanConvert(reflect.TypeOf(new(time.Time))) { | ||
return t.UnwrapTo(rto.Convert(reflect.TypeOf(new(time.Time))).Interface()) | ||
} | ||
return fmt.Errorf("cannot unwrap to value of type: %T", to) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *Time) Unwrap() (any, error) { | ||
tt := new(time.Time) | ||
return *tt, t.UnwrapTo(tt) | ||
} | ||
|
||
func (t *Time) copy() Value { | ||
if t == nil { | ||
return nil | ||
} | ||
|
||
return NewTime(t.Underlying) | ||
} | ||
|
||
func (t *Time) proto() *pb.Value { | ||
return pb.NewTime(t.Underlying) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package values | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_TimeUnwrapTo(t *testing.T) { | ||
expected, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") | ||
assert.NoError(t, err) | ||
|
||
// Unwraps to a time.Time pointer | ||
v := NewTime(expected) | ||
got := new(time.Time) | ||
err = v.UnwrapTo(got) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, *got) | ||
|
||
// Fails to unwrap to nil time.Time pointer | ||
gotTime := (*time.Time)(nil) | ||
err = v.UnwrapTo(gotTime) | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "cannot unwrap to nil pointer") | ||
|
||
// Unwraps to an any pointer | ||
var varAny any | ||
err = v.UnwrapTo(&varAny) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, varAny) | ||
|
||
// Fails to unwrap to a string pointer | ||
var varStr string | ||
err = v.UnwrapTo(&varStr) | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "cannot unwrap to value of type: *string") | ||
|
||
// Fails to unwrap nil value of Time | ||
nilVal := (*Time)(nil) | ||
_, err = nilVal.Unwrap() | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "could not unwrap nil") | ||
|
||
// Unwraps zero value of Time | ||
zeroTime := &Time{} | ||
unwrapped, err := zeroTime.Unwrap() | ||
assert.NoError(t, err) | ||
assert.Equal(t, time.Time{}, unwrapped) | ||
} | ||
|
||
// Test_Time tests that Time values can be converted to and from protobuf representations. | ||
func Test_Time(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
t time.Time | ||
}{ | ||
{ | ||
name: "zero", | ||
t: time.Time{}, | ||
}, | ||
{ | ||
name: "some time", | ||
t: func() time.Time { | ||
someTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") | ||
assert.NoError(t, err) | ||
return someTime | ||
}(), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
v := NewTime(tc.t) | ||
|
||
vp := Proto(v) | ||
got, err := FromProto(vp) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.t, got.(*Time).Underlying) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters