Skip to content

Commit 5602951

Browse files
authored
refactor(misconf): move common logic to base value and simplify typed values (#9986)
Signed-off-by: nikpivkin <[email protected]>
1 parent 809db46 commit 5602951

File tree

8 files changed

+165
-410
lines changed

8 files changed

+165
-410
lines changed

pkg/iac/types/base_value.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
type BaseValue[T any] struct {
8+
metadata Metadata
9+
value T
10+
}
11+
12+
func defaultValue[T any](value T, m Metadata) BaseValue[T] {
13+
m.isDefault = true
14+
return newValue(value, m)
15+
}
16+
17+
func unresolvableValue[T any](m Metadata) BaseValue[T] {
18+
m.isUnresolvable = true
19+
var zero T
20+
return newValue(zero, m)
21+
}
22+
23+
func explicitValue[T any](value T, m Metadata) BaseValue[T] {
24+
m.isExplicit = true
25+
return newValue(value, m)
26+
}
27+
28+
func testValue[T any](value T) BaseValue[T] {
29+
return newValue(value, NewTestMetadata())
30+
}
31+
32+
func newValue[T any](val T, metadata Metadata) BaseValue[T] {
33+
return BaseValue[T]{
34+
metadata: metadata,
35+
value: val,
36+
}
37+
}
38+
39+
func (v BaseValue[T]) GetMetadata() Metadata {
40+
return v.metadata
41+
}
42+
43+
func (v BaseValue[T]) Value() T {
44+
return v.value
45+
}
46+
47+
func (v BaseValue[T]) GetRawValue() any {
48+
return v.value
49+
}
50+
51+
func (v BaseValue[T]) ToRego() any {
52+
m := v.metadata.ToRego().(map[string]any)
53+
m["value"] = v.value
54+
return m
55+
}
56+
57+
type encodedValue[T any] struct {
58+
Value T `json:"value"`
59+
Metadata Metadata `json:"metadata"`
60+
}
61+
62+
func (v BaseValue[T]) MarshalJSON() ([]byte, error) {
63+
ev := encodedValue[T]{
64+
Value: v.value,
65+
Metadata: v.metadata,
66+
}
67+
return json.Marshal(ev)
68+
}
69+
70+
func (v *BaseValue[T]) UnmarshalJSON(data []byte) error {
71+
var ev encodedValue[T]
72+
if err := json.Unmarshal(data, &ev); err != nil {
73+
return err
74+
}
75+
76+
v.value = ev.Value
77+
v.metadata = ev.Metadata
78+
return nil
79+
}

pkg/iac/types/bool.go

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,33 @@
11
package types
22

33
import (
4-
"encoding/json"
54
"strings"
65

76
"github.com/zclconf/go-cty/cty"
87
)
98

109
type BoolValue struct {
11-
BaseAttribute
12-
value bool
10+
BaseValue[bool]
1311
}
1412

15-
func (b BoolValue) MarshalJSON() ([]byte, error) {
16-
return json.Marshal(map[string]any{
17-
"value": b.value,
18-
"metadata": b.metadata,
19-
})
13+
func Bool(value bool, m Metadata) BoolValue {
14+
return BoolValue{newValue(value, m)}
2015
}
2116

22-
func (b *BoolValue) UnmarshalJSON(data []byte) error {
23-
var keys map[string]any
24-
if err := json.Unmarshal(data, &keys); err != nil {
25-
return err
26-
}
27-
if keys["value"] != nil {
28-
b.value = keys["value"].(bool)
29-
}
30-
if keys["metadata"] != nil {
31-
raw, err := json.Marshal(keys["metadata"])
32-
if err != nil {
33-
return err
34-
}
35-
var m Metadata
36-
if err := json.Unmarshal(raw, &m); err != nil {
37-
return err
38-
}
39-
b.metadata = m
40-
}
41-
return nil
42-
}
43-
44-
func Bool(value bool, metadata Metadata) BoolValue {
45-
return BoolValue{
46-
value: value,
47-
BaseAttribute: BaseAttribute{metadata: metadata},
48-
}
49-
}
50-
51-
func BoolTest(value bool) BoolValue {
52-
return Bool(value, NewTestMetadata())
53-
}
54-
55-
func BoolDefault(value bool, metadata Metadata) BoolValue {
56-
b := Bool(value, metadata)
57-
b.BaseAttribute.metadata.isDefault = true
58-
return b
17+
func BoolDefault(value bool, m Metadata) BoolValue {
18+
return BoolValue{defaultValue(value, m)}
5919
}
6020

6121
func BoolUnresolvable(m Metadata) BoolValue {
62-
b := Bool(false, m)
63-
b.BaseAttribute.metadata.isUnresolvable = true
64-
return b
65-
}
66-
67-
func BoolExplicit(value bool, metadata Metadata) BoolValue {
68-
b := Bool(value, metadata)
69-
b.BaseAttribute.metadata.isExplicit = true
70-
return b
22+
return BoolValue{unresolvableValue[bool](m)}
7123
}
7224

73-
func (b BoolValue) Value() bool {
74-
return b.value
25+
func BoolExplicit(value bool, m Metadata) BoolValue {
26+
return BoolValue{explicitValue(value, m)}
7527
}
7628

77-
func (b BoolValue) GetRawValue() any {
78-
return b.value
29+
func BoolTest(value bool) BoolValue {
30+
return BoolValue{testValue(value)}
7931
}
8032

8133
func (b BoolValue) IsTrue() bool {
@@ -93,16 +45,7 @@ func (b BoolValue) IsFalse() bool {
9345
}
9446

9547
func (b BoolValue) Invert() BoolValue {
96-
return BoolValue{
97-
BaseAttribute: b.BaseAttribute,
98-
value: !b.value,
99-
}
100-
}
101-
102-
func (b BoolValue) ToRego() any {
103-
m := b.metadata.ToRego().(map[string]any)
104-
m["value"] = b.Value()
105-
return m
48+
return Bool(!b.value, b.metadata)
10649
}
10750

10851
// BoolFromCtyValue converts a cty.Value to iacTypes.BoolValue.

pkg/iac/types/bytes.go

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,35 @@
11
package types
22

3-
import (
4-
"encoding/json"
5-
)
6-
73
type BytesValue struct {
8-
BaseAttribute
9-
value []byte
10-
}
11-
12-
func (b BytesValue) MarshalJSON() ([]byte, error) {
13-
return json.Marshal(map[string]any{
14-
"value": b.value,
15-
"metadata": b.metadata,
16-
})
17-
}
18-
19-
func (b *BytesValue) UnmarshalJSON(data []byte) error {
20-
var keys map[string]any
21-
if err := json.Unmarshal(data, &keys); err != nil {
22-
return err
23-
}
24-
if keys["value"] != nil {
25-
raw, err := json.Marshal(keys["value"])
26-
if err != nil {
27-
return err
28-
}
29-
var m []byte
30-
if err := json.Unmarshal(raw, &m); err != nil {
31-
return err
32-
}
33-
b.value = m
34-
}
35-
if keys["metadata"] != nil {
36-
raw, err := json.Marshal(keys["metadata"])
37-
if err != nil {
38-
return err
39-
}
40-
var m Metadata
41-
if err := json.Unmarshal(raw, &m); err != nil {
42-
return err
43-
}
44-
b.metadata = m
45-
}
46-
return nil
47-
}
48-
49-
func (b BytesValue) Value() []byte {
50-
return b.value
51-
}
52-
53-
func (b BytesValue) GetRawValue() any {
54-
return b.value
55-
}
56-
57-
func (b BytesValue) Len() int {
58-
return len(b.value)
59-
}
60-
61-
func (b BytesValue) GetMetadata() Metadata {
62-
return b.metadata
4+
BaseValue[[]byte]
635
}
646

657
func Bytes(value []byte, m Metadata) BytesValue {
66-
return BytesValue{
67-
value: value,
68-
BaseAttribute: BaseAttribute{metadata: m},
69-
}
8+
return BytesValue{newValue(value, m)}
709
}
7110

7211
func BytesDefault(value []byte, m Metadata) BytesValue {
73-
b := Bytes(value, m)
74-
b.BaseAttribute.metadata.isDefault = true
75-
return b
12+
return BytesValue{defaultValue(value, m)}
7613
}
7714

7815
func BytesExplicit(value []byte, m Metadata) BytesValue {
79-
b := Bytes(value, m)
80-
b.BaseAttribute.metadata.isExplicit = true
81-
return b
16+
return BytesValue{explicitValue(value, m)}
8217
}
8318

8419
func BytesUnresolvable(m Metadata) BytesValue {
85-
b := Bytes(nil, m)
86-
b.BaseAttribute.metadata.isUnresolvable = true
87-
return b
20+
return BytesValue{unresolvableValue[[]byte](m)}
8821
}
8922

9023
func BytesTest(value []byte) BytesValue {
91-
b := Bytes(value, NewTestMetadata())
92-
b.BaseAttribute.metadata.isUnresolvable = true
93-
return b
24+
return BytesValue{testValue(value)}
9425
}
9526

96-
func (b BytesValue) ToRego() any {
97-
m := b.metadata.ToRego().(map[string]any)
98-
m["value"] = string(b.Value())
27+
func (v BytesValue) ToRego() any {
28+
m := v.metadata.ToRego().(map[string]any)
29+
m["value"] = string(v.value)
9930
return m
10031
}
32+
33+
func (v BytesValue) Len() int {
34+
return len(v.value)
35+
}

0 commit comments

Comments
 (0)