-
Notifications
You must be signed in to change notification settings - Fork 91
/
list_return.go
77 lines (64 loc) · 2.65 KB
/
list_return.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package function
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/internal/fwfunction"
"github.com/hashicorp/terraform-plugin-framework/internal/fwtype"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
// Ensure the implementation satisifies the desired interfaces.
var (
_ Return = ListReturn{}
_ fwfunction.ReturnWithValidateImplementation = ListReturn{}
)
// ListReturn represents a function return that is an ordered collection of a
// single element type. Either the ElementType or CustomType field must be set.
//
// When setting the value for this return:
//
// - If CustomType is set, use its associated value type.
// - Otherwise, use [types.List] or a Go slice value type compatible with the
// element type.
type ListReturn struct {
// ElementType is the type for all elements of the list. This field must be
// set.
//
// Element types that contain a dynamic type (i.e. types.Dynamic) are not supported.
// If underlying dynamic values are required, replace this return definition with
// DynamicReturn instead.
ElementType attr.Type
// CustomType enables the use of a custom data type in place of the
// default [basetypes.ListType]. When setting data, the
// [basetypes.ListValuable] implementation associated with this custom
// type must be used in place of [types.List].
CustomType basetypes.ListTypable
}
// GetType returns the return data type.
func (r ListReturn) GetType() attr.Type {
if r.CustomType != nil {
return r.CustomType
}
return basetypes.ListType{
ElemType: r.ElementType,
}
}
// NewResultData returns a new result data based on the type.
func (r ListReturn) NewResultData(ctx context.Context) (ResultData, *FuncError) {
value := basetypes.NewListUnknown(r.ElementType)
if r.CustomType == nil {
return NewResultData(value), nil
}
valuable, diags := r.CustomType.ValueFromList(ctx, value)
return NewResultData(valuable), FuncErrorFromDiags(ctx, diags)
}
// ValidateImplementation contains logic for validating the
// provider-defined implementation of the Return to prevent unexpected
// errors or panics. This logic runs during the GetProviderSchema RPC and
// should never include false positives.
func (p ListReturn) ValidateImplementation(ctx context.Context, req fwfunction.ValidateReturnImplementationRequest, resp *fwfunction.ValidateReturnImplementationResponse) {
if p.CustomType == nil && fwtype.ContainsCollectionWithDynamic(p.GetType()) {
resp.Diagnostics.Append(fwtype.ReturnCollectionWithDynamicTypeDiag())
}
}