-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathapi.go
More file actions
64 lines (58 loc) · 1.83 KB
/
api.go
File metadata and controls
64 lines (58 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package dasel contains everything you'll need to use dasel from a go application.
package dasel
import (
"context"
"github.com/tomwright/dasel/v3/execution"
"github.com/tomwright/dasel/v3/model"
)
// Query queries the data using the selector and returns the results.
func Query(ctx context.Context, data any, selector string, opts ...execution.ExecuteOptionFn) ([]*model.Value, int, error) {
options := execution.NewOptions(opts...)
val := model.NewValue(data)
out, err := execution.ExecuteSelector(ctx, selector, val, options)
if err != nil {
return nil, 0, err
}
if out.IsBranch() || out.IsSpread() {
res := make([]*model.Value, 0)
if err := out.RangeSlice(func(i int, v *model.Value) error {
res = append(res, v)
return nil
}); err != nil {
return nil, 0, err
}
return res, len(res), nil
}
return []*model.Value{out}, 1, nil
}
// Select queries the data using the selector and returns the results as native Go types.
// Ordering within maps is not guaranteed.
func Select(ctx context.Context, data any, selector string, opts ...execution.ExecuteOptionFn) (any, int, error) {
res, count, err := Query(ctx, data, selector, opts...)
if err != nil {
return nil, 0, err
}
out := make([]any, 0)
for _, v := range res {
goValue, err := v.GoValue()
if err != nil {
return nil, 0, err
}
out = append(out, goValue)
}
return out, count, err
}
// Modify runs the query against the given data and updates it in-place.
// Given data must be a pointer to a mutable data structure.
func Modify(ctx context.Context, data any, selector string, newValue any, opts ...execution.ExecuteOptionFn) (int, error) {
res, count, err := Query(ctx, data, selector, opts...)
if err != nil {
return 0, err
}
for _, v := range res {
if err := v.Set(model.NewValue(newValue)); err != nil {
return 0, err
}
}
return count, nil
}