Skip to content

Commit

Permalink
[CAPPL-31] feat(values): adds support for time.Time as value (#787)
Browse files Browse the repository at this point in the history
* feat(values): adds support for time.Time as value

* chore(deps): updates .tool-versions

* refactor(values): uses primitive type in protos
  • Loading branch information
MStreet3 authored and cedric-cordenier committed Oct 4, 2024
1 parent f758a45 commit f56aed3
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
golang 1.21.4
golang 1.22.7
protoc 25.1
protoc-gen-go-grpc 1.3.0
golangci-lint 1.55.2
mockery 2.43.2
11 changes: 11 additions & 0 deletions pkg/values/pb/values.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package pb

import (
"time"

"github.com/shopspring/decimal"
"google.golang.org/protobuf/types/known/timestamppb"
)

func NewBoolValue(b bool) *Value {
Expand Down Expand Up @@ -80,3 +83,11 @@ func NewBigIntValue(sign int, bib []byte) *Value {
},
}
}

func NewTime(t time.Time) *Value {
return &Value{
Value: &Value_TimeValue{
TimeValue: timestamppb.New(t),
},
}
}
150 changes: 87 additions & 63 deletions pkg/values/pb/values.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions pkg/values/pb/values.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

option go_package = "github.com/smartcontractkit/chainlink-common/pkg/values/pb";

package values;
Expand All @@ -15,21 +17,18 @@ message Value {
Decimal decimal_value = 6;
int64 int64_value = 7;
BigInt bigint_value = 9;
google.protobuf.Timestamp time_value = 10;
}
}

message BigInt {
bytes abs_val = 1;
int64 sign = 2;
bytes abs_val = 1;
int64 sign = 2;
}

message Map {
map<string, Value> fields = 1;
}
message Map { map<string, Value> fields = 1; }

message List {
repeated Value fields = 2;
}
message List { repeated Value fields = 2; }

message Decimal {
BigInt coefficient = 1;
Expand Down
41 changes: 41 additions & 0 deletions pkg/values/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package values

import (
"errors"
"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")
}

return unwrapTo(t.Underlying, to)
}

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)
}
Loading

0 comments on commit f56aed3

Please sign in to comment.