-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdecode.go
More file actions
120 lines (102 loc) · 2.86 KB
/
decode.go
File metadata and controls
120 lines (102 loc) · 2.86 KB
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
// Copyright IBM Corp. 2017, 2025
// SPDX-License-Identifier: MPL-2.0
package functions
import (
"context"
"fmt"
"math/big"
"regexp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"sigs.k8s.io/yaml"
)
var documentSeparator = regexp.MustCompile(`(:?^|\s*\n)---\s*`)
func decode(ctx context.Context, manifest string) (v types.Tuple, diags diag.Diagnostics) {
docs := documentSeparator.Split(manifest, -1)
dtypes := []attr.Type{}
dvalues := []attr.Value{}
diags = diag.Diagnostics{}
for _, d := range docs {
var data map[string]any
err := yaml.Unmarshal([]byte(d), &data)
if err != nil {
diags.Append(diag.NewErrorDiagnostic("Invalid YAML document", err.Error()))
return
}
if len(data) == 0 {
diags.Append(diag.NewWarningDiagnostic("Empty document", "encountered a YAML document with no values"))
continue
}
if err := validateKubernetesManifest(data); err != nil {
diags.Append(diag.NewErrorDiagnostic("Invalid Kubernetes manifest", err.Error()))
return
}
obj, d := decodeScalar(ctx, data)
diags.Append(d...)
if diags.HasError() {
return
}
dtypes = append(dtypes, obj.Type(ctx))
dvalues = append(dvalues, obj)
}
return types.TupleValue(dtypes, dvalues)
}
func decodeMapping(ctx context.Context, m map[string]any) (attr.Value, diag.Diagnostics) {
vm := make(map[string]attr.Value, len(m))
tm := make(map[string]attr.Type, len(m))
for k, v := range m {
vv, diags := decodeScalar(ctx, v)
if diags.HasError() {
return nil, diags
}
vm[k] = vv
tm[k] = vv.Type(ctx)
}
return types.ObjectValue(tm, vm)
}
func decodeSequence(ctx context.Context, s []any) (attr.Value, diag.Diagnostics) {
vl := make([]attr.Value, len(s))
tl := make([]attr.Type, len(s))
for i, v := range s {
vv, diags := decodeScalar(ctx, v)
if diags.HasError() {
return nil, diags
}
vl[i] = vv
tl[i] = vv.Type(ctx)
}
return types.TupleValue(tl, vl)
}
func decodeScalar(ctx context.Context, m any) (value attr.Value, diags diag.Diagnostics) {
switch v := m.(type) {
case nil:
value = types.DynamicNull()
case float64:
value = types.NumberValue(big.NewFloat(float64(v)))
case bool:
value = types.BoolValue(v)
case string:
value = types.StringValue(v)
case []any:
return decodeSequence(ctx, v)
case map[string]any:
return decodeMapping(ctx, v)
default:
diags.Append(diag.NewErrorDiagnostic("failed to decode", fmt.Sprintf("unexpected type: %T for value %#v", v, v)))
}
return
}
func validateKubernetesManifest(m map[string]any) error {
// NOTE: a Kubernetes manifest should have:
// - an apiVersion
// - a kind
// - a metadata field
for _, k := range []string{"apiVersion", "kind", "metadata"} {
_, ok := m[k]
if !ok {
return fmt.Errorf("missing field %q", k)
}
}
return nil
}