| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | ) |
| 9 | |
| 10 | // FuncGet is a function returns the value at the given key/index. |
| 11 | var FuncGet = NewFunc( |
| 12 | "get", |
| 13 | func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) { |
| 14 | |
| 15 | arg := args[0] |
| 16 | |
| 17 | switch arg.Type() { |
| 18 | case model.TypeInt: |
| 19 | index, err := arg.IntValue() |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | return data.GetSliceIndex(int(index)) |
| 24 | case model.TypeString: |
| 25 | key, err := arg.StringValue() |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | return data.GetMapKey(key) |
| 30 | default: |
| 31 | return nil, fmt.Errorf("get expects string or int argument") |
| 32 | } |
| 33 | }, |
| 34 | ValidateArgsExactly(1), |
| 35 | ) |