-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatten.go
194 lines (168 loc) · 4.23 KB
/
flatten.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
190
191
192
193
194
package tfconv
import (
"errors"
"fmt"
"reflect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Flatten converts an object into a Terraform data structure using its schema.
func (c *Converter) Flatten(obj any, s map[string]*schema.Schema) (any, error) {
v := reflect.ValueOf(obj)
return c.flattenStruct(v, s)
}
func (c *Converter) flatten(v reflect.Value, s *schema.Schema) (any, error) {
switch {
case s.Type == schema.TypeInvalid:
return nil, errors.New("invalid schema type")
case s.Type == schema.TypeList && s.MaxItems == 1 && isResource(s.Elem):
res := s.Elem.(*schema.Resource)
if v.Type().Kind() == reflect.Ptr {
_, hasConversion := c.conversions[v.Type().Elem()]
if v.Type().Elem().Kind() != reflect.Struct || hasConversion {
val, err := c.flattenPrimitive(v, res.Schema["value"])
if err != nil {
return nil, err
}
return []any{map[string]any{"value": val}}, nil
}
}
return c.flattenStruct(v, res.Schema)
case s.Type == schema.TypeList:
return c.flattenSlice(v, s)
case s.Type == schema.TypeMap:
return c.flattenMap(v, s)
default:
return c.flattenPrimitive(v, s)
}
}
func (c *Converter) flattenSlice(v reflect.Value, s *schema.Schema) (any, error) {
t := v.Type()
if t.Kind() != reflect.Slice {
return nil, fmt.Errorf("expected slice, got %s", t.String())
}
if v.IsNil() || v.Len() == 0 {
return []any{}, nil
}
var elemS *schema.Schema
switch {
case isSchema(s.Elem):
elemS = s.Elem.(*schema.Schema)
case isResource(s.Elem):
elemS = &schema.Schema{}
*elemS = *s
elemS.MaxItems = 1
default:
// Do nothing.
}
d := make([]any, v.Len())
for i := range v.Len() {
a, err := c.flatten(v.Index(i), elemS)
if err != nil {
return nil, err
}
if isResource(s.Elem) {
// This is for the edge case where a slice of structs should
// not return a []any[]any. Unwind the second slice.
a = a.([]any)[0]
}
d[i] = a
}
return d, nil
}
func (c *Converter) flattenMap(v reflect.Value, s *schema.Schema) (any, error) {
t := v.Type()
if t.Kind() != reflect.Map {
return nil, fmt.Errorf("expected map, got %s", t.String())
}
if v.Len() == 0 {
return map[string]any{}, nil
}
elemS := s.Elem.(*schema.Schema)
d := make(map[string]any, v.Len())
for _, k := range v.MapKeys() {
a, err := c.flatten(v.MapIndex(k), elemS)
if err != nil {
return nil, err
}
d[k.String()] = a
}
return d, nil
}
func (c *Converter) flattenStruct(v reflect.Value, s map[string]*schema.Schema) (any, error) {
if v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
t := v.Type()
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected struct, got %s", t.String())
}
d := make(map[string]any, t.NumField())
for i := range t.NumField() {
sf := t.Field(i)
name := c.resolveName(sf)
sch, found := s[name]
if !found {
continue
}
fv := v.Field(i)
if fv.IsZero() {
continue
}
val, err := c.flatten(fv, sch)
if err != nil {
return nil, err
}
d[name] = val
}
return []any{d}, nil
}
func (c *Converter) flattenPrimitive(v reflect.Value, s *schema.Schema) (any, error) {
if v.Type().Kind() == reflect.Ptr {
if v.IsNil() {
//nolint:nilnil // This is expected.
return nil, nil
}
v = v.Elem()
}
t := v.Type()
if con, ok := c.conversions[t]; ok {
obj, err := con.flatten(v.Interface())
if err != nil {
return nil, err
}
v = reflect.ValueOf(obj)
t = v.Type()
}
switch s.Type {
case schema.TypeString:
if t.Kind() == reflect.String {
return v.String(), nil
}
case schema.TypeInt:
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int(v.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
//nolint:gosec // I see no way around this.
return int(v.Uint()), nil
}
case schema.TypeFloat:
switch t.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float(), nil
}
case schema.TypeBool:
if t.Kind() == reflect.Bool {
return v.Bool(), nil
}
}
return nil, fmt.Errorf("unsupported primitve type %s for schema type %s", t.String(), s.Type.String())
}
func isResource(v any) bool {
_, ok := v.(*schema.Resource)
return ok
}
func isSchema(v any) bool {
_, ok := v.(*schema.Schema)
return ok
}