Skip to content

Commit aa0a2be

Browse files
author
Ivan De Marino
committed
Implementing Unknown() and Null() for the types.Bool type
1 parent a43e57b commit aa0a2be

File tree

3 files changed

+64
-39
lines changed

3 files changed

+64
-39
lines changed

internal/testing/types/bool.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-go/tftypes"
8+
79
"github.com/hashicorp/terraform-plugin-framework/attr"
810
"github.com/hashicorp/terraform-plugin-framework/types"
9-
"github.com/hashicorp/terraform-plugin-go/tftypes"
1011
)
1112

1213
var (
@@ -40,13 +41,13 @@ func (t BoolType) TerraformType(_ context.Context) tftypes.Type {
4041
func (t BoolType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
4142
if in.IsNull() {
4243
return Bool{
43-
Bool: types.Bool{Null: true},
44+
Bool: types.NullBool(),
4445
CreatedBy: t,
4546
}, nil
4647
}
4748
if !in.IsKnown() {
4849
return Bool{
49-
Bool: types.Bool{Unknown: true},
50+
Bool: types.UnknownBool(),
5051
CreatedBy: t,
5152
}, nil
5253
}

types/bool.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ package types
33
import (
44
"context"
55

6-
"github.com/hashicorp/terraform-plugin-framework/attr"
76
"github.com/hashicorp/terraform-plugin-go/tftypes"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
89
)
910

1011
func boolValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
1112
if in.IsNull() {
1213
return Bool{
13-
Null: true,
14+
null: true,
1415
}, nil
1516
}
1617
if !in.IsKnown() {
1718
return Bool{
18-
Unknown: true,
19+
unknown: true,
1920
}, nil
2021
}
2122
var b bool
@@ -30,29 +31,44 @@ var _ attr.Value = Bool{}
3031

3132
// Bool represents a boolean value.
3233
type Bool struct {
33-
// Unknown will be true if the value is not yet known.
34-
Unknown bool
34+
// unknown will be true if the value is not yet known.
35+
unknown bool
3536

36-
// Null will be true if the value was not set, or was explicitly set to
37+
// null will be true if the value was not set, or was explicitly set to
3738
// null.
38-
Null bool
39+
null bool
3940

40-
// Value contains the set value, as long as Unknown and Null are both
41+
// Value contains the set value, as long as unknown and null are both
4142
// false.
4243
Value bool
4344
}
4445

46+
// NewBool returns a Bool initialized with the given value.
47+
func NewBool(value bool) Bool {
48+
return Bool{Value: value}
49+
}
50+
51+
// UnknownBool returns a Bool where Bool.Unknown returns true.
52+
func UnknownBool() Bool {
53+
return Bool{unknown: true}
54+
}
55+
56+
// NullBool returns a Bool where Bool.Null returns true.
57+
func NullBool() Bool {
58+
return Bool{null: true}
59+
}
60+
4561
// Type returns a BoolType.
4662
func (b Bool) Type(_ context.Context) attr.Type {
4763
return BoolType
4864
}
4965

5066
// ToTerraformValue returns the data contained in the *Bool as a tftypes.Value.
5167
func (b Bool) ToTerraformValue(_ context.Context) (tftypes.Value, error) {
52-
if b.Null {
68+
if b.null {
5369
return tftypes.NewValue(tftypes.Bool, nil), nil
5470
}
55-
if b.Unknown {
71+
if b.unknown {
5672
return tftypes.NewValue(tftypes.Bool, tftypes.UnknownValue), nil
5773
}
5874
if err := tftypes.ValidateValue(tftypes.Bool, b.Value); err != nil {
@@ -67,11 +83,19 @@ func (b Bool) Equal(other attr.Value) bool {
6783
if !ok {
6884
return false
6985
}
70-
if b.Unknown != o.Unknown {
86+
if b.unknown != o.unknown {
7187
return false
7288
}
73-
if b.Null != o.Null {
89+
if b.null != o.null {
7490
return false
7591
}
7692
return b.Value == o.Value
7793
}
94+
95+
func (b Bool) Unknown() bool {
96+
return b.unknown
97+
}
98+
99+
func (b Bool) Null() bool {
100+
return b.null
101+
}

types/bool_test.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func testBoolValueFromTerraform(t *testing.T, direct bool) {
3333
},
3434
"unknown": {
3535
input: tftypes.NewValue(tftypes.Bool, tftypes.UnknownValue),
36-
expectation: Bool{Unknown: true},
36+
expectation: Bool{unknown: true},
3737
},
3838
"null": {
3939
input: tftypes.NewValue(tftypes.Bool, nil),
40-
expectation: Bool{Null: true},
40+
expectation: Bool{null: true},
4141
},
4242
"wrongType": {
4343
input: tftypes.NewValue(tftypes.String, "oops"),
@@ -96,11 +96,11 @@ func TestBoolToTerraformValue(t *testing.T) {
9696
expectation: tftypes.NewValue(tftypes.Bool, false),
9797
},
9898
"unknown": {
99-
input: Bool{Unknown: true},
99+
input: Bool{unknown: true},
100100
expectation: tftypes.NewValue(tftypes.Bool, tftypes.UnknownValue),
101101
},
102102
"null": {
103-
input: Bool{Null: true},
103+
input: Bool{null: true},
104104
expectation: tftypes.NewValue(tftypes.Bool, nil),
105105
},
106106
}
@@ -143,12 +143,12 @@ func TestBoolEqual(t *testing.T) {
143143
},
144144
"true-unknown": {
145145
input: Bool{Value: true},
146-
candidate: Bool{Unknown: true},
146+
candidate: Bool{unknown: true},
147147
expectation: false,
148148
},
149149
"true-null": {
150150
input: Bool{Value: true},
151-
candidate: Bool{Null: true},
151+
candidate: Bool{null: true},
152152
expectation: false,
153153
},
154154
"true-wrongType": {
@@ -173,12 +173,12 @@ func TestBoolEqual(t *testing.T) {
173173
},
174174
"false-unknown": {
175175
input: Bool{Value: false},
176-
candidate: Bool{Unknown: true},
176+
candidate: Bool{unknown: true},
177177
expectation: false,
178178
},
179179
"false-null": {
180180
input: Bool{Value: false},
181-
candidate: Bool{Null: true},
181+
candidate: Bool{null: true},
182182
expectation: false,
183183
},
184184
"false-wrongType": {
@@ -192,62 +192,62 @@ func TestBoolEqual(t *testing.T) {
192192
expectation: false,
193193
},
194194
"unknown-true": {
195-
input: Bool{Unknown: true},
195+
input: Bool{unknown: true},
196196
candidate: Bool{Value: true},
197197
expectation: false,
198198
},
199199
"unknown-false": {
200-
input: Bool{Unknown: true},
200+
input: Bool{unknown: true},
201201
candidate: Bool{Value: false},
202202
expectation: false,
203203
},
204204
"unknown-unknown": {
205-
input: Bool{Unknown: true},
206-
candidate: Bool{Unknown: true},
205+
input: Bool{unknown: true},
206+
candidate: Bool{unknown: true},
207207
expectation: true,
208208
},
209209
"unknown-null": {
210-
input: Bool{Unknown: true},
211-
candidate: Bool{Null: true},
210+
input: Bool{unknown: true},
211+
candidate: Bool{null: true},
212212
expectation: false,
213213
},
214214
"unknown-wrongType": {
215-
input: Bool{Unknown: true},
215+
input: Bool{unknown: true},
216216
candidate: &String{Value: "oops"},
217217
expectation: false,
218218
},
219219
"unknown-nil": {
220-
input: Bool{Unknown: true},
220+
input: Bool{unknown: true},
221221
candidate: nil,
222222
expectation: false,
223223
},
224224
"null-true": {
225-
input: Bool{Null: true},
225+
input: Bool{null: true},
226226
candidate: Bool{Value: true},
227227
expectation: false,
228228
},
229229
"null-false": {
230-
input: Bool{Null: true},
230+
input: Bool{null: true},
231231
candidate: Bool{Value: false},
232232
expectation: false,
233233
},
234234
"null-unknown": {
235-
input: Bool{Null: true},
236-
candidate: Bool{Unknown: true},
235+
input: Bool{null: true},
236+
candidate: Bool{unknown: true},
237237
expectation: false,
238238
},
239239
"null-null": {
240-
input: Bool{Null: true},
241-
candidate: Bool{Null: true},
240+
input: Bool{null: true},
241+
candidate: Bool{null: true},
242242
expectation: true,
243243
},
244244
"null-wrongType": {
245-
input: Bool{Null: true},
245+
input: Bool{null: true},
246246
candidate: &String{Value: "oops"},
247247
expectation: false,
248248
},
249249
"null-nil": {
250-
input: Bool{Null: true},
250+
input: Bool{null: true},
251251
candidate: nil,
252252
expectation: false,
253253
},

0 commit comments

Comments
 (0)