-
Notifications
You must be signed in to change notification settings - Fork 132
/
bool.go
104 lines (75 loc) · 2.06 KB
/
bool.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package types
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
type TfBoolValue struct {
basetypes.BoolValue
}
var _ basetypes.BoolValuable = TfBoolValue{}
func (t *TfBoolValue) UnmarshalJSON(data []byte) error {
var v bool
if err := json.Unmarshal(data, &v); err != nil {
return err
}
t.BoolValue = basetypes.NewBoolValue(v)
return nil
}
func (t TfBoolValue) MarshalJSON() ([]byte, error) {
return json.Marshal(t.BoolValue.ValueBool())
}
func (v TfBoolValue) Equal(o attr.Value) bool {
other, ok := o.(TfBoolValue)
if !ok {
return false
}
return v.BoolValue.Equal(other.BoolValue)
}
func (v TfBoolValue) Type(ctx context.Context) attr.Type {
return TfBoolType{}
}
type TfBoolType struct {
basetypes.BoolType
}
var _ basetypes.BoolTypable = TfBoolType{}
func (t TfBoolType) Equal(o attr.Type) bool {
other, ok := o.(TfBoolType)
if !ok {
return false
}
return t.BoolType.Equal(other.BoolType)
}
func (t TfBoolType) String() string {
return "TfBoolType"
}
func (t TfBoolType) ValueFromBool(ctx context.Context, in basetypes.BoolValue) (basetypes.BoolValuable, diag.Diagnostics) {
value := TfBoolValue{
BoolValue: in,
}
return value, nil
}
func (t TfBoolType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.BoolType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
boolValue, ok := attrValue.(basetypes.BoolValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
boolValuable, diags := t.ValueFromBool(ctx, boolValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting BoolValue to BoolValuable: %v", diags)
}
return boolValuable, nil
}
func (t TfBoolType) ValueType(ctx context.Context) attr.Value {
return TfBoolValue{}
}