Skip to content

Commit

Permalink
function: Additional Go documentation details about argument data bei…
Browse files Browse the repository at this point in the history
…ng zero-based and how to handle variadic parameters
  • Loading branch information
bflad committed Dec 15, 2023
1 parent b2f1c6f commit 07a8699
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions function/arguments_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
)

// ArgumentsData is the positional argument data sent by Terraform for a single
// function call. Use the GetArgument method in the Function type Run method to
// fetch the data of each argument.
// ArgumentsData is the zero-based positional argument data sent by Terraform
// for a single function call. Use the Get method or GetArgument method in the
// Function type Run method to fetch the data.
//
// This data is automatically populated by the framework based on the function
// definition. For unit testing, use the NewArgumentsData function to manually
Expand All @@ -41,9 +41,14 @@ func (d ArgumentsData) Equal(o ArgumentsData) bool {

// Get retrieves all argument data and populates the targets with the values.
// All arguments must be present in the targets, including all parameters and an
// optional variadic parameter, if defined for the function, otherwise an error
// diagnostic will be raised. Each target type must be acceptable for the data
// type in the parameter definition.
// optional variadic parameter, otherwise an error diagnostic will be raised.
// Each target type must be acceptable for the data type in the parameter
// definition.
//
// Variadic parameter argument data must be consumed by a types.List or Go slice
// type with an element type appropriate for the parameter definition ([]T). The
// framework automatically populates this list with elements matching the zero,
// one, or more arguments passed.
func (d ArgumentsData) Get(ctx context.Context, targets ...any) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down Expand Up @@ -103,9 +108,14 @@ func (d ArgumentsData) Get(ctx context.Context, targets ...any) diag.Diagnostics
return diags
}

// GetArgument retrieves the argument data found at the given position and
// populates the target with the value. The target type must be acceptable for
// the data type in the parameter definition.
// GetArgument retrieves the argument data found at the given zero-based
// position and populates the target with the value. The target type must be
// acceptable for the data type in the parameter definition.
//
// Variadic parameter argument data must be consumed by a types.List or Go slice
// type with an element type appropriate for the parameter definition ([]T) at
// the position after all parameters. The framework automatically populates this
// list with elements matching the zero, one, or more arguments passed.
func (d ArgumentsData) GetArgument(ctx context.Context, position int, target any) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down
2 changes: 1 addition & 1 deletion function/result_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// ResultData is the response data sent to Terraform for a single function call.
// Use the Set method in the Function type Run method to set the result data.
//
// For unit testing, use the NewResulData function to manually create the data
// For unit testing, use the NewResultData function to manually create the data
// for comparison.
type ResultData struct {
value attr.Value
Expand Down

0 comments on commit 07a8699

Please sign in to comment.