-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_tuple_project.go
50 lines (42 loc) · 1.13 KB
/
expr_tuple_project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package rel
import (
"fmt"
"strings"
"github.com/arr-ai/wbnf/parser"
"github.com/go-errors/errors"
)
type TupleProjectExpr struct {
ExprScanner
base Expr
inverse bool
attrs Names
}
func NewTupleProjectExpr(scanner parser.Scanner, base Expr, inverse bool, attrs []string) Expr {
return &TupleProjectExpr{ExprScanner{scanner}, base, inverse, NewNames(attrs...)}
}
func (tp *TupleProjectExpr) Eval(local Scope) (Value, error) {
val, err := tp.base.Eval(local)
if err != nil {
return nil, WrapContext(err, tp, local)
}
tuple, isTuple := val.(Tuple)
if !isTuple {
return nil, WrapContext(errors.Errorf("lhs does not evaluate to tuple: %s", val), tp, local)
}
if !tp.attrs.IsSubsetOf(tuple.Names()) {
return nil, WrapContext(errors.Errorf("names are not subset of lhs: %s", tuple.Names()), tp, local)
}
if tp.inverse {
return TupleProjectAllBut(tuple, tp.attrs), nil
}
return tuple.Project(tp.attrs), nil
}
func (tp *TupleProjectExpr) String() string {
str := strings.Builder{}
str.WriteString(fmt.Sprintf("(%s).", tp.base))
if tp.inverse {
str.WriteRune('~')
}
str.WriteString(tp.attrs.String())
return str.String()
}