This repository was archived by the owner on Jan 28, 2021. It is now read-only.
This repository was archived by the owner on Jan 28, 2021. It is now read-only.
Reduce amount of implemented code on Expressions #772
Closed
Description
To reduce the amount of implemented code on Expressions and Nodes, we can heavily use UnaryExpression, BinaryExpression, and so on.
Right now, these interfaces are just implementing Children()
, Resolved()
and IsNullable()
.
We could implement also TransformUp
function as following:
- Create a new interface in charge of return a new instance of the implemented expression:
type ExpressionInstanceGenerator interface {
Generate(exprs ...sql.Expression) (sql.Expression, error)
}
- With this, we will be able to implement this interface on Unary and Binary expressions:
type BinaryExpression struct {
ExpressionInstanceGenerator
Left sql.Expression
Right sql.Expression
}
- After that,
TransformUp
method can be implemented on BinaryExpression.Generate
method is the one that will be implemented on the other Expressions:
func (p *BinaryExpression) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) {
var err error
var l sql.Expression
if p.Left != nil {
l, err = p.Left.TransformUp(f)
if err != nil {
return nil, err
}
}
var r sql.Expression
if p.Left != nil {
r, err = p.Right.TransformUp(f)
if err != nil {
return nil, err
}
}
exp, err := p.Generate(l, r)
if err != nil {
return nil, err
}
return f(exp)
}
Doing this, we will reduce bugs on bad implemented TransformUp functions, also we can create generic Tests suites for this kind of expressions.