Skip to content

Commit

Permalink
Simplify map expr
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Oct 10, 2024
1 parent 9010bd5 commit 797f001
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
2 changes: 2 additions & 0 deletions execution/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tomwright/dasel/v3/selector/ast"
)

// ExecuteSelector parses the selector and executes the resulting AST with the given input.
func ExecuteSelector(selectorStr string, value *model.Value) (*model.Value, error) {
if selectorStr == "" {
return value, nil
Expand All @@ -28,6 +29,7 @@ func ExecuteSelector(selectorStr string, value *model.Value) (*model.Value, erro

type expressionExecutor func(data *model.Value) (*model.Value, error)

// ExecuteAST executes the given AST with the given input.
func ExecuteAST(expr ast.Expr, value *model.Value) (*model.Value, error) {
if expr == nil {
return value, nil
Expand Down
9 changes: 3 additions & 6 deletions execution/execute_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ func mapExprExecutor(e ast.MapExpr) (expressionExecutor, error) {
res := model.NewSliceValue()

if err := data.RangeSlice(func(i int, item *model.Value) error {
var err error
for _, expr := range e.Exprs {
item, err = ExecuteAST(expr, item)
if err != nil {
return err
}
item, err := ExecuteAST(e.Expr, item)
if err != nil {
return err
}
if err := res.Append(item); err != nil {
return fmt.Errorf("error appending item to result: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion selector/ast/expression_complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type ObjectExpr struct {
func (ObjectExpr) expr() {}

type MapExpr struct {
Exprs Expressions
Expr Expr
}

func (MapExpr) expr() {}
Expand Down
2 changes: 1 addition & 1 deletion selector/parser/parse_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func parseMap(p *Parser) (ast.Expr, error) {
}

return ast.MapExpr{
Exprs: expressions,
Expr: ast.ChainExprs(expressions...),
}, nil
}

Expand Down
14 changes: 5 additions & 9 deletions selector/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ func TestParser_Parse_HappyPath(t *testing.T) {
expected: ast.ChainExprs(
ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
ast.MapExpr{
Exprs: ast.Expressions{
ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
},
Expr: ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
},
),
}))
Expand All @@ -278,12 +276,10 @@ func TestParser_Parse_HappyPath(t *testing.T) {
expected: ast.ChainExprs(
ast.PropertyExpr{Property: ast.StringExpr{Value: "foo"}},
ast.MapExpr{
Exprs: ast.Expressions{
ast.ChainExprs(
ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
ast.PropertyExpr{Property: ast.StringExpr{Value: "y"}},
),
},
Expr: ast.ChainExprs(
ast.PropertyExpr{Property: ast.StringExpr{Value: "x"}},
ast.PropertyExpr{Property: ast.StringExpr{Value: "y"}},
),
},
),
}))
Expand Down

0 comments on commit 797f001

Please sign in to comment.