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: Initial provider defined functions implementation #889

Merged
merged 28 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e15147e
function: Initial provider defined functions implementation
bflad Oct 31, 2023
113cc9f
Update CHANGELOG for #889
bflad Dec 11, 2023
e5c1f6c
diag: Add Diagnostics type AddArgumentError() and AddArgumentWarning(…
bflad Dec 11, 2023
3735dc1
website: Always recommend pointer types with collection/object elemen…
bflad Dec 11, 2023
316b82b
website: Call out function argument collection/object null element/at…
bflad Dec 11, 2023
4c51f47
website: Further clarify function argument collection/object null ele…
bflad Dec 11, 2023
3ebd4f0
deps: Update terraform-plugin-go@fb1d856be38c4fccc56fb61bcf6559aefb3a…
bflad Dec 12, 2023
e68d4ea
Add experimental callouts for functions
bflad Dec 12, 2023
6ea401c
Clarify wording around function support compatibility
bflad Dec 14, 2023
8adbb8d
website: Add ConfigPlanCheck example for unknown testing
bflad Dec 14, 2023
7bb93c4
Update CHANGELOG for #889
bflad Dec 14, 2023
2ffc5d8
deps: Update [email protected]
bflad Dec 14, 2023
9979fe6
Apply suggestions from code review
bflad Dec 15, 2023
2471135
Update website/docs/plugin/framework/functions/index.mdx
bflad Dec 15, 2023
4d992e5
Apply suggestions from code review
bflad Dec 15, 2023
f1aa0f6
function: Add missing CustomType testing
bflad Dec 15, 2023
1c8f974
internal: Add ArgumentsData unit testing with CustomType implementations
bflad Dec 15, 2023
258e080
function: Do not return early with attr.Value in (ArgumentData).Get()
bflad Dec 15, 2023
eec0477
internal/fwserver: Fix errant functionTypes naming in Server
bflad Dec 15, 2023
b2f1c6f
internal/fwserver: Prevent false positives with GetMetadataResponse.F…
bflad Dec 15, 2023
07a8699
function: Additional Go documentation details about argument data bei…
bflad Dec 15, 2023
c4a1246
website: Document function variadic parameter implementation details
bflad Dec 15, 2023
9ebe290
website: Use plain code fence language for markdown with frontmatter
bflad Dec 15, 2023
a93d54b
Update website/docs/plugin/framework/functions/implementation.mdx
bflad Dec 18, 2023
ed19111
internal: Support xattr.TypeWithValidation for argument data
bflad Dec 18, 2023
bff2f0a
Merge branch 'functions' of ssh://github.com/hashicorp/terraform-plug…
bflad Dec 18, 2023
bdb7fe6
Merge branch 'main' into functions
bflad Dec 18, 2023
b785808
Merge branch 'main' into functions
bflad Dec 19, 2023
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
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231211-125057.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'diag: Added `NewArgumentErrorDiagnostic()` and `NewArgumentWarningDiagnostic()`
functions, which create diagnostics with the function argument position set'
time: 2023-12-11T12:50:57.570179-05:00
custom:
Issue: "889"
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231211-125151.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'provider: Added `ProviderWithFunctions` interface for implementing provider
defined functions'
time: 2023-12-11T12:51:51.441373-05:00
custom:
Issue: "889"
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231211-125843.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'diag: Added `(Diagnostics).AddArgumentError()` and `(Diagnostics).AddArgumentWarning()`
methods for appending function argument diagnostics'
time: 2023-12-11T12:58:43.277177-05:00
custom:
Issue: "889"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20231211-125122.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'function: New package for implementing provider defined functions'
time: 2023-12-11T12:51:22.409392-05:00
custom:
Issue: "889"
13 changes: 13 additions & 0 deletions diag/argument_error_diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package diag

// NewArgumentErrorDiagnostic returns a new error severity diagnostic with the
// given summary, detail, and function argument.
func NewArgumentErrorDiagnostic(functionArgument int, summary string, detail string) DiagnosticWithFunctionArgument {
return withFunctionArgument{
Diagnostic: NewErrorDiagnostic(summary, detail),
functionArgument: functionArgument,
}
}
13 changes: 13 additions & 0 deletions diag/argument_warning_diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package diag

// NewArgumentWarningDiagnostic returns a new warning severity diagnostic with
// the given summary, detail, and function argument.
func NewArgumentWarningDiagnostic(functionArgument int, summary string, detail string) DiagnosticWithFunctionArgument {
return withFunctionArgument{
Diagnostic: NewWarningDiagnostic(summary, detail),
functionArgument: functionArgument,
}
}
15 changes: 15 additions & 0 deletions diag/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ type Diagnostic interface {
Equal(Diagnostic) bool
}

// DiagnosticWithFunctionArgument is a diagnostic associated with a
// function argument.
//
// This information is used to display contextual source configuration to
// practitioners.
type DiagnosticWithFunctionArgument interface {
Diagnostic

// FunctionArgument points to a specific function argument position.
//
// If present, this enables the display of source configuration context for
// supporting implementations such as Terraform CLI commands.
FunctionArgument() int
}

// DiagnosticWithPath is a diagnostic associated with an attribute path.
//
// This attribute information is used to display contextual source configuration
Expand Down
12 changes: 12 additions & 0 deletions diag/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ import (
// or consistent.
type Diagnostics []Diagnostic

// AddArgumentError adds a generic function argument error diagnostic to the
// collection.
func (diags *Diagnostics) AddArgumentError(position int, summary string, detail string) {
diags.Append(NewArgumentErrorDiagnostic(position, summary, detail))
}

// AddArgumentWarning adds a function argument warning diagnostic to the
// collection.
func (diags *Diagnostics) AddArgumentWarning(position int, summary string, detail string) {
diags.Append(NewArgumentWarningDiagnostic(position, summary, detail))
}

// AddAttributeError adds a generic attribute error diagnostic to the collection.
func (diags *Diagnostics) AddAttributeError(path path.Path, summary string, detail string) {
diags.Append(NewAttributeErrorDiagnostic(path, summary, detail))
Expand Down
124 changes: 124 additions & 0 deletions diag/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,130 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
)

func TestDiagnosticsAddArgumentError(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
diags diag.Diagnostics
position int
summary string
detail string
expected diag.Diagnostics
}{
"nil-add": {
diags: nil,
position: 0,
summary: "one summary",
detail: "one detail",
expected: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
},
},
"add": {
diags: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
position: 0,
summary: "three summary",
detail: "three detail",
expected: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
diag.NewArgumentErrorDiagnostic(0, "three summary", "three detail"),
},
},
"duplicate": {
diags: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
position: 0,
summary: "one summary",
detail: "one detail",
expected: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
},
}

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

tc.diags.AddArgumentError(tc.position, tc.summary, tc.detail)

if diff := cmp.Diff(tc.diags, tc.expected); diff != "" {
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
}
})
}
}

func TestDiagnosticsAddArgumentWarning(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
diags diag.Diagnostics
position int
summary string
detail string
expected diag.Diagnostics
}{
"nil-add": {
diags: nil,
position: 0,
summary: "one summary",
detail: "one detail",
expected: diag.Diagnostics{
diag.NewArgumentWarningDiagnostic(0, "one summary", "one detail"),
},
},
"add": {
diags: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
position: 0,
summary: "three summary",
detail: "three detail",
expected: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
diag.NewArgumentWarningDiagnostic(0, "three summary", "three detail"),
},
},
"duplicate": {
diags: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
position: 0,
summary: "two summary",
detail: "two detail",
expected: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(0, "one summary", "one detail"),
diag.NewArgumentWarningDiagnostic(0, "two summary", "two detail"),
},
},
}

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

tc.diags.AddArgumentWarning(tc.position, tc.summary, tc.detail)

if diff := cmp.Diff(tc.diags, tc.expected); diff != "" {
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
}
})
}
}

func TestDiagnosticsAddAttributeError(t *testing.T) {
t.Parallel()

Expand Down
54 changes: 54 additions & 0 deletions diag/with_function_argument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package diag

var _ DiagnosticWithFunctionArgument = withFunctionArgument{}

// withFunctionArgument wraps a diagnostic with function argument information.
type withFunctionArgument struct {
Diagnostic

functionArgument int
}

// Equal returns true if the other diagnostic is wholly equivalent.
func (d withFunctionArgument) Equal(other Diagnostic) bool {
o, ok := other.(withFunctionArgument)

if !ok {
return false
}

if d.functionArgument != o.functionArgument {
return false
}

if d.Diagnostic == nil {
return d.Diagnostic == o.Diagnostic
}

return d.Diagnostic.Equal(o.Diagnostic)
}

// FunctionArgument returns the diagnostic function argument.
func (d withFunctionArgument) FunctionArgument() int {
return d.functionArgument
}

// WithFunctionArgument wraps a diagnostic with function argument information
// or overwrites the function argument.
func WithFunctionArgument(functionArgument int, d Diagnostic) DiagnosticWithFunctionArgument {
wp, ok := d.(withFunctionArgument)

if !ok {
return withFunctionArgument{
Diagnostic: d,
functionArgument: functionArgument,
}
}

wp.functionArgument = functionArgument

return wp
}
Loading
Loading