MCPcopy Create free account
hub / github.com/cel-expr/cel-go / refQualify

Function refQualify

interpreter/attributes.go:1574–1636  ·  view source on GitHub ↗

refQualify attempts to convert the value to a CEL value and then uses reflection methods to try and apply the qualifier with the option to presence test field accesses before retrieving field values.

(adapter types.Adapter, obj any, idx ref.Val, presenceTest, presenceOnly, errorOnBadPresenceTest bool)

Source from the content-addressed store, hash-verified

1572// refQualify attempts to convert the value to a CEL value and then uses reflection methods to try and
1573// apply the qualifier with the option to presence test field accesses before retrieving field values.
1574func refQualify(adapter types.Adapter, obj any, idx ref.Val, presenceTest, presenceOnly, errorOnBadPresenceTest bool) (ref.Val, bool, error) {
1575 celVal := adapter.NativeToValue(obj)
1576 switch v := celVal.(type) {
1577 case *types.Unknown:
1578 return v, true, nil
1579 case *types.Err:
1580 return nil, false, v
1581 case traits.Mapper:
1582 val, found := v.Find(idx)
1583 // If the index is of the wrong type for the map, then it is possible
1584 // for the Find call to produce an error.
1585 if isError(val) {
1586 return nil, false, val.(*types.Err)
1587 }
1588 if found {
1589 return val, true, nil
1590 }
1591 if presenceTest {
1592 return nil, false, nil
1593 }
1594 return nil, false, missingKey(idx)
1595 case traits.Lister:
1596 // If the index argument is not a valid numeric type, then it is possible
1597 // for the index operation to produce an error.
1598 i, err := types.GetOrError(idx)
1599 if err != nil {
1600 return nil, false, err
1601 }
1602 celIndex := types.Int(i)
1603 if i >= 0 && celIndex < v.Size().(types.Int) {
1604 return v.Get(idx), true, nil
1605 }
1606 if presenceTest {
1607 return nil, false, nil
1608 }
1609 return nil, false, missingIndex(idx)
1610 case traits.Indexer:
1611 if presenceTest {
1612 ft, ok := v.(traits.FieldTester)
1613 if ok {
1614 presence := ft.IsSet(idx)
1615 if isError(presence) {
1616 return nil, false, presence.(*types.Err)
1617 }
1618 // If not found or presence only test, then return.
1619 // Otherwise, if found, obtain the value later on.
1620 if presenceOnly || presence == types.False {
1621 return nil, presence == types.True, nil
1622 }
1623 }
1624 }
1625 val := v.Get(idx)
1626 if isError(val) {
1627 return nil, false, val.(*types.Err)
1628 }
1629 return val, true, nil
1630 default:
1631 if presenceTest && !errorOnBadPresenceTest {

Callers 5

qualifyInternalMethod · 0.85
qualifyInternalMethod · 0.85
qualifyInternalMethod · 0.85
qualifyInternalMethod · 0.85
qualifyInternalMethod · 0.85

Calls 10

GetOrErrorFunction · 0.92
IntTypeAlias · 0.92
missingKeyFunction · 0.85
missingIndexFunction · 0.85
isErrorFunction · 0.70
NativeToValueMethod · 0.65
FindMethod · 0.65
SizeMethod · 0.65
GetMethod · 0.65
IsSetMethod · 0.65

Tested by

no test coverage detected