|
| 1 | +package numberfunc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math" |
| 6 | + "math/big" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/function" |
| 9 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 10 | +) |
| 11 | + |
| 12 | +// ensure the implementation satisfies the expected interfaces |
| 13 | +var _ function.Function = &truncateFunction{} |
| 14 | + |
| 15 | +// helper pseudo-constructor to simplify provider server and testing implementation |
| 16 | +func NewTruncateFunction() function.Function { |
| 17 | + return &truncateFunction{} |
| 18 | +} |
| 19 | + |
| 20 | +// function implementation |
| 21 | +type truncateFunction struct{} |
| 22 | + |
| 23 | +// function metadata |
| 24 | +func (*truncateFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { |
| 25 | + resp.Name = "truncate" |
| 26 | +} |
| 27 | + |
| 28 | +// define the provider-level definition for the function |
| 29 | +func (*truncateFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { |
| 30 | + resp.Definition = function.Definition{ |
| 31 | + Summary: "Determine integer value of a number", |
| 32 | + MarkdownDescription: "Return the integer value of an input parameter.", |
| 33 | + Parameters: []function.Parameter{ |
| 34 | + function.NumberParameter{ |
| 35 | + Name: "number", |
| 36 | + Description: "Input number parameter for determining the truncated integer value.", |
| 37 | + }, |
| 38 | + }, |
| 39 | + Return: function.Int64Return{}, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (*truncateFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { |
| 44 | + // initialize input parameters |
| 45 | + var inputNum *big.Float |
| 46 | + |
| 47 | + resp.Error = req.Arguments.Get(ctx, &inputNum) |
| 48 | + if resp.Error != nil { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + ctx = tflog.SetField(ctx, "truncate: number", inputNum) |
| 53 | + |
| 54 | + // convert to float64 |
| 55 | + float, _ := inputNum.Float64() |
| 56 | + if math.IsNaN(float) || math.IsInf(float, 0) { |
| 57 | + resp.Error = function.NewArgumentFuncError(0, "truncate: input number is beyond the limits of float64") |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + // determine the truncated integer |
| 62 | + truncate := math.Trunc(float) |
| 63 | + if truncate > float64(math.MaxInt64) || truncate < float64(math.MinInt64) { |
| 64 | + resp.Error = function.NewArgumentFuncError(0, "truncate: truncated input number is beyond the limits of int64") |
| 65 | + return |
| 66 | + } |
| 67 | + result := int64(truncate) |
| 68 | + |
| 69 | + ctx = tflog.SetField(ctx, "truncate: truncated", result) |
| 70 | + |
| 71 | + // store the result as an int64 |
| 72 | + resp.Error = resp.Result.Set(ctx, &result) |
| 73 | + if resp.Error != nil { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + tflog.Debug(ctx, "truncate: successful return", map[string]any{"success": true}) |
| 78 | +} |
0 commit comments