| 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 |
|
| 21 |
"github.com/google/cel-go/common/types/ref" |
| 22 |
"github.com/google/cel-go/common/types/traits" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
// IteratorType singleton. |
| 27 |
IteratorType = NewObjectType("iterator", traits.IteratorType) |
| 28 |
) |
| 29 |
|
| 30 |
// baseIterator is the basis for list, map, and object iterators. |
| 31 |
// |
| 32 |
// An iterator in and of itself should not be a valid value for comparison, but must implement the |
| 33 |
// `ref.Val` methods in order to be well-supported within instruction arguments processed by the |
| 34 |
// interpreter. |
| 35 |
type baseIterator struct{} |
| 36 |
|
| 37 |
func (*baseIterator) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 38 |
return nil, fmt.Errorf("type conversion on iterators not supported") |
| 39 |
} |
| 40 |
|
| 41 |
func (*baseIterator) ConvertToType(typeVal ref.Type) ref.Val { |
| 42 |
return NewErr("no such overload") |
| 43 |
} |
| 44 |
|
| 45 |
func (*baseIterator) Equal(other ref.Val) ref.Val { |
| 46 |
return NewErr("no such overload") |
| 47 |
} |
| 48 |
|
| 49 |
func (*baseIterator) Type() ref.Type { |
| 50 |
return IteratorType |
| 51 |
} |
| 52 |
|
| 53 |
func (*baseIterator) Value() any { |
| 54 |
return nil |
| 55 |
} |