Skip to content

Commit b027dc1

Browse files
committed
feat: add support for cel->proto w/bytes, add base64.encode to cel
1 parent 20365d5 commit b027dc1

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

private/stubs/cel.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/google/cel-go/cel"
8+
"github.com/google/cel-go/ext"
89
"github.com/sudorandom/fauxrpc/celfakeit"
910
"github.com/sudorandom/fauxrpc/private/registry"
1011
"github.com/sudorandom/fauxrpc/protocel"
@@ -20,11 +21,13 @@ func NewActiveIf(md protoreflect.MethodDescriptor, expr string) (*ActiveIf, erro
2021
reqMsg := registry.NewMessage(md.Input()).New()
2122
env, err := cel.NewEnv(
2223
celfakeit.Configure(),
24+
ext.Encoders(),
2325
cel.Types(reqMsg),
2426
cel.Variable("req", cel.ObjectType(string(md.Input().FullName()))),
2527
cel.Variable("service", cel.StringType),
2628
cel.Variable("method", cel.StringType),
27-
cel.Variable("procedure", cel.StringType))
29+
cel.Variable("procedure", cel.StringType),
30+
)
2831
if err != nil {
2932
return nil, err
3033
}

protocel/cel.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ package protocel
22

33
import (
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
328361
func 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

Comments
 (0)