-
Notifications
You must be signed in to change notification settings - Fork 91
/
result_data.go
60 lines (47 loc) · 1.52 KB
/
result_data.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package function
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
fwreflect "github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/path"
)
// 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 NewResultData function to manually create the data
// for comparison.
type ResultData struct {
value attr.Value
}
// Equal returns true if the value is equivalent.
func (d ResultData) Equal(o ResultData) bool {
if d.value == nil {
return o.value == nil
}
return d.value.Equal(o.value)
}
// Set saves the result data. The value type must be acceptable for the data
// type in the result definition.
func (d *ResultData) Set(ctx context.Context, value any) *FuncError {
reflectValue, reflectDiags := fwreflect.FromValue(ctx, d.value.Type(ctx), value, path.Empty())
funcErr := FuncErrorFromDiags(ctx, reflectDiags)
if funcErr != nil {
return funcErr
}
d.value = reflectValue
return nil
}
// Value returns the saved value.
func (d ResultData) Value() attr.Value {
return d.value
}
// NewResultData creates a ResultData. This is only necessary for unit testing
// as the framework automatically creates this data for the Function type Run
// method.
func NewResultData(value attr.Value) ResultData {
return ResultData{
value: value,
}
}