-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
157 lines (138 loc) · 3.79 KB
/
Copy pathhelpers.go
File metadata and controls
157 lines (138 loc) · 3.79 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
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
package blackstart
import (
"fmt"
"reflect"
)
// InputAs converts an Input value into the requested type T.
// When required is true, nil/empty values for common scalar/list types are rejected.
func InputAs[T any](input Input, required bool) (T, error) {
var zero T
if input == nil {
if required {
return zero, fmt.Errorf("required input is missing")
}
return zero, nil
}
if !input.IsStatic() {
return zero, fmt.Errorf("input value is not static")
}
val := input.Any()
if val == nil {
if required {
return zero, fmt.Errorf("value cannot be nil")
}
return zero, nil
}
out, err := coerceValue[T](val)
if err != nil {
return zero, err
}
if required {
if err = validateRequiredValue(out); err != nil {
return zero, err
}
}
return out, nil
}
// ContextInputAs reads input `key` from a ModuleContext and converts it to type T.
func ContextInputAs[T any](ctx ModuleContext, key string, required bool) (T, error) {
var zero T
input, err := ctx.Input(key)
if err != nil {
if required {
return zero, fmt.Errorf("missing required input %s: %w", key, err)
}
return zero, nil
}
out, err := InputAs[T](input, required)
if err != nil {
return zero, fmt.Errorf("invalid input %s: %w", key, err)
}
return out, nil
}
// coerceValue converts a raw input value into T using assignment/conversion rules and
// special handling for common YAML/JSON decode shapes.
func coerceValue[T any](value any) (T, error) {
var zero T
targetType := reflect.TypeFor[T]()
// Special case: allow bool to satisfy *bool inputs.
if targetType.Kind() == reflect.Pointer && targetType.Elem().Kind() == reflect.Bool {
if b, ok := value.(bool); ok {
ptr := &b
return any(ptr).(T), nil
}
}
// Special case: YAML/JSON lists commonly decode to []any.
if targetType.Kind() == reflect.Slice {
elemType := targetType.Elem()
switch x := value.(type) {
case []any:
out := reflect.MakeSlice(targetType, 0, len(x))
for i, item := range x {
if item == nil {
return zero, fmt.Errorf("value[%d] must not be nil", i)
}
itemValue := reflect.ValueOf(item)
itemType := itemValue.Type()
if elemType.Kind() == reflect.String {
if itemType.Kind() != reflect.String {
return zero, fmt.Errorf("value[%d] must be string, got %T", i, item)
}
}
if itemType.AssignableTo(elemType) {
out = reflect.Append(out, itemValue)
continue
}
if itemType.ConvertibleTo(elemType) && elemType.Kind() != reflect.String {
out = reflect.Append(out, itemValue.Convert(elemType))
continue
}
return zero, fmt.Errorf("value[%d] must be %v, got %T", i, elemType, item)
}
return out.Interface().(T), nil
case string:
if elemType.Kind() == reflect.String {
out := reflect.MakeSlice(targetType, 1, 1)
out.Index(0).Set(reflect.ValueOf(x).Convert(elemType))
return out.Interface().(T), nil
}
}
}
if value == nil {
return zero, fmt.Errorf("value is nil")
}
v := reflect.ValueOf(value)
if v.Type().AssignableTo(targetType) {
return v.Interface().(T), nil
}
if v.Type().ConvertibleTo(targetType) {
cv := v.Convert(targetType)
return cv.Interface().(T), nil
}
return zero, fmt.Errorf("expected %v, got %T", targetType, value)
}
// validateRequiredValue enforces non-empty/non-nil semantics for required input values.
func validateRequiredValue[T any](value T) error {
v := any(value)
switch x := v.(type) {
case string:
if x == "" {
return fmt.Errorf("value cannot be empty")
}
case []string:
if len(x) == 0 {
return fmt.Errorf("value cannot be empty")
}
for i, s := range x {
if s == "" {
return fmt.Errorf("value[%d] cannot be empty", i)
}
}
default:
rv := reflect.ValueOf(v)
if rv.IsValid() && rv.Kind() == reflect.Pointer && rv.IsNil() {
return fmt.Errorf("value cannot be nil")
}
}
return nil
}