-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathephemeral_result_data.go
94 lines (78 loc) · 3.3 KB
/
ephemeral_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfsdk
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// EphemeralResultData represents the data returned after opening a Terraform ephemeral resource.
type EphemeralResultData struct {
Raw tftypes.Value
Schema fwschema.Schema
}
// Get populates the struct passed as `target` with the entire ephemeral result data object.
func (s EphemeralResultData) Get(ctx context.Context, target interface{}) diag.Diagnostics {
return s.data().Get(ctx, target)
}
// GetAttribute retrieves the attribute or block found at `path` and populates
// the `target` with the value. This method is intended for top level schema
// attributes or blocks. Use `types` package methods or custom types to step
// into collections.
//
// Attributes or elements under null or unknown collections return null
// values, however this behavior is not protected by compatibility promises.
func (s EphemeralResultData) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
return s.data().GetAtPath(ctx, path, target)
}
// PathMatches returns all matching path.Paths from the given path.Expression.
//
// If a parent path is null or unknown, which would prevent a full expression
// from matching, the parent path is returned rather than no match to prevent
// false positives.
func (s EphemeralResultData) PathMatches(ctx context.Context, pathExpr path.Expression) (path.Paths, diag.Diagnostics) {
return s.data().PathMatches(ctx, pathExpr)
}
// Set populates the entire ephemeral result data object using the supplied Go value. The value `val`
// should be a struct whose values have one of the attr.Value types. Each field
// must be tagged with the corresponding schema field.
func (s *EphemeralResultData) Set(ctx context.Context, val interface{}) diag.Diagnostics {
data := s.data()
diags := data.Set(ctx, val)
if diags.HasError() {
return diags
}
s.Raw = data.TerraformValue
return diags
}
// SetAttribute sets the attribute at `path` using the supplied Go value.
//
// The attribute path and value must be valid with the current schema. If the
// attribute path already has a value, it will be overwritten. If the attribute
// path does not have a value, it will be added, including any parent attribute
// paths as necessary.
//
// The value must not be an untyped nil. Use a typed nil or types package null
// value function instead. For example with a types.StringType attribute,
// use (*string)(nil) or types.StringNull().
//
// Lists can only have the next element added according to the current length.
func (s *EphemeralResultData) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
data := s.data()
diags := data.SetAtPath(ctx, path, val)
if diags.HasError() {
return diags
}
s.Raw = data.TerraformValue
return diags
}
func (s EphemeralResultData) data() *fwschemadata.Data {
return &fwschemadata.Data{
Description: fwschemadata.DataDescriptionEphemeralResultData,
Schema: s.Schema,
TerraformValue: s.Raw,
}
}