/
primitive.go
110 lines (96 loc) · 2.95 KB
/
primitive.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package reflect
import (
"context"
"errors"
"reflect"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Primitive builds a string or boolean, depending on the type of `target`, and
// populates it with the data in `val`.
//
// It is meant to be called through `Into`, not directly.
func Primitive(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
switch target.Kind() {
case reflect.Bool:
var b bool
err := val.As(&b)
if err != nil {
diags.Append(DiagIntoIncompatibleType{
Val: val,
TargetType: target.Type(),
AttrPath: path,
Err: err,
})
return target, diags
}
return reflect.ValueOf(b).Convert(target.Type()), nil
case reflect.String:
var s string
err := val.As(&s)
if err != nil {
diags.Append(DiagIntoIncompatibleType{
Val: val,
TargetType: target.Type(),
AttrPath: path,
Err: err,
})
return target, diags
}
return reflect.ValueOf(s).Convert(target.Type()), nil
default:
diags.Append(DiagIntoIncompatibleType{
Val: val,
TargetType: target.Type(),
AttrPath: path,
Err: errors.New("unknown type"),
})
return target, diags
}
}
// FromString returns an attr.Value as produced by `typ` from a string.
//
// It is meant to be called through FromValue, not directly.
func FromString(ctx context.Context, typ attr.Type, val string, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.String, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfStr := tftypes.NewValue(tftypes.String, val)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfStr, path)...)
if diags.HasError() {
return nil, diags
}
}
str, err := typ.ValueFromTerraform(ctx, tfStr)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return str, diags
}
// FromBool returns an attr.Value as produced by `typ` from a bool.
//
// It is meant to be called through FromValue, not directly.
func FromBool(ctx context.Context, typ attr.Type, val bool, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.Bool, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfBool := tftypes.NewValue(tftypes.Bool, val)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfBool, path)...)
if diags.HasError() {
return nil, diags
}
}
b, err := typ.ValueFromTerraform(ctx, tfBool)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return b, diags
}