|
| 1 | +package sun |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "go.starlark.net/starlark" |
| 7 | +) |
| 8 | + |
| 9 | +type filterFunc = func(x starlark.Value) (starlark.Value, error) |
| 10 | + |
| 11 | +type filterIter struct { |
| 12 | + thread *starlark.Thread |
| 13 | + function filterFunc |
| 14 | + iterator starlark.Iterator |
| 15 | +} |
| 16 | + |
| 17 | +func (f filterIter) Next(p *starlark.Value) bool { |
| 18 | + var x starlark.Value |
| 19 | + for { |
| 20 | + if !f.iterator.Next(&x) { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + v, err := f.function(x) |
| 25 | + if err != nil { |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + if v.Truth() { |
| 30 | + *p = x |
| 31 | + return true |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (f filterIter) Done() { |
| 37 | + f.iterator.Done() |
| 38 | +} |
| 39 | + |
| 40 | +type filterObject struct { |
| 41 | + thread *starlark.Thread |
| 42 | + function filterFunc |
| 43 | + iterable starlark.Iterable |
| 44 | + iterator starlark.Iterator |
| 45 | +} |
| 46 | + |
| 47 | +func (f filterObject) String() string { |
| 48 | + return fmt.Sprintf("<filter object>") |
| 49 | +} |
| 50 | + |
| 51 | +func (f filterObject) Type() string { |
| 52 | + return "filter" |
| 53 | +} |
| 54 | + |
| 55 | +func (f filterObject) Freeze() { |
| 56 | + f.iterable.Freeze() |
| 57 | +} |
| 58 | + |
| 59 | +func (f filterObject) Truth() starlark.Bool { |
| 60 | + return starlark.True |
| 61 | +} |
| 62 | + |
| 63 | +func (f filterObject) Hash() (uint32, error) { |
| 64 | + return 0, fmt.Errorf("unhashable type: filter") |
| 65 | +} |
| 66 | + |
| 67 | +func (f filterObject) Iterate() starlark.Iterator { |
| 68 | + return filterIter{ |
| 69 | + thread: f.thread, |
| 70 | + function: f.function, |
| 71 | + iterator: f.iterator, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func filter( |
| 76 | + thread *starlark.Thread, |
| 77 | + b *starlark.Builtin, |
| 78 | + args starlark.Tuple, |
| 79 | + kwargs []starlark.Tuple, |
| 80 | +) (starlark.Value, error) { |
| 81 | + var ( |
| 82 | + function filterFunc |
| 83 | + iterable starlark.Iterable |
| 84 | + ) |
| 85 | + if err := wantArgs(b.Name(), args, kwargs, 2); err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + switch fn := args[0].(type) { |
| 90 | + case starlark.Callable: |
| 91 | + function = func(x starlark.Value) (starlark.Value, error) { |
| 92 | + return starlark.Call(thread, fn, starlark.Tuple{x}, nil) |
| 93 | + } |
| 94 | + case starlark.NoneType: |
| 95 | + function = func(x starlark.Value) (starlark.Value, error) { |
| 96 | + return x.Truth(), nil |
| 97 | + } |
| 98 | + default: |
| 99 | + return nil, fmt.Errorf("got %s, want callable", fn.Type()) |
| 100 | + } |
| 101 | + |
| 102 | + iterable, ok := args[1].(starlark.Iterable) |
| 103 | + if !ok { |
| 104 | + return nil, fmt.Errorf("got %s, want iterable", args[1].Type()) |
| 105 | + } |
| 106 | + |
| 107 | + return &filterObject{ |
| 108 | + thread: thread, |
| 109 | + function: function, |
| 110 | + iterable: iterable, |
| 111 | + iterator: iterable.Iterate(), |
| 112 | + }, nil |
| 113 | +} |
0 commit comments