-
Notifications
You must be signed in to change notification settings - Fork 91
/
object_return.go
73 lines (60 loc) · 2.69 KB
/
object_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
// 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 = ObjectReturn{}
_ fwfunction.ReturnWithValidateImplementation = ObjectReturn{}
)
// ObjectReturn represents a function return that is mapping of defined
// attribute names to values. When setting the value for this return, use
// [types.Object] or a compatible Go struct as the value type unless the
// CustomType field is set. The AttributeTypes field must be set.
type ObjectReturn struct {
// AttributeTypes is the mapping of underlying attribute names to attribute
// types. This field must be set.
//
// Attribute types that contain a collection with a nested dynamic type (i.e. types.List[types.Dynamic]) are not supported.
// If underlying dynamic collection values are required, replace this return definition with
// DynamicReturn instead.
AttributeTypes map[string]attr.Type
// CustomType enables the use of a custom data type in place of the
// default [basetypes.ObjectType]. When setting data, the
// [basetypes.ObjectValuable] implementation associated with this custom
// type must be used in place of [types.Object].
CustomType basetypes.ObjectTypable
}
// GetType returns the return data type.
func (r ObjectReturn) GetType() attr.Type {
if r.CustomType != nil {
return r.CustomType
}
return basetypes.ObjectType{
AttrTypes: r.AttributeTypes,
}
}
// NewResultData returns a new result data based on the type.
func (r ObjectReturn) NewResultData(ctx context.Context) (ResultData, *FuncError) {
value := basetypes.NewObjectUnknown(r.AttributeTypes)
if r.CustomType == nil {
return NewResultData(value), nil
}
valuable, diags := r.CustomType.ValueFromObject(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 ObjectReturn) ValidateImplementation(ctx context.Context, req fwfunction.ValidateReturnImplementationRequest, resp *fwfunction.ValidateReturnImplementationResponse) {
if p.CustomType == nil && fwtype.ContainsCollectionWithDynamic(p.GetType()) {
resp.Diagnostics.Append(fwtype.ReturnCollectionWithDynamicTypeDiag())
}
}