Skip to content

Commit

Permalink
Fix slice value length assertion in conversion.Convert
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Apr 19, 2024
1 parent 6600707 commit 9a128e9
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/config/conversion/runtime_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ func Convert(params map[string]any, paths []string, mode Mode) (map[string]any,
return nil, errors.Wrapf(err, "cannot set the singleton list's value at the field path %s", exp[0])
}
case ToEmbeddedObject:
s, ok := v.([]any)
if !ok || len(s) > 1 {
// if len(s) is 0, then it's not a slice
return nil, errors.Errorf("singleton list, at the field path %s, must have a length of 1 but it has a length of %d", exp[0], len(s))
}
var newVal any = map[string]any{}
if len(s) > 0 {
newVal = s[0]
var newVal any = nil
if v != nil {
newVal = map[string]any{}
s, ok := v.([]any)
if !ok || len(s) > 1 {
// if len(s) is 0, then it's not a slice
return nil, errors.Errorf("singleton list, at the field path %s, must have a length of at most 1 but it has a length of %d", exp[0], len(s))
}
if len(s) > 0 {
newVal = s[0]
}
}
if err := setValue(pv, newVal, exp[0]); err != nil {
return nil, errors.Wrapf(err, "cannot set the embedded object's value at the field path %s", exp[0])
Expand Down

0 comments on commit 9a128e9

Please sign in to comment.