Skip to content

Commit 0eb939e

Browse files
authored
tfsdk: Document and clarify GetAttribute and SetAttribute further (#534)
Reference: #66 Reference: #186 Documenting existing intentions and behaviors with `tfsdk.Config`, `tfsdk.Plan`, and `tfsdk.State` type `GetAttribute()` and `SetAttribute()` methods.
1 parent f427696 commit 0eb939e

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

tfsdk/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ func (c Config) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2020
return c.data().Get(ctx, target)
2121
}
2222

23-
// GetAttribute retrieves the attribute found at `path` and populates the
24-
// `target` with the value.
23+
// GetAttribute retrieves the attribute or block found at `path` and populates
24+
// the `target` with the value. This method is intended for top level schema
25+
// attributes or blocks. Use `types` package methods or custom types to step
26+
// into collections.
27+
//
28+
// Attributes or elements under null or unknown collections return null
29+
// values, however this behavior is not protected by compatibility promises.
2530
func (c Config) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
2631
return c.data().GetAtPath(ctx, path, target)
2732
}

tfsdk/plan.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ func (p Plan) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2020
return p.data().Get(ctx, target)
2121
}
2222

23-
// GetAttribute retrieves the attribute found at `path` and populates the
24-
// `target` with the value.
23+
// GetAttribute retrieves the attribute or block found at `path` and populates
24+
// the `target` with the value. This method is intended for top level schema
25+
// attributes or blocks. Use `types` package methods or custom types to step
26+
// into collections.
27+
//
28+
// Attributes or elements under null or unknown collections return null
29+
// values, however this behavior is not protected by compatibility promises.
2530
func (p Plan) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
2631
return p.data().GetAtPath(ctx, path, target)
2732
}
@@ -58,6 +63,10 @@ func (p *Plan) Set(ctx context.Context, val interface{}) diag.Diagnostics {
5863
// path does not have a value, it will be added, including any parent attribute
5964
// paths as necessary.
6065
//
66+
// The value must not be an untyped nil. Use a typed nil or types package null
67+
// value function instead. For example with a types.StringType attribute,
68+
// use (*string)(nil) or types.StringNull().
69+
//
6170
// Lists can only have the next element added according to the current length.
6271
func (p *Plan) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
6372
data := p.data()

tfsdk/state.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ func (s State) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2121
return s.data().Get(ctx, target)
2222
}
2323

24-
// GetAttribute retrieves the attribute found at `path` and populates the
25-
// `target` with the value.
24+
// GetAttribute retrieves the attribute or block found at `path` and populates
25+
// the `target` with the value. This method is intended for top level schema
26+
// attributes or blocks. Use `types` package methods or custom types to step
27+
// into collections.
28+
//
29+
// Attributes or elements under null or unknown collections return null
30+
// values, however this behavior is not protected by compatibility promises.
2631
func (s State) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
2732
return s.data().GetAtPath(ctx, path, target)
2833
}
@@ -69,6 +74,10 @@ func (s *State) Set(ctx context.Context, val interface{}) diag.Diagnostics {
6974
// path does not have a value, it will be added, including any parent attribute
7075
// paths as necessary.
7176
//
77+
// The value must not be an untyped nil. Use a typed nil or types package null
78+
// value function instead. For example with a types.StringType attribute,
79+
// use (*string)(nil) or types.StringNull().
80+
//
7281
// Lists can only have the next element added according to the current length.
7382
func (s *State) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
7483
data := s.data()

website/docs/plugin/framework/accessing-values.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ explanation on how objects can be converted into Go types.
6767

6868
To descend into deeper nested data structures, the `types.List`, `types.Map`, and `types.Set` types each have an `ElementsAs()` method. The `types.Object` type has an `As()` method.
6969

70-
## Get a Single Attribute's Value
70+
## Get a Single Attribute or Block Value
7171

72-
Use the `GetAttribute` method to retrieve a single attribute value from the configuration, plan, and state.
72+
Use the `GetAttribute` method to retrieve a top level attribute or block value from the configuration, plan, and state.
7373

7474
```go
7575
func (r ThingResource) Read(ctx context.Context,

website/docs/plugin/framework/writing-state.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ The state information is represented as an object, and gets persisted like an
6464
object. Refer to the [conversion rules](#conversion-rules) for an explanation on how
6565
objects get persisted and what Go types are valid for persisting as an object.
6666

67-
## Set a Single Attribute's Value
67+
## Set a Single Attribute or Block Value
6868

69-
Use the `SetAttribute` method to set an individual attribute value.
69+
Use the `SetAttribute` method to set an individual attribute or block value.
70+
71+
-> 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()`.
7072

7173
```go
7274
func (r ThingResource) Read(ctx context.Context,

0 commit comments

Comments
 (0)