| 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 |
"math" |
| 20 |
"reflect" |
| 21 |
"sort" |
| 22 |
"strings" |
| 23 |
"unicode" |
| 24 |
|
| 25 |
"github.com/google/cel-go/common/types/ref" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
unspecifiedAttribute = &AttributeTrail{qualifierPath: []any{}} |
| 30 |
) |
| 31 |
|
| 32 |
// NewAttributeTrail creates a new simple attribute from a variable name. |
| 33 |
func NewAttributeTrail(variable string) *AttributeTrail { |
| 34 |
if variable == "" { |
| 35 |
return unspecifiedAttribute |
| 36 |
} |
| 37 |
return &AttributeTrail{variable: variable} |
| 38 |
} |
| 39 |
|
| 40 |
// AttributeTrail specifies a variable with an optional qualifier path. An attribute value is expected to |
| 41 |
// correspond to an AbsoluteAttribute, meaning a field selection which starts with a top-level variable. |
| 42 |
// |
| 43 |
// The qualifer path elements adhere to the AttributeQualifier type constraint. |
| 44 |
type AttributeTrail struct { |
| 45 |
variable string |
| 46 |
qualifierPath []any |
| 47 |
} |
| 48 |
|
| 49 |
// Equal returns whether two attribute values have the same variable name and qualifier paths. |
| 50 |
func (a *AttributeTrail) Equal(other *AttributeTrail) bool { |
| 51 |
if a.Variable() != other.Variable() || len(a.QualifierPath()) != len(other.QualifierPath()) { |
| 52 |
return false |
| 53 |
} |
| 54 |
for i, q := range a.QualifierPath() { |
| 55 |
qual := other.QualifierPath()[i] |
| 56 |
if !qualifiersEqual(q, qual) { |
| 57 |
return false |
| 58 |
} |
| 59 |
} |
| 60 |
return true |
| 61 |
} |
| 62 |
|
| 63 |
func qualifiersEqual(a, b any) bool { |
| 64 |
if a == b { |
| 65 |
return true |
| 66 |
} |
| 67 |
switch numA := a.(type) { |
| 68 |
case int64: |
| 69 |
numB, ok := b.(uint64) |
| 70 |
if !ok { |
| 71 |
return false |
| 72 |
} |
| 73 |
return intUintEqual(numA, numB) |
| 74 |
case uint64: |
| 75 |
numB, ok := b.(int64) |
| 76 |
if !ok { |
| 77 |
return false |
| 78 |
} |
| 79 |
return intUintEqual(numB, numA) |
| 80 |
default: |
| 81 |
return false |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func intUintEqual(i int64, u uint64) bool { |
| 86 |
if i < 0 || u > math.MaxInt64 { |
| 87 |
return false |
| 88 |
} |
| 89 |
return i == int64(u) |
| 90 |
} |
| 91 |
|
| 92 |
// Variable returns the variable name associated with the attribute. |
| 93 |
func (a *AttributeTrail) Variable() string { |
| 94 |
return a.variable |
| 95 |
} |
| 96 |
|
| 97 |
// QualifierPath returns the optional set of qualifying fields or indices applied to the variable. |
| 98 |
func (a *AttributeTrail) QualifierPath() []any { |
| 99 |
return a.qualifierPath |
| 100 |
} |
| 101 |
|
| 102 |
// String returns the string representation of the Attribute. |
| 103 |
func (a *AttributeTrail) String() string { |
| 104 |
if a.variable == "" { |
| 105 |
return "<unspecified>" |
| 106 |
} |
| 107 |
var str strings.Builder |
| 108 |
str.WriteString(a.variable) |
| 109 |
for _, q := range a.qualifierPath { |
| 110 |
switch q := q.(type) { |
| 111 |
case bool, int64: |
| 112 |
str.WriteString(fmt.Sprintf("[%v]", q)) |
| 113 |
case uint64: |
| 114 |
str.WriteString(fmt.Sprintf("[%vu]", q)) |
| 115 |
case string: |
| 116 |
if isIdentifierCharacter(q) { |
| 117 |
str.WriteString(fmt.Sprintf(".%v", q)) |
| 118 |
} else { |
| 119 |
str.WriteString(fmt.Sprintf("[%q]", q)) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
return str.String() |
| 124 |
} |
| 125 |
|
| 126 |
func isIdentifierCharacter(str string) bool { |
| 127 |
for _, c := range str { |
| 128 |
if unicode.IsLetter(c) || unicode.IsDigit(c) || string(c) == "_" { |
| 129 |
continue |
| 130 |
} |
| 131 |
return false |
| 132 |
} |
| 133 |
return true |
| 134 |
} |
| 135 |
|
| 136 |
// AttributeQualifier constrains the possible types which may be used to qualify an attribute. |
| 137 |
type AttributeQualifier interface { |
| 138 |
bool | int64 | uint64 | string |
| 139 |
} |
| 140 |
|
| 141 |
// QualifyAttribute qualifies an attribute using a valid AttributeQualifier type. |
| 142 |
func QualifyAttribute[T AttributeQualifier](attr *AttributeTrail, qualifier T) *AttributeTrail { |
| 143 |
attr.qualifierPath = append(attr.qualifierPath, qualifier) |
| 144 |
return attr |
| 145 |
} |
| 146 |
|
| 147 |
// Unknown type which collects expression ids which caused the current value to become unknown. |
| 148 |
type Unknown struct { |
| 149 |
attributeTrails map[int64][]*AttributeTrail |
| 150 |
} |
| 151 |
|
| 152 |
// NewUnknown creates a new unknown at a given expression id for an attribute. |
| 153 |
// |
| 154 |
// If the attribute is nil, the attribute value will be the `unspecifiedAttribute`. |
| 155 |
func NewUnknown(id int64, attr *AttributeTrail) *Unknown { |
| 156 |
if attr == nil { |
| 157 |
attr = unspecifiedAttribute |
| 158 |
} |
| 159 |
return &Unknown{ |
| 160 |
attributeTrails: map[int64][]*AttributeTrail{id: {attr}}, |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// IDs returns the set of unknown expression ids contained by this value. |
| 165 |
// |
| 166 |
// Numeric identifiers are guaranteed to be in sorted order. |
| 167 |
func (u *Unknown) IDs() []int64 { |
| 168 |
ids := make(int64Slice, len(u.attributeTrails)) |
| 169 |
i := 0 |
| 170 |
for id := range u.attributeTrails { |
| 171 |
ids[i] = id |
| 172 |
i++ |
| 173 |
} |
| 174 |
ids.Sort() |
| 175 |
return ids |
| 176 |
} |
| 177 |
|
| 178 |
// GetAttributeTrails returns the attribute trails, if present, missing for a given expression id. |
| 179 |
func (u *Unknown) GetAttributeTrails(id int64) ([]*AttributeTrail, bool) { |
| 180 |
trails, found := u.attributeTrails[id] |
| 181 |
return trails, found |
| 182 |
} |
| 183 |
|
| 184 |
// Contains returns true if the input unknown is a subset of the current unknown. |
| 185 |
func (u *Unknown) Contains(other *Unknown) bool { |
| 186 |
for id, otherTrails := range other.attributeTrails { |
| 187 |
trails, found := u.attributeTrails[id] |
| 188 |
if !found || len(otherTrails) != len(trails) { |
| 189 |
return false |
| 190 |
} |
| 191 |
for _, ot := range otherTrails { |
| 192 |
found := false |
| 193 |
for _, t := range trails { |
| 194 |
if t.Equal(ot) { |
| 195 |
found = true |
| 196 |
break |
| 197 |
} |
| 198 |
} |
| 199 |
if !found { |
| 200 |
return false |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
return true |
| 205 |
} |
| 206 |
|
| 207 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 208 |
func (u *Unknown) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 209 |
return u.Value(), nil |
| 210 |
} |
| 211 |
|
| 212 |
// ConvertToType is an identity function since unknown values cannot be modified. |
| 213 |
func (u *Unknown) ConvertToType(typeVal ref.Type) ref.Val { |
| 214 |
return u |
| 215 |
} |
| 216 |
|
| 217 |
// Equal is an identity function since unknown values cannot be modified. |
| 218 |
func (u *Unknown) Equal(other ref.Val) ref.Val { |
| 219 |
return u |
| 220 |
} |
| 221 |
|
| 222 |
// String implements the Stringer interface |
| 223 |
func (u *Unknown) String() string { |
| 224 |
var str strings.Builder |
| 225 |
for id, attrs := range u.attributeTrails { |
| 226 |
if str.Len() != 0 { |
| 227 |
str.WriteString(", ") |
| 228 |
} |
| 229 |
if len(attrs) == 1 { |
| 230 |
str.WriteString(fmt.Sprintf("%v (%d)", attrs[0], id)) |
| 231 |
} else { |
| 232 |
str.WriteString(fmt.Sprintf("%v (%d)", attrs, id)) |
| 233 |
} |
| 234 |
} |
| 235 |
return str.String() |
| 236 |
} |
| 237 |
|
| 238 |
// Type implements ref.Val.Type. |
| 239 |
func (u *Unknown) Type() ref.Type { |
| 240 |
return UnknownType |
| 241 |
} |
| 242 |
|
| 243 |
// Value implements ref.Val.Value. |
| 244 |
func (u *Unknown) Value() any { |
| 245 |
return u |
| 246 |
} |
| 247 |
|
| 248 |
// IsUnknown returns whether the element ref.Val is in instance of *types.Unknown |
| 249 |
func IsUnknown(val ref.Val) bool { |
| 250 |
switch val.(type) { |
| 251 |
case *Unknown: |
| 252 |
return true |
| 253 |
default: |
| 254 |
return false |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// MaybeMergeUnknowns determines whether an input value and another, possibly nil, unknown will produce |
| 259 |
// an unknown result. |
| 260 |
// |
| 261 |
// If the input `val` is another Unknown, then the result will be the merge of the `val` and the input |
| 262 |
// `unk`. If the `val` is not unknown, then the result will depend on whether the input `unk` is nil. |
| 263 |
// If both values are non-nil and unknown, then the return value will be a merge of both unknowns. |
| 264 |
func MaybeMergeUnknowns(val ref.Val, unk *Unknown) (*Unknown, bool) { |
| 265 |
src, isUnk := val.(*Unknown) |
| 266 |
if !isUnk { |
| 267 |
if unk != nil { |
| 268 |
return unk, true |
| 269 |
} |
| 270 |
return unk, false |
| 271 |
} |
| 272 |
return MergeUnknowns(src, unk), true |
| 273 |
} |
| 274 |
|
| 275 |
// MergeUnknowns combines two unknown values into a new unknown value. |
| 276 |
func MergeUnknowns(unk1, unk2 *Unknown) *Unknown { |
| 277 |
if unk1 == nil { |
| 278 |
return unk2 |
| 279 |
} |
| 280 |
if unk2 == nil { |
| 281 |
return unk1 |
| 282 |
} |
| 283 |
out := &Unknown{ |
| 284 |
attributeTrails: make(map[int64][]*AttributeTrail, len(unk1.attributeTrails)+len(unk2.attributeTrails)), |
| 285 |
} |
| 286 |
for id, ats := range unk1.attributeTrails { |
| 287 |
out.attributeTrails[id] = ats |
| 288 |
} |
| 289 |
for id, ats := range unk2.attributeTrails { |
| 290 |
existing, found := out.attributeTrails[id] |
| 291 |
if !found { |
| 292 |
out.attributeTrails[id] = ats |
| 293 |
continue |
| 294 |
} |
| 295 |
|
| 296 |
for _, at := range ats { |
| 297 |
found := false |
| 298 |
for _, et := range existing { |
| 299 |
if at.Equal(et) { |
| 300 |
found = true |
| 301 |
break |
| 302 |
} |
| 303 |
} |
| 304 |
if !found { |
| 305 |
existing = append(existing, at) |
| 306 |
} |
| 307 |
} |
| 308 |
out.attributeTrails[id] = existing |
| 309 |
} |
| 310 |
return out |
| 311 |
} |
| 312 |
|
| 313 |
// int64Slice is an implementation of the sort.Interface |
| 314 |
type int64Slice []int64 |
| 315 |
|
| 316 |
// Len returns the number of elements in the slice. |
| 317 |
func (x int64Slice) Len() int { return len(x) } |
| 318 |
|
| 319 |
// Less indicates whether the value at index i is less than the value at index j. |
| 320 |
func (x int64Slice) Less(i, j int) bool { return x[i] < x[j] } |
| 321 |
|
| 322 |
// Swap swaps the values at indices i and j in place. |
| 323 |
func (x int64Slice) Swap(i, j int) { x[i], x[j] = x[j], x[i] } |
| 324 |
|
| 325 |
// Sort is a convenience method: x.Sort() calls Sort(x). |
| 326 |
func (x int64Slice) Sort() { sort.Sort(x) } |