| 1 | package execution_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | "github.com/tomwright/dasel/v3/model/orderedmap" |
| 8 | ) |
| 9 | |
| 10 | func TestRecursiveDescent(t *testing.T) { |
| 11 | t.Run("find key at any depth", testCase{ |
| 12 | inFn: func() *model.Value { |
| 13 | return model.NewValue( |
| 14 | orderedmap.NewMap(). |
| 15 | Set("a", orderedmap.NewMap(). |
| 16 | Set("x", "found1"). |
| 17 | Set("b", orderedmap.NewMap(). |
| 18 | Set("x", "found2"))), |
| 19 | ) |
| 20 | }, |
| 21 | s: `..x`, |
| 22 | outFn: func() *model.Value { |
| 23 | s := model.NewSliceValue() |
| 24 | _ = s.Append(model.NewStringValue("found1")) |
| 25 | _ = s.Append(model.NewStringValue("found2")) |
| 26 | return s |
| 27 | }, |
| 28 | }.run) |
| 29 | |
| 30 | t.Run("wildcard collects all scalars", testCase{ |
| 31 | inFn: func() *model.Value { |
| 32 | return model.NewValue( |
| 33 | orderedmap.NewMap(). |
| 34 | Set("a", "hello"). |
| 35 | Set("b", orderedmap.NewMap(). |
| 36 | Set("c", "world")), |
| 37 | ) |
| 38 | }, |
| 39 | s: `..*`, |
| 40 | outFn: func() *model.Value { |
| 41 | s := model.NewSliceValue() |
| 42 | _ = s.Append(model.NewStringValue("hello")) |
| 43 | _ = s.Append(model.NewStringValue("world")) |
| 44 | return s |
| 45 | }, |
| 46 | }.run) |
| 47 | |
| 48 | t.Run("find key in nested slices containing maps", testCase{ |
| 49 | inFn: func() *model.Value { |
| 50 | return model.NewValue( |
| 51 | orderedmap.NewMap(). |
| 52 | Set("items", []any{ |
| 53 | orderedmap.NewMap().Set("x", "first"), |
| 54 | orderedmap.NewMap().Set("x", "second"), |
| 55 | }), |
| 56 | ) |
| 57 | }, |
| 58 | s: `..x`, |
| 59 | outFn: func() *model.Value { |
| 60 | s := model.NewSliceValue() |
| 61 | _ = s.Append(model.NewStringValue("first")) |
| 62 | _ = s.Append(model.NewStringValue("second")) |
| 63 | return s |
| 64 | }, |
| 65 | }.run) |
| 66 | |
| 67 | t.Run("wildcard with $key available", testCase{ |
| 68 | inFn: func() *model.Value { |
| 69 | return model.NewValue( |
| 70 | orderedmap.NewMap(). |
| 71 | Set("a", "hello"). |
| 72 | Set("b", "world"), |
| 73 | ) |
| 74 | }, |
| 75 | s: `..*`, |
| 76 | outFn: func() *model.Value { |
| 77 | s := model.NewSliceValue() |
| 78 | _ = s.Append(model.NewStringValue("hello")) |
| 79 | _ = s.Append(model.NewStringValue("world")) |
| 80 | return s |
| 81 | }, |
| 82 | }.run) |
| 83 | |
| 84 | t.Run("key not found returns empty", testCase{ |
| 85 | inFn: func() *model.Value { |
| 86 | return model.NewValue( |
| 87 | orderedmap.NewMap(). |
| 88 | Set("a", orderedmap.NewMap(). |
| 89 | Set("b", "value")), |
| 90 | ) |
| 91 | }, |
| 92 | s: `..z`, |
| 93 | outFn: func() *model.Value { |
| 94 | return model.NewSliceValue() |
| 95 | }, |
| 96 | }.run) |
| 97 | } |