| 1 |
// Copyright 2018 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package types |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"sort" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"google.golang.org/protobuf/encoding/protojson" |
| 24 |
"google.golang.org/protobuf/proto" |
| 25 |
"google.golang.org/protobuf/reflect/protoreflect" |
| 26 |
|
| 27 |
"github.com/google/cel-go/common/types/pb" |
| 28 |
"github.com/google/cel-go/common/types/ref" |
| 29 |
|
| 30 |
anypb "google.golang.org/protobuf/types/known/anypb" |
| 31 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 32 |
) |
| 33 |
|
| 34 |
type protoObj struct { |
| 35 |
Adapter |
| 36 |
value proto.Message |
| 37 |
typeDesc *pb.TypeDescription |
| 38 |
typeValue ref.Val |
| 39 |
} |
| 40 |
|
| 41 |
// NewObject returns an object based on a proto.Message value which handles |
| 42 |
// conversion between protobuf type values and expression type values. |
| 43 |
// Objects support indexing and iteration. |
| 44 |
// |
| 45 |
// Note: the type value is pulled from the list of registered types within the |
| 46 |
// type provider. If the proto type is not registered within the type provider, |
| 47 |
// then this will result in an error within the type adapter / provider. |
| 48 |
func NewObject(adapter Adapter, |
| 49 |
typeDesc *pb.TypeDescription, |
| 50 |
typeValue ref.Val, |
| 51 |
value proto.Message) ref.Val { |
| 52 |
return &protoObj{ |
| 53 |
Adapter: adapter, |
| 54 |
value: value, |
| 55 |
typeDesc: typeDesc, |
| 56 |
typeValue: typeValue} |
| 57 |
} |
| 58 |
|
| 59 |
func (o *protoObj) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 60 |
srcPB := o.value |
| 61 |
if reflect.TypeOf(srcPB).AssignableTo(typeDesc) { |
| 62 |
return srcPB, nil |
| 63 |
} |
| 64 |
if reflect.TypeOf(o).AssignableTo(typeDesc) { |
| 65 |
return o, nil |
| 66 |
} |
| 67 |
switch typeDesc { |
| 68 |
case anyValueType: |
| 69 |
_, isAny := srcPB.(*anypb.Any) |
| 70 |
if isAny { |
| 71 |
return srcPB, nil |
| 72 |
} |
| 73 |
return anypb.New(srcPB) |
| 74 |
case JSONValueType: |
| 75 |
// Marshal the proto to JSON first, and then rehydrate as protobuf.Value as there is no |
| 76 |
// support for direct conversion from proto.Message to protobuf.Value. |
| 77 |
bytes, err := protojson.Marshal(srcPB) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
json := &structpb.Value{} |
| 82 |
err = protojson.Unmarshal(bytes, json) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
return json, nil |
| 87 |
default: |
| 88 |
if typeDesc == o.typeDesc.ReflectType() { |
| 89 |
return o.value, nil |
| 90 |
} |
| 91 |
if typeDesc.Kind() == reflect.Ptr { |
| 92 |
val := reflect.New(typeDesc.Elem()).Interface() |
| 93 |
dstPB, ok := val.(proto.Message) |
| 94 |
if ok { |
| 95 |
err := pb.Merge(dstPB, srcPB) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("type conversion error: %v", err) |
| 98 |
} |
| 99 |
return dstPB, nil |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
return nil, fmt.Errorf("type conversion error from '%T' to '%v'", o.value, typeDesc) |
| 104 |
} |
| 105 |
|
| 106 |
func (o *protoObj) ConvertToType(typeVal ref.Type) ref.Val { |
| 107 |
switch typeVal { |
| 108 |
default: |
| 109 |
if o.Type().TypeName() == typeVal.TypeName() { |
| 110 |
return o |
| 111 |
} |
| 112 |
case TypeType: |
| 113 |
return o.typeValue |
| 114 |
} |
| 115 |
return NewErr("type conversion error from '%s' to '%s'", o.typeDesc.Name(), typeVal) |
| 116 |
} |
| 117 |
|
| 118 |
func (o *protoObj) Equal(other ref.Val) ref.Val { |
| 119 |
otherPB, ok := other.Value().(proto.Message) |
| 120 |
return Bool(ok && pb.Equal(o.value, otherPB)) |
| 121 |
} |
| 122 |
|
| 123 |
// IsSet tests whether a field which is defined is set to a non-default value. |
| 124 |
func (o *protoObj) IsSet(field ref.Val) ref.Val { |
| 125 |
protoFieldName, ok := field.(String) |
| 126 |
if !ok { |
| 127 |
return MaybeNoSuchOverloadErr(field) |
| 128 |
} |
| 129 |
protoFieldStr := string(protoFieldName) |
| 130 |
fd, found := o.typeDesc.FieldByName(protoFieldStr) |
| 131 |
if !found { |
| 132 |
return NewErr("no such field '%s'", field) |
| 133 |
} |
| 134 |
if fd.IsSet(o.value) { |
| 135 |
return True |
| 136 |
} |
| 137 |
return False |
| 138 |
} |
| 139 |
|
| 140 |
// IsZeroValue returns true if the protobuf object is empty. |
| 141 |
func (o *protoObj) IsZeroValue() bool { |
| 142 |
return proto.Equal(o.value, o.typeDesc.Zero()) |
| 143 |
} |
| 144 |
|
| 145 |
func (o *protoObj) Get(index ref.Val) ref.Val { |
| 146 |
protoFieldName, ok := index.(String) |
| 147 |
if !ok { |
| 148 |
return MaybeNoSuchOverloadErr(index) |
| 149 |
} |
| 150 |
protoFieldStr := string(protoFieldName) |
| 151 |
fd, found := o.typeDesc.FieldByName(protoFieldStr) |
| 152 |
if !found { |
| 153 |
return NewErr("no such field '%s'", index) |
| 154 |
} |
| 155 |
fv, err := fd.GetFrom(o.value) |
| 156 |
if err != nil { |
| 157 |
return NewErrFromString(err.Error()) |
| 158 |
} |
| 159 |
return o.NativeToValue(fv) |
| 160 |
} |
| 161 |
|
| 162 |
func (o *protoObj) Type() ref.Type { |
| 163 |
return o.typeValue.(ref.Type) |
| 164 |
} |
| 165 |
|
| 166 |
func (o *protoObj) Value() any { |
| 167 |
return o.value |
| 168 |
} |
| 169 |
|
| 170 |
type protoObjField struct { |
| 171 |
fd protoreflect.FieldDescriptor |
| 172 |
v protoreflect.Value |
| 173 |
} |
| 174 |
|
| 175 |
func (o *protoObj) format(sb *strings.Builder) { |
| 176 |
var fields []protoreflect.FieldDescriptor |
| 177 |
o.value.ProtoReflect().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { |
| 178 |
fields = append(fields, fd) |
| 179 |
return true |
| 180 |
}) |
| 181 |
sort.SliceStable(fields, func(i, j int) bool { |
| 182 |
return fields[i].Number() < fields[j].Number() |
| 183 |
}) |
| 184 |
sb.WriteString(o.Type().TypeName()) |
| 185 |
sb.WriteString("{") |
| 186 |
for i, field := range fields { |
| 187 |
if i > 0 { |
| 188 |
sb.WriteString(", ") |
| 189 |
} |
| 190 |
sb.WriteString(fmt.Sprintf("%s: ", field.Name())) |
| 191 |
formatTo(sb, o.Get(String(field.Name()))) |
| 192 |
} |
| 193 |
sb.WriteString("}") |
| 194 |
} |