| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | |
| 8 | "github.com/tomwright/dasel/v3/model" |
| 9 | ) |
| 10 | |
| 11 | // FuncCeil is a function that returns the smallest integer value greater than or equal to the input. |
| 12 | var FuncCeil = NewFunc( |
| 13 | "ceil", |
| 14 | func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) { |
| 15 | var input *model.Value |
| 16 | if len(args) == 1 { |
| 17 | input = args[0] |
| 18 | } else { |
| 19 | input = data |
| 20 | } |
| 21 | |
| 22 | if input.IsInt() { |
| 23 | v, err := input.IntValue() |
| 24 | if err != nil { |
| 25 | return nil, fmt.Errorf("ceil could not read int value: %w", err) |
| 26 | } |
| 27 | return model.NewIntValue(v), nil |
| 28 | } |
| 29 | |
| 30 | v, err := input.FloatValue() |
| 31 | if err != nil { |
| 32 | return nil, fmt.Errorf("ceil expects a numeric value: %w", err) |
| 33 | } |
| 34 | return model.NewIntValue(int64(math.Ceil(v))), nil |
| 35 | }, |
| 36 | ValidateArgsMax(1), |
| 37 | ) |