@@ -2,16 +2,22 @@ package protocel
22
33import (
44 "context"
5+ "encoding/base64"
56 "errors"
67 "fmt"
78
9+ "time"
10+
811 "github.com/google/cel-go/cel"
912 "github.com/google/cel-go/common/types"
1013 "github.com/google/cel-go/common/types/ref"
14+
15+ "github.com/google/cel-go/ext"
1116 "google.golang.org/protobuf/proto"
1217 "google.golang.org/protobuf/reflect/protoreflect"
1318 "google.golang.org/protobuf/reflect/protoregistry"
1419 "google.golang.org/protobuf/types/known/structpb"
20+ "google.golang.org/protobuf/types/known/timestamppb"
1521
1622 "github.com/sudorandom/fauxrpc"
1723 "github.com/sudorandom/fauxrpc/celfakeit"
@@ -298,6 +304,33 @@ func (p *protocel) celToValue(fd protoreflect.FieldDescriptor, val any) (protore
298304 case uint64 :
299305 return protoreflect .ValueOfFloat64 (float64 (t )), nil
300306 }
307+ case protoreflect .BytesKind :
308+ switch t := val .(type ) {
309+ case []byte :
310+ return protoreflect .ValueOfBytes (t ), nil
311+ case string :
312+ // Attempt to base64 decode the string, if it fails, use the raw string bytes
313+ decoded , err := base64 .StdEncoding .DecodeString (t )
314+ if err == nil {
315+ return protoreflect .ValueOfBytes (decoded ), nil
316+ }
317+ return protoreflect .ValueOfBytes ([]byte (t )), nil
318+ }
319+ case protoreflect .MessageKind :
320+ if fd .Message ().FullName () == "google.protobuf.Timestamp" {
321+ switch t := val .(type ) {
322+ case time.Time :
323+ ts := timestamppb .New (t )
324+ return protoreflect .ValueOfMessage (ts .ProtoReflect ()), nil
325+ case string :
326+ parsedTime , err := time .Parse (time .RFC3339Nano , t )
327+ if err != nil {
328+ return protoreflect .ValueOf (nil ), fmt .Errorf ("failed to parse timestamp: %w" , err )
329+ }
330+ ts := timestamppb .New (parsedTime )
331+ return protoreflect .ValueOfMessage (ts .ProtoReflect ()), nil
332+ }
333+ }
301334 }
302335 }
303336 return protoreflect .ValueOf (val ), nil
@@ -328,6 +361,7 @@ func getFieldFromName(fds protoreflect.FieldDescriptors, key string) protoreflec
328361func newEnv (files * protoregistry.Files ) (* cel.Env , error ) {
329362 return cel .NewEnv (
330363 celfakeit .Configure (),
364+ ext .Encoders (),
331365 cel .TypeDescs (files ),
332366 cel .Variable ("req" , cel .DynType ),
333367 cel .Variable ("service" , cel .StringType ),
@@ -337,4 +371,4 @@ func newEnv(files *protoregistry.Files) (*cel.Env, error) {
337371 cel .Variable ("faker" , cel .DynType ),
338372 cel .Types (& stubsv1.CELGenerate {}),
339373 )
340- }
374+ }
0 commit comments