/
state.go
54 lines (43 loc) · 1.66 KB
/
state.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
package fromproto6
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)
// State returns the *tfsdk.State for a *tfprotov6.DynamicValue and
// fwschema.Schema.
func State(ctx context.Context, proto6DynamicValue *tfprotov6.DynamicValue, schema fwschema.Schema) (*tfsdk.State, diag.Diagnostics) {
if proto6DynamicValue == nil {
return nil, nil
}
var diags diag.Diagnostics
// Panic prevention here to simplify the calling implementations.
// This should not happen, but just in case.
if schema == nil {
diags.AddError(
"Unable to Convert State",
"An unexpected error was encountered when converting the state from the protocol type. "+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
"Please report this to the provider developer:\n\n"+
"Missing schema.",
)
return nil, diags
}
proto6Value, err := proto6DynamicValue.Unmarshal(schema.Type().TerraformType(ctx))
if err != nil {
diags.AddError(
"Unable to Convert State",
"An unexpected error was encountered when converting the state from the protocol type. "+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
"Please report this to the provider developer:\n\n"+err.Error(),
)
return nil, diags
}
fw := &tfsdk.State{
Raw: proto6Value,
Schema: tfsdkSchema(schema),
}
return fw, nil
}