Skip to content

Commit

Permalink
Re-add support for duration and timestamp proto values to CEL values. (
Browse files Browse the repository at this point in the history
…#414)

* Re-add support for duration and timestamp proto values to CEL values.
* Removed redundant test.
  • Loading branch information
TristonianJones authored Feb 10, 2021
1 parent c1e3967 commit d3eb036
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common/types/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (

exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
anypb "google.golang.org/protobuf/types/known/anypb"
dpb "google.golang.org/protobuf/types/known/durationpb"
structpb "google.golang.org/protobuf/types/known/structpb"
tpb "google.golang.org/protobuf/types/known/timestamppb"
)

type protoTypeRegistry struct {
Expand Down Expand Up @@ -302,8 +304,12 @@ func nativeToValue(a ref.TypeAdapter, value interface{}) (ref.Val, bool) {
return Double(v), true
case string:
return String(v), true
case *dpb.Duration:
return Duration{Duration: v.AsDuration()}, true
case time.Duration:
return Duration{Duration: v}, true
case *tpb.Timestamp:
return Timestamp{Time: v.AsTime()}, true
case time.Time:
return Timestamp{Time: v}, true
case *bool:
Expand Down
4 changes: 4 additions & 0 deletions common/types/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (

exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
anypb "google.golang.org/protobuf/types/known/anypb"
dpb "google.golang.org/protobuf/types/known/durationpb"
structpb "google.golang.org/protobuf/types/known/structpb"
tpb "google.golang.org/protobuf/types/known/timestamppb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
)

Expand Down Expand Up @@ -395,6 +397,8 @@ func TestNativeToValue_Primitive(t *testing.T) {
expectNativeToValue(t, []byte("world"), Bytes("world"))
expectNativeToValue(t, time.Duration(500), Duration{Duration: time.Duration(500)})
expectNativeToValue(t, time.Unix(12345, 0), Timestamp{Time: time.Unix(12345, 0)})
expectNativeToValue(t, dpb.New(time.Duration(500)), Duration{Duration: time.Duration(500)})
expectNativeToValue(t, tpb.New(time.Unix(12345, 0)), Timestamp{Time: time.Unix(12345, 0)})
expectNativeToValue(t, []int32{1, 2, 3}, NewDynamicList(reg, []int32{1, 2, 3}))
expectNativeToValue(t, map[int32]int32{1: 1, 2: 1, 3: 1},
NewDynamicMap(reg, map[int32]int32{1: 1, 2: 1, 3: 1}))
Expand Down

0 comments on commit d3eb036

Please sign in to comment.