Skip to content

Commit

Permalink
General fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Oct 30, 2024
1 parent d67cb15 commit 11c723e
Show file tree
Hide file tree
Showing 11 changed files with 539 additions and 261 deletions.
3 changes: 3 additions & 0 deletions execution/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func ExecuteAST(expr ast.Expr, value *model.Value, options *Options) (*model.Val
if err != nil {
return err
}
if r.IsIgnore() {
return nil
}
return res.Append(r)
}); err != nil {
return nil, fmt.Errorf("branch execution error: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions execution/execute_func.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package execution

import (
"errors"
"fmt"
"slices"

"github.com/tomwright/dasel/v3/model"
"github.com/tomwright/dasel/v3/selector/ast"
Expand Down Expand Up @@ -41,7 +43,14 @@ func callFnExecutor(opts *Options, f FuncFn, argsE ast.Expressions) (expressionE
}, nil
}

var unstableFuncs = []string{
"ignore",
}

func callExprExecutor(opts *Options, e ast.CallExpr) (expressionExecutor, error) {
if !opts.Unstable && (slices.Contains(unstableFuncs, e.Function)) {
return nil, errors.New("unstable function are not enabled. to enable them use --unstable")
}
if f, ok := opts.Funcs.Get(e.Function); ok {
res, err := callFnExecutor(opts, f, e.Args)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions execution/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var (
FuncTypeOf,
FuncMax,
FuncMin,
FuncIgnore,
FuncBase64Encode,
FuncBase64Decode,
FuncParse,
)
)

Expand Down
40 changes: 40 additions & 0 deletions execution/func_base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package execution

import (
"encoding/base64"

"github.com/tomwright/dasel/v3/model"
)

// FuncBase64Encode base64 encodes the given value.
var FuncBase64Encode = NewFunc(
"base64e",
func(data *model.Value, args model.Values) (*model.Value, error) {
arg := args[0]
strVal, err := arg.StringValue()
if err != nil {
return nil, err
}
out := base64.StdEncoding.EncodeToString([]byte(strVal))
return model.NewStringValue(out), nil
},
ValidateArgsExactly(1),
)

// FuncBase64Decode base64 decodes the given value.
var FuncBase64Decode = NewFunc(
"base64d",
func(data *model.Value, args model.Values) (*model.Value, error) {
arg := args[0]
strVal, err := arg.StringValue()
if err != nil {
return nil, err
}
out, err := base64.StdEncoding.DecodeString(strVal)
if err != nil {
return nil, err
}
return model.NewStringValue(string(out)), nil
},
ValidateArgsExactly(1),
)
15 changes: 15 additions & 0 deletions execution/func_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package execution

import (
"github.com/tomwright/dasel/v3/model"
)

// FuncIgnore is a function that ignores the value, causing it to be rejected from a branch.
var FuncIgnore = NewFunc(
"ignore",
func(data *model.Value, args model.Values) (*model.Value, error) {
data.MarkAsIgnore()
return data, nil
},
ValidateArgsExactly(0),
)
42 changes: 42 additions & 0 deletions execution/func_parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package execution

import (
"github.com/tomwright/dasel/v3/model"
"github.com/tomwright/dasel/v3/parsing"
)

// FuncParse parses the given data at runtime.
var FuncParse = NewFunc(
"parse",
func(data *model.Value, args model.Values) (*model.Value, error) {
var format parsing.Format
var content []byte
{
strVal, err := args[0].StringValue()
if err != nil {
return nil, err
}
format = parsing.Format(strVal)
}
{
strVal, err := args[1].StringValue()
if err != nil {
return nil, err
}
content = []byte(strVal)
}

reader, err := format.NewReader(parsing.DefaultReaderOptions())
if err != nil {
return nil, err
}

doc, err := reader.Read(content)
if err != nil {
return nil, err
}

return doc, nil
},
ValidateArgsExactly(2),
)
Loading

0 comments on commit 11c723e

Please sign in to comment.