/
primitive.go
129 lines (112 loc) · 3.38 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package types
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
type primitive uint8
const (
// StringType represents a UTF-8 string type.
StringType primitive = iota
// NumberType represents a number type, either an integer or a float.
NumberType
// BoolType represents a boolean type.
BoolType
// Int64Type represents a 64-bit integer.
Int64Type
// Float64Type represents a 64-bit floating point.
Float64Type
)
var (
_ attr.Type = StringType
_ attr.Type = NumberType
_ attr.Type = BoolType
_ xattr.TypeWithValidate = Int64Type
_ xattr.TypeWithValidate = Float64Type
)
func (p primitive) String() string {
switch p {
case StringType:
return "types.StringType"
case NumberType:
return "types.NumberType"
case BoolType:
return "types.BoolType"
case Int64Type:
return "types.Int64Type"
case Float64Type:
return "types.Float64Type"
default:
return fmt.Sprintf("unknown primitive %d", p)
}
}
// TerraformType returns the tftypes.Type that should be used to represent this
// type. This constrains what user input will be accepted and what kind of data
// can be set in state. The framework will use this to translate the Type to
// something Terraform can understand.
func (p primitive) TerraformType(_ context.Context) tftypes.Type {
switch p {
case StringType:
return tftypes.String
case NumberType, Int64Type, Float64Type:
return tftypes.Number
case BoolType:
return tftypes.Bool
default:
panic(fmt.Sprintf("unknown primitive %d", p))
}
}
// ValueFromTerraform returns a Value given a tftypes.Value. This is meant to
// convert the tftypes.Value into a more convenient Go type for the provider to
// consume the data with.
func (p primitive) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
switch p {
case StringType:
return stringValueFromTerraform(ctx, in)
case NumberType:
return numberValueFromTerraform(ctx, in)
case BoolType:
return boolValueFromTerraform(ctx, in)
case Int64Type:
return int64ValueFromTerraform(ctx, in)
case Float64Type:
return float64ValueFromTerraform(ctx, in)
default:
panic(fmt.Sprintf("unknown primitive %d", p))
}
}
// Equal returns true if `o` is also a primitive, and is the same type of
// primitive as `p`.
func (p primitive) Equal(o attr.Type) bool {
other, ok := o.(primitive)
if !ok {
return false
}
switch p {
case StringType, NumberType, BoolType, Int64Type, Float64Type:
return p == other
default:
// unrecognized types are never equal to anything.
return false
}
}
// ApplyTerraform5AttributePathStep applies the given AttributePathStep to the
// type.
func (p primitive) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
return nil, fmt.Errorf("cannot apply AttributePathStep %T to %s", step, p.String())
}
// Validate implements type validation.
func (p primitive) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics {
var diags diag.Diagnostics
switch p {
case Int64Type:
diags.Append(int64Validate(ctx, in, path)...)
case Float64Type:
diags.Append(float64Validate(ctx, in, path)...)
}
return diags
}