MCPcopy Create free account
hub / github.com/TomWright/dasel / objectExprExecutor

Function objectExprExecutor

execution/execute_object.go:11–64  ·  view source on GitHub ↗
(e ast.ObjectExpr)

Source from the content-addressed store, hash-verified

9)
10
11func objectExprExecutor(e ast.ObjectExpr) (expressionExecutor, error) {
12 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
13 ctx = WithExecutorID(ctx, "objectExpr")
14 obj := model.NewMapValue()
15 for _, p := range e.Pairs {
16
17 if ast.IsType[ast.SpreadExpr](p.Key) {
18 var val *model.Value
19 var err error
20 if p.Value != nil {
21 // We need to spread the resulting value.
22 val, err = ExecuteAST(ctx, p.Value, data, options)
23 if err != nil {
24 return nil, fmt.Errorf("error evaluating spread values: %w", err)
25 }
26 } else {
27 val = data
28 }
29
30 if err := val.RangeMap(func(key string, value *model.Value) error {
31 if err := obj.SetMapKey(key, value); err != nil {
32 return fmt.Errorf("error setting map key: %w", err)
33 }
34 return nil
35 }); err != nil {
36 return nil, fmt.Errorf("error spreading into object: %w", err)
37 }
38 continue
39 }
40
41 key, err := ExecuteAST(ctx, p.Key, data, options)
42 if err != nil {
43 return nil, fmt.Errorf("error evaluating key: %w", err)
44 }
45 if !key.IsString() {
46 return nil, fmt.Errorf("expected key to resolve to string, got %s", key.Type())
47 }
48
49 val, err := ExecuteAST(ctx, p.Value, data, options)
50 if err != nil {
51 return nil, fmt.Errorf("error evaluating value: %w", err)
52 }
53
54 keyStr, err := key.StringValue()
55 if err != nil {
56 return nil, fmt.Errorf("error getting string value: %w", err)
57 }
58 if err := obj.SetMapKey(keyStr, val); err != nil {
59 return nil, fmt.Errorf("error setting map key: %w", err)
60 }
61 }
62 return obj, nil
63 }, nil
64}
65
66func propertyExprExecutor(e ast.PropertyExpr) (expressionExecutor, error) {
67 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {

Callers 1

exprExecutorFunction · 0.85

Calls 8

RangeMapMethod · 0.95
NewMapValueFunction · 0.92
WithExecutorIDFunction · 0.85
ExecuteASTFunction · 0.85
SetMapKeyMethod · 0.80
IsStringMethod · 0.80
TypeMethod · 0.80
StringValueMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…