-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
478aa91
commit 0764ace
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package numberfunc | ||
|
||
import ( | ||
"context" | ||
"math" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
// ensure the implementation satisfies the expected interfaces | ||
var _ function.Function = &modFunction{} | ||
|
||
// helper pseudo-constructor to simplify provider server and testing implementation | ||
func NewModFunction() function.Function { | ||
return &modFunction{} | ||
} | ||
|
||
// function implementation | ||
type modFunction struct{} | ||
|
||
// function metadata | ||
func (*modFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "mod" | ||
} | ||
|
||
// define the provider-level definition for the function | ||
func (*modFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Determine modulus of a number", | ||
MarkdownDescription: "Return the remainder of the dividend number divided by the divisor number.", | ||
Parameters: []function.Parameter{ | ||
function.Float64Parameter{ | ||
Name: "dividend", | ||
Description: "The dividend number from which to divide.", | ||
}, | ||
function.Float64Parameter{ | ||
Name: "divisor", | ||
Description: "The divisor number by which to divide.", | ||
}, | ||
}, | ||
Return: function.Float64Return{}, | ||
} | ||
} | ||
|
||
func (*modFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
// initialize input parameters | ||
var dividend, divisor float64 | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, ÷nd, &divisor)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
ctx = tflog.SetField(ctx, "mod: dividend", dividend) | ||
ctx = tflog.SetField(ctx, "mod: divisor", divisor) | ||
|
||
// determine the modulus | ||
modulus := math.Mod(dividend, divisor) | ||
ctx = tflog.SetField(ctx, "mod: modulus", modulus) | ||
|
||
// store the result as a float64 | ||
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, &modulus)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
tflog.Debug(ctx, "mod: successful return", map[string]any{"success": true}) | ||
} |