Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function: Add validation for parameter name conflicts and update defaulting logic #936

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions function/bool_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ type BoolParameter struct {

// Name is a short usage name for the parameter, such as "data". This name
// is used in documentation, such as generating a function signature,
// however its usage may be extended in the future. If no name is provided,
// this will default to "param".
// however its usage may be extended in the future.
//
// If no name is provided, this will default to "param" with a suffix of the
// position the parameter is in the function definition. ("param1", "param2", etc.)
// If the parameter is variadic, the default name will be "varparam".
//
// This must be a valid Terraform identifier, such as starting with an
// alphabetical character and followed by alphanumeric or underscore
Expand Down Expand Up @@ -91,11 +94,7 @@ func (p BoolParameter) GetMarkdownDescription() string {

// GetName returns the parameter name.
func (p BoolParameter) GetName() string {
if p.Name != "" {
return p.Name
}

return DefaultParameterName
return p.Name
}

// GetType returns the parameter data type.
Expand Down
8 changes: 1 addition & 7 deletions function/bool_parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,7 @@ func TestBoolParameterGetName(t *testing.T) {
}{
"unset": {
parameter: function.BoolParameter{},
expected: function.DefaultParameterName,
},
"Name-empty": {
parameter: function.BoolParameter{
Name: "",
},
expected: function.DefaultParameterName,
expected: "",
},
"Name-nonempty": {
parameter: function.BoolParameter{
Expand Down
57 changes: 57 additions & 0 deletions function/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ func (d Definition) Parameter(ctx context.Context, position int) (Parameter, dia
return d.Parameters[position], nil
}

// ParameterName returns the Parameter name for a given argument position. This will be the return
// from the `(Parameter).GetName` function or a default value. An error diagnostic is raised if the
// position is outside the expected arguments.
func (d Definition) ParameterName(ctx context.Context, position int) (string, diag.Diagnostics) {
austinvalle marked this conversation as resolved.
Show resolved Hide resolved
parameter, diags := d.Parameter(ctx, position)
if diags.HasError() {
return "", diags
}

definedName := parameter.GetName()
if definedName != "" {
return definedName, nil
}

if position >= len(d.Parameters) {
return DefaultVariadicParameterName, nil
}

return fmt.Sprintf("%s%d", DefaultParameterNamePrefix, position+1), nil
}

// ValidateImplementation contains logic for validating the provider-defined
// implementation of the definition to prevent unexpected errors or panics. This
// logic runs during the GetProviderSchema RPC, or via provider-defined unit
Expand All @@ -108,6 +129,42 @@ func (d Definition) ValidateImplementation(ctx context.Context) diag.Diagnostics
)
}

paramNames := make(map[string]int, len(d.Parameters))
for pos := range d.Parameters {
// TODO: what should we do with the diags? This should never happen
name, _ := d.ParameterName(ctx, pos)
austinvalle marked this conversation as resolved.
Show resolved Hide resolved

conflictPos, exists := paramNames[name]
if exists {
diags.AddError(
"Invalid Function Definition",
"When validating the function definition, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
"Parameter names must be unique. "+
fmt.Sprintf("Parameters at position %d and %d have the same name %q", conflictPos, pos, name),
)
continue
}

paramNames[name] = pos
}

if d.VariadicParameter != nil {
// TODO: what should we do with the diags? This should never happen
name, _ := d.ParameterName(ctx, len(d.Parameters)+1)

conflictPos, exists := paramNames[name]
if exists {
diags.AddError(
"Invalid Function Definition",
"When validating the function definition, an implementation issue was found. "+
"This is always an issue with the provider and should be reported to the provider developers.\n\n"+
"Parameter names must be unique. "+
fmt.Sprintf("Parameter at position %d and the variadic parameter have the same name %q", conflictPos, name),
)
}
}

return diags
}

Expand Down
Loading
Loading