forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
189 lines (173 loc) · 4.89 KB
/
encode.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package hcl
import (
"fmt"
"reflect"
"sort"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)
// OnEncodeBlock is called during encoding of a struct to HCL.
type OnEncodeBlock interface {
OnEncodeBlock(block *Block) hcl.Diagnostics
}
// Encode encodes a struct to HCL.
// The value must be a struct.
func Encode(val any, block *Block) hcl.Diagnostics {
return encodeSingleBlock(reflect.ValueOf(val), block, "", nil)
}
// EncodeBlock encodes a struct to HCL block and appends it to the given block.
// The value must be a struct.
//
// The typeName argument specifies the block type name. Labels are specified
// using the labels argument. If labels is nil, labels are read from the struct
// tags.
func EncodeBlock(val any, block *Block, typeName string, labels []string) hcl.Diagnostics {
if typeName == "" {
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Encode error",
Detail: "EncodeBlock requires a non-empty typeName",
}}
}
return encodeSingleBlock(reflect.ValueOf(val), block, typeName, labels)
}
// encodeSingleBlock encodes given struct value as HCL block.
//
//nolint:gocyclo
func encodeSingleBlock(val reflect.Value, body *Block, typeName string, labels []string) hcl.Diagnostics {
val = derefValue(val)
if val.Kind() != reflect.Struct {
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Encode error",
Detail: fmt.Sprintf("Unable to encode %s as block; value must be a struct", val.Kind()),
}}
}
// Decode struct tags.
meta, diags := getStructMeta(val.Type())
if diags.HasErrors() {
return diags
}
// If typeName is empty, we are encoding the root block, so there is no need
// to create a new block.
if typeName != "" {
// Encode labels from fields with `label` tag, but only if labels are not
// already set.
if labels == nil {
for _, label := range meta.Labels {
var labelVal cty.Value
fieldRef := val.FieldByIndex(label.Reflect.Index)
labelRef := reflect.ValueOf(&labelVal)
if err := mapper.MapRefl(fieldRef, labelRef); err != nil {
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Decode error",
Detail: fmt.Sprintf("Cannot encode label %q: %s", label.Name, err),
}}
}
if labelVal.Type() != cty.String {
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Decode error",
Detail: fmt.Sprintf("Cannot encode label %q: value must be a string", label.Name),
}}
}
labels = append(labels, labelVal.AsString())
}
}
body = body.AppendBlock(typeName, labels)
}
// Encode attributes.
for _, attr := range meta.Attrs {
if attr.Ignore {
continue
}
field := val.FieldByIndex(attr.Reflect.Index)
if attr.Optional && canBeSkipped(field) {
continue
}
body.AppendAttribute(attr.Name, field.Interface())
}
// Encode blocks.
for _, block := range meta.Blocks {
if block.Ignore {
continue
}
field := val.FieldByIndex(block.Reflect.Index)
if block.Optional && canBeSkipped(field) {
continue
}
if block.Multiple {
diags := encodeMultipleBlocks(body, field, block.Name)
if diags.HasErrors() {
return diags
}
} else {
diags := encodeSingleBlock(field, body, block.Name, nil)
if diags.HasErrors() {
return diags
}
}
}
// Encode hook.
if n, ok := val.Interface().(OnEncodeBlock); ok {
diags := n.OnEncodeBlock(body)
if diags.HasErrors() {
return diags
}
}
return nil
}
// encodeMultipleBlocks encodes a map or slice value as HCL blocks.
func encodeMultipleBlocks(body *Block, val reflect.Value, typeName string) hcl.Diagnostics {
val = derefValue(val)
switch val.Kind() {
case reflect.Map:
if val.Type().Key().Kind() != reflect.String {
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Encode error",
Detail: "Unable to encode value as HCL; map key is not a string",
}}
}
// Keys are sorted to ensure consistent output.
keys := val.MapKeys()
sort.Slice(keys, func(i, j int) bool {
return keys[i].String() < keys[j].String()
})
for _, key := range keys {
diags := encodeSingleBlock(val.MapIndex(key), body, typeName, []string{key.String()})
if diags.HasErrors() {
return diags
}
}
return nil
case reflect.Slice:
for i := 0; i < val.Len(); i++ {
diags := encodeSingleBlock(val.Index(i), body, typeName, nil)
if diags.HasErrors() {
return diags
}
}
return nil
}
return hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Encode error",
Detail: fmt.Sprintf("Unable to encode %s as block", val.Kind()),
}}
}
// canBeSkipped returns true if the given value can be skipped during encoding
// to HCL if the field has the `optional` tag set.
func canBeSkipped(v reflect.Value) bool {
if !v.IsValid() {
// Can this happen?
return true
}
if v.Type().PkgPath() != "" {
// Custom types may implement MarshalHCL or MarshalText, so we cannot
// skip them.
return false
}
return v.IsZero()
}