forked from hashicorp/terraform-plugin-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_as.go
26 lines (23 loc) · 976 Bytes
/
value_as.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
package tfsdk
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
)
// ValueAs takes the attr.Value `val` and populates the Go value `target` with its content.
//
// This is achieved using reflection rules provided by the internal/reflect package.
func ValueAs(ctx context.Context, val attr.Value, target interface{}) diag.Diagnostics {
if reflect.IsGenericAttrValue(ctx, target) {
*(target.(*attr.Value)) = val
return nil
}
raw, err := val.ToTerraformValue(ctx)
if err != nil {
return diag.Diagnostics{diag.NewErrorDiagnostic("Error converting value",
fmt.Sprintf("An unexpected error was encountered converting a %T to its equivalent Terraform representation. This is always a bug in the provider.\n\nError: %s", val, err))}
}
return reflect.Into(ctx, val.Type(ctx), raw, target, reflect.Options{})
}