-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start getting v3 core functionality working
- Loading branch information
Showing
76 changed files
with
1,818 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
"github.com/tomwright/dasel/v3/selector/ast" | ||
"github.com/tomwright/dasel/v3/selector/lexer" | ||
"github.com/tomwright/dasel/v3/selector/parser" | ||
) | ||
|
||
func ExecuteSelector(selector string, value *model.Value) (*model.Value, error) { | ||
tokens, err := lexer.NewTokenizer(selector).Tokenize() | ||
if err != nil { | ||
return nil, fmt.Errorf("error tokenizing selector: %w", err) | ||
} | ||
|
||
expr, err := parser.NewParser(tokens).Parse() | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing selector: %w", err) | ||
} | ||
|
||
res, err := ExecuteAST(expr, value) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing selector: %w", err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
type expressionExecutor func(data *model.Value) (*model.Value, error) | ||
|
||
func ExecuteAST(expr ast.Expr, value *model.Value) (*model.Value, error) { | ||
executor, err := exprExecutor(expr) | ||
if err != nil { | ||
return nil, fmt.Errorf("error evaluating expression: %w", err) | ||
} | ||
res, err := executor(value) | ||
if err != nil { | ||
return nil, fmt.Errorf("execution error: %w", err) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func exprExecutor(expr ast.Expr) (expressionExecutor, error) { | ||
switch e := expr.(type) { | ||
case ast.BinaryExpr: | ||
return binaryExprExecutor(e) | ||
case ast.CallExpr: | ||
return callExprExecutor(e) | ||
case ast.ChainedExpr: | ||
return chainedExprExecutor(e) | ||
case ast.SpreadExpr: | ||
return spreadExprExecutor() | ||
case ast.RangeExpr: | ||
return rangeExprExecutor(e) | ||
case ast.IndexExpr: | ||
return indexExprExecutor(e) | ||
case ast.PropertyExpr: | ||
return propertyExprExecutor(e) | ||
case ast.NumberIntExpr: | ||
return numberIntExprExecutor(e) | ||
case ast.NumberFloatExpr: | ||
return numberFloatExprExecutor(e) | ||
case ast.StringExpr: | ||
return stringExprExecutor(e) | ||
case ast.BoolExpr: | ||
return boolExprExecutor(e) | ||
case ast.ObjectExpr: | ||
return objectExprExecutor(e) | ||
case ast.MapExpr: | ||
return mapExprExecutor(e) | ||
default: | ||
return nil, fmt.Errorf("unhandled expression type: %T", e) | ||
} | ||
} | ||
|
||
func binaryExprExecutor(e ast.BinaryExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
panic("not implemented") | ||
}, nil | ||
} | ||
|
||
func chainedExprExecutor(e ast.ChainedExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
for _, expr := range e.Exprs { | ||
res, err := ExecuteAST(expr, data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing expression: %w", err) | ||
} | ||
data = res | ||
} | ||
return data, nil | ||
}, nil | ||
} | ||
|
||
func spreadExprExecutor() (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
s := model.NewSliceValue() | ||
|
||
switch { | ||
case data.IsSlice(): | ||
v, err := data.SliceValue() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting slice value: %w", err) | ||
} | ||
for _, sv := range v { | ||
s.Append(model.NewValue(sv)) | ||
} | ||
case data.IsMap(): | ||
v, err := data.MapValue() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting map value: %w", err) | ||
} | ||
for _, kv := range v.KeyValues() { | ||
s.Append(model.NewValue(kv.Value)) | ||
} | ||
default: | ||
return nil, fmt.Errorf("cannot spread on type %s", data.Type()) | ||
} | ||
|
||
return s, nil | ||
}, nil | ||
} | ||
|
||
func rangeExprExecutor(e ast.RangeExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
panic("not implemented") | ||
}, nil | ||
} | ||
|
||
func indexExprExecutor(e ast.IndexExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
panic("not implemented") | ||
}, nil | ||
} | ||
|
||
func propertyExprExecutor(e ast.PropertyExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
if !data.IsMap() { | ||
return nil, fmt.Errorf("expected map, got %s", data.Type()) | ||
} | ||
key, err := ExecuteAST(e.Property, data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error evaluating property: %w", err) | ||
} | ||
if !key.IsString() { | ||
return nil, fmt.Errorf("expected property to resolve to string, got %s", key.Type()) | ||
} | ||
keyStr, err := key.StringValue() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting string value: %w", err) | ||
} | ||
return data.GetMapKey(keyStr) | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
"github.com/tomwright/dasel/v3/selector/ast" | ||
) | ||
|
||
func prepareArgs(data *model.Value, argsE ast.Expressions) (model.Values, error) { | ||
args := make(model.Values, 0) | ||
for i, arg := range argsE { | ||
res, err := ExecuteAST(arg, data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error evaluating argument %d: %w", i, err) | ||
} | ||
args = append(args, res) | ||
} | ||
return args, nil | ||
} | ||
|
||
func callSingleExecutor(f singleResponseFunc, argsE ast.Expressions) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
args, err := prepareArgs(data, argsE) | ||
if err != nil { | ||
return nil, fmt.Errorf("error preparing arguments: %w", err) | ||
} | ||
|
||
res, err := f(data, args) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing function: %w", err) | ||
} | ||
|
||
return res, nil | ||
}, nil | ||
} | ||
|
||
func callMultiExecutor(f multiResponseFunc, argsE ast.Expressions) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
panic("multi response functions are not supported") | ||
//args, err := prepareArgs(data, argsE) | ||
//if err != nil { | ||
// return nil, fmt.Errorf("error preparing arguments: %w", err) | ||
//} | ||
|
||
//res, err := f(data, args) | ||
//if err != nil { | ||
// return nil, fmt.Errorf("error executing function: %w", err) | ||
//} | ||
|
||
//return res, nil | ||
}, nil | ||
} | ||
|
||
func callExprExecutor(e ast.CallExpr) (expressionExecutor, error) { | ||
if f, ok := singleResponseFuncLookup[e.Function]; ok { | ||
return callSingleExecutor(f, e.Args) | ||
} | ||
if f, ok := multiResponseFuncLookup[e.Function]; ok { | ||
return callMultiExecutor(f, e.Args) | ||
} | ||
|
||
return nil, fmt.Errorf("unknown function: %q", e.Function) | ||
} |
Oops, something went wrong.