-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
293 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,31 @@ | ||
// Package dasel contains everything you'll need to use dasel from a go application. | ||
package dasel | ||
|
||
func Select(data any, selector string) any { | ||
panic("not implemented") | ||
import ( | ||
"github.com/tomwright/dasel/v3/execution" | ||
"github.com/tomwright/dasel/v3/model" | ||
) | ||
|
||
func Select(data any, selector string) (any, error) { | ||
val := model.NewValue(data) | ||
res, err := execution.ExecuteSelector(selector, val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return res.Interface(), nil | ||
} | ||
|
||
func Modify(data any, selector string, newValue any) error { | ||
val := model.NewValue(data) | ||
newVal := model.NewValue(newValue) | ||
res, err := execution.ExecuteSelector(selector, val) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := res.Set(newVal); err != nil { | ||
return err | ||
} | ||
|
||
return 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,32 @@ | ||
package dasel_test | ||
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/tomwright/dasel/v3" | ||
"testing" | ||
) | ||
|
||
type modifyTestCase struct { | ||
selector string | ||
in any | ||
value any | ||
exp any | ||
} | ||
|
||
func (tc modifyTestCase) run(t *testing.T) { | ||
if err := dasel.Modify(&tc.in, "[1]", 4); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if !cmp.Equal(tc.exp, tc.in) { | ||
t.Errorf("unexpected result: %s", cmp.Diff(tc.exp, tc.in)) | ||
} | ||
} | ||
|
||
func TestModify(t *testing.T) { | ||
t.Run("int over int", modifyTestCase{ | ||
selector: "[1]", | ||
in: []int{1, 2, 3}, | ||
value: 4, | ||
exp: []int{1, 4, 3}, | ||
}.run) | ||
} |
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,28 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tomwright/dasel/v3/model" | ||
"github.com/tomwright/dasel/v3/selector/ast" | ||
) | ||
|
||
func branchExprExecutor(e ast.BranchExpr) (expressionExecutor, error) { | ||
return func(data *model.Value) (*model.Value, error) { | ||
res := model.NewSliceValue() | ||
|
||
for _, expr := range e.Exprs { | ||
r, err := ExecuteAST(expr, data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute branch expr: %w", err) | ||
} | ||
if err := res.Append(r); err != nil { | ||
return nil, fmt.Errorf("failed to append branch result: %w", err) | ||
} | ||
} | ||
|
||
res.MarkAsBranch() | ||
|
||
return res, nil | ||
}, 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
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,21 @@ | ||
package model | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func (v *Value) Set(newValue *Value) error { | ||
a := v.UnpackKinds(reflect.Ptr, reflect.Interface) | ||
b := newValue.UnpackKinds(reflect.Ptr, reflect.Interface) | ||
|
||
if a.Kind() == b.Kind() { | ||
a.Value.Set(b.Value) | ||
return nil | ||
} | ||
|
||
x := newPtr() | ||
x.Elem().Set(b.Value) | ||
v.Value.Set(x) | ||
|
||
return 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
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
Oops, something went wrong.