| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/model" |
| 9 | ) |
| 10 | |
| 11 | // FuncEndsWith is a function that checks if a string ends with a given suffix. |
| 12 | var FuncEndsWith = NewFunc( |
| 13 | "endsWith", |
| 14 | func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) { |
| 15 | var input string |
| 16 | var suffix string |
| 17 | var err error |
| 18 | |
| 19 | if len(args) == 2 { |
| 20 | input, err = args[0].StringValue() |
| 21 | if err != nil { |
| 22 | return nil, fmt.Errorf("endsWith expects a string as the first argument: %w", err) |
| 23 | } |
| 24 | suffix, err = args[1].StringValue() |
| 25 | if err != nil { |
| 26 | return nil, fmt.Errorf("endsWith expects a string suffix as the second argument: %w", err) |
| 27 | } |
| 28 | } else { |
| 29 | input, err = data.StringValue() |
| 30 | if err != nil { |
| 31 | return nil, fmt.Errorf("endsWith expects data to be a string: %w", err) |
| 32 | } |
| 33 | suffix, err = args[0].StringValue() |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("endsWith expects a string suffix as the first argument: %w", err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return model.NewBoolValue(strings.HasSuffix(input, suffix)), nil |
| 40 | }, |
| 41 | ValidateArgsMinMax(1, 2), |
| 42 | ) |