Skip to content

Commit

Permalink
Fix panic when using a repeated field as a predefined rule
Browse files Browse the repository at this point in the history
  • Loading branch information
rodaine committed Sep 30, 2024
1 parent 4d5a829 commit d8f8217
Show file tree
Hide file tree
Showing 6 changed files with 612 additions and 605 deletions.
11 changes: 11 additions & 0 deletions celext/lookups.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package celext

import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"google.golang.org/protobuf/reflect/protoreflect"
)

Expand Down Expand Up @@ -58,6 +60,15 @@ func ProtoFieldToCELType(fieldDesc protoreflect.FieldDescriptor, generic, forIte
return protoKindToCELType(fieldDesc.Kind())
}

func ProtoFieldToCELValue(fieldDesc protoreflect.FieldDescriptor, value protoreflect.Value, forItems bool) ref.Val {
switch {
case fieldDesc.IsList() && !forItems:
return types.NewProtoList(types.DefaultTypeAdapter, value.List())
default:
return types.DefaultTypeAdapter.NativeToValue(value.Interface())
}
}

// protoKindToCELType maps a protoreflect.Kind to a compatible cel.Type.
func protoKindToCELType(kind protoreflect.Kind) *cel.Type {
switch kind {
Expand Down
3 changes: 1 addition & 2 deletions internal/constraints/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/bufbuild/protovalidate-go/internal/expression"
"github.com/bufbuild/protovalidate-go/internal/extensions"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (c *Cache) Build(
cel.Constant(
"rule",
celext.ProtoFieldToCELType(desc, true, forItems),
types.DefaultTypeAdapter.NativeToValue(rule.Interface()),
celext.ProtoFieldToCELValue(desc, rule, forItems),
),
)
if compileErr != nil {
Expand Down
Loading

0 comments on commit d8f8217

Please sign in to comment.