|
| 1 | +package expressions |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "maps" |
| 6 | + "reflect" |
| 7 | + "slices" |
| 8 | + |
| 9 | + "github.com/google/cel-go/common/types" |
| 10 | + "github.com/google/cel-go/common/types/ref" |
| 11 | + "github.com/google/cel-go/common/types/traits" |
| 12 | +) |
| 13 | + |
| 14 | +var ErrNotImplemented = errors.New("expressions: not implemented") |
| 15 | + |
| 16 | +type stringSliceIterator struct { |
| 17 | + keys []string |
| 18 | + idx int |
| 19 | +} |
| 20 | + |
| 21 | +func (s *stringSliceIterator) Value() any { |
| 22 | + return s |
| 23 | +} |
| 24 | + |
| 25 | +func (s *stringSliceIterator) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 26 | + return nil, ErrNotImplemented |
| 27 | +} |
| 28 | + |
| 29 | +func (s *stringSliceIterator) ConvertToType(typeValue ref.Type) ref.Val { |
| 30 | + return types.NewErr("can't convert from %q to %q", types.IteratorType, typeValue) |
| 31 | +} |
| 32 | + |
| 33 | +func (s *stringSliceIterator) Equal(other ref.Val) ref.Val { |
| 34 | + return types.NewErr("can't compare %q to %q", types.IteratorType, other.Type()) |
| 35 | +} |
| 36 | + |
| 37 | +func (s *stringSliceIterator) Type() ref.Type { |
| 38 | + return types.IteratorType |
| 39 | +} |
| 40 | + |
| 41 | +func (s *stringSliceIterator) HasNext() ref.Val { |
| 42 | + return types.Bool(s.idx < len(s.keys)) |
| 43 | +} |
| 44 | + |
| 45 | +func (s *stringSliceIterator) Next() ref.Val { |
| 46 | + if s.HasNext() != types.True { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + val := s.keys[s.idx] |
| 51 | + s.idx++ |
| 52 | + return types.String(val) |
| 53 | +} |
| 54 | + |
| 55 | +func newMapIterator(m map[string][]string) traits.Iterator { |
| 56 | + return &stringSliceIterator{ |
| 57 | + keys: slices.Collect(maps.Keys(m)), |
| 58 | + idx: 0, |
| 59 | + } |
| 60 | +} |
0 commit comments