From 0c57170b3ca395e3f866c4e5fc04e3d64e1b4c97 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 19 Apr 2024 09:47:35 +0300 Subject: [PATCH] Fix slice value length assertion in conversion.Convert Signed-off-by: Alper Rifat Ulucinar --- pkg/config/conversion/conversions.go | 2 +- pkg/config/conversion/runtime_conversion.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/config/conversion/conversions.go b/pkg/config/conversion/conversions.go index a2b33d28..e25c42cd 100644 --- a/pkg/config/conversion/conversions.go +++ b/pkg/config/conversion/conversions.go @@ -209,7 +209,7 @@ func (s *singletonListConverter) ConvertPaved(src, target *fieldpath.Paved) (boo return true, errors.Errorf("value at path %s is not a map[string]any", pathForProvider) } if _, err := Convert(m, s.crdPaths, s.mode); err != nil { - return true, errors.Wrapf(err, "failed to convert the source map in mode %q with %s", s.mode, s.baseConversion) + return true, errors.Wrapf(err, "failed to convert the source map in mode %q with %s", s.mode, s.baseConversion.String()) } return true, errors.Wrapf(target.SetValue(pathForProvider, m), "failed to set the %s value for conversion in mode %q", pathForProvider, s.mode) } diff --git a/pkg/config/conversion/runtime_conversion.go b/pkg/config/conversion/runtime_conversion.go index 4f2217a6..73d8fb89 100644 --- a/pkg/config/conversion/runtime_conversion.go +++ b/pkg/config/conversion/runtime_conversion.go @@ -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])