Skip to content

Commit

Permalink
add prototype of modulus function
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Jan 29, 2025
1 parent 478aa91 commit 0764ace
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions stdlib/number/mod.go
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, &dividend, &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})
}

0 comments on commit 0764ace

Please sign in to comment.