Skip to content

Commit b1f6fe9

Browse files
committed
roachprod: better wrapper validation
Previously, if we attempted to unmarshal malformed YAML or an invalid type the errors could be confusing. This change adds better error handling to the wrapper. Epic: None Release note: None
1 parent 20c3c8e commit b1f6fe9

File tree

1 file changed

+8
-1
lines changed
  • pkg/roachprod/roachprodutil/codec

1 file changed

+8
-1
lines changed

pkg/roachprod/roachprodutil/codec/types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ func (w Wrapper[T]) MarshalYAML() (any, error) {
149149
// was stored with metadata, we decode the type information and then decode the
150150
// value using the type information.
151151
func (w *Wrapper[T]) UnmarshalYAML(value *yaml.Node) error {
152+
if len(value.Content) != 4 {
153+
return fmt.Errorf("invalid wrapper")
154+
}
152155
ft := fieldType(value.Content[1].Value)
153156
typeName, err := ft.name()
154157
if err != nil {
@@ -166,8 +169,12 @@ func (w *Wrapper[T]) UnmarshalYAML(value *yaml.Node) error {
166169
if ft.isPointer() {
167170
objValue = reflect.ValueOf(objValuePtr).Interface()
168171
}
172+
castObj, ok := objValue.(T)
173+
if !ok {
174+
return fmt.Errorf("invalid type %T", objValue)
175+
}
169176
*w = Wrapper[T]{
170-
objValue.(T),
177+
castObj,
171178
}
172179
return nil
173180
}

0 commit comments

Comments
 (0)