From d3eb036a5f0466b1725f437012363a67ed6fd17e Mon Sep 17 00:00:00 2001 From: Tristan Swadell Date: Tue, 9 Feb 2021 19:11:47 -0800 Subject: [PATCH] Re-add support for duration and timestamp proto values to CEL values. (#414) * Re-add support for duration and timestamp proto values to CEL values. * Removed redundant test. --- common/types/provider.go | 6 ++++++ common/types/provider_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/common/types/provider.go b/common/types/provider.go index 7f99f786..62be9f00 100644 --- a/common/types/provider.go +++ b/common/types/provider.go @@ -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 { @@ -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: diff --git a/common/types/provider_test.go b/common/types/provider_test.go index bd6d3de1..6cecab39 100644 --- a/common/types/provider_test.go +++ b/common/types/provider_test.go @@ -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" ) @@ -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}))