-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbool.go
89 lines (76 loc) · 1.62 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
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terra
import (
"errors"
"fmt"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)
func Bool(b bool) BoolValue {
return BoolValue{
isInit: true,
isRef: false,
value: cty.BoolVal(b),
}
}
func ReferenceAsBool(ref Reference) BoolValue {
return BoolValue{
isInit: true,
isRef: true,
ref: ref,
}
}
var _ Value[BoolValue] = (*BoolValue)(nil)
type BoolValue struct {
isInit bool
isRef bool
ref Reference
value cty.Value
}
func (v BoolValue) AsString() StringValue {
if v.isRef {
return ReferenceAsString(v.ref)
}
val, err := convert.Convert(v.value, cty.String)
if err != nil {
panic(fmt.Sprintf("converting bool to string: %s", err.Error()))
}
return StringValue{
isInit: true,
value: val,
}
}
func (v BoolValue) AsNumber() NumberValue {
if v.isRef {
return ReferenceAsNumber(v.ref)
}
val, err := convert.Convert(v.value, cty.Number)
if err != nil {
panic(fmt.Sprintf("converting bool to number: %s", err.Error()))
}
return NumberValue{
isInit: true,
value: val,
}
}
func (v BoolValue) InternalTokens() (hclwrite.Tokens, error) {
if !v.isInit {
return nil, nil
}
if v.isRef {
return v.ref.InternalTokens()
}
return hclwrite.TokensForValue(v.value), nil
}
func (v BoolValue) InternalRef() (Reference, error) {
if !v.isRef {
return Reference{},
errors.New("BoolValue: cannot use value as reference")
}
return v.ref.copy(), nil
}
func (v BoolValue) InternalWithRef(ref Reference) BoolValue {
return ReferenceAsBool(ref)
}