Skip to content

Commit 3df63b1

Browse files
committed
refactor formatValue method to reduce complexity
Signed-off-by: Atif Ali <[email protected]>
1 parent cd6b3a6 commit 3df63b1

File tree

1 file changed

+84
-39
lines changed

1 file changed

+84
-39
lines changed

pkg/sync/sync_context.go

Lines changed: 84 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,56 +1031,101 @@ func (sc *syncContext) shouldUseServerSideApply(targetObj *unstructured.Unstruct
10311031
return sc.serverSideApply || resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
10321032
}
10331033

1034+
// formatValue converts any value to its string representation with special handling for
1035+
// templates, maps, and strings. Returns "<nil>" for nil values.
10341036
func formatValue(v any) string {
10351037
if v == nil {
10361038
return "<nil>"
10371039
}
10381040

1039-
// Special case for volumeClaimTemplates
1040-
if templates, ok := v.([]any); ok {
1041-
// For a single volumeClaimTemplate field change
1042-
if len(templates) == 1 {
1043-
if template, ok := templates[0].(map[string]any); ok {
1044-
if storage := getTemplateStorage(template); storage != "" {
1045-
return fmt.Sprintf("%q", storage)
1046-
}
1047-
}
1048-
}
1049-
// For multiple templates or other array types format
1050-
var names []string
1051-
for _, t := range templates {
1052-
if template, ok := t.(map[string]any); ok {
1053-
if metadata, ok := template["metadata"].(map[string]any); ok {
1054-
if name, ok := metadata["name"].(string); ok {
1055-
if storage := getTemplateStorage(template); storage != "" {
1056-
names = append(names, fmt.Sprintf("%s(%s)", name, storage))
1057-
continue
1058-
}
1059-
names = append(names, name)
1060-
}
1061-
}
1062-
}
1041+
switch val := v.(type) {
1042+
case []any:
1043+
return formatTemplates(val)
1044+
case map[string]any:
1045+
return formatMap(val)
1046+
case string:
1047+
return fmt.Sprintf("%q", val)
1048+
default:
1049+
return fmt.Sprintf("%v", val)
1050+
}
1051+
}
1052+
1053+
// formatTemplates handles the formatting of volumeClaimTemplates arrays.
1054+
// For single templates with storage, returns the storage value.
1055+
// For multiple templates, returns a formatted list of template names with storage.
1056+
func formatTemplates(templates []any) string {
1057+
if len(templates) == 1 {
1058+
if storage := getSingleTemplateStorage(templates[0]); storage != "" {
1059+
return fmt.Sprintf("%q", storage)
10631060
}
1064-
return fmt.Sprintf("[%s]", strings.Join(names, ", "))
10651061
}
1062+
return formatMultipleTemplates(templates)
1063+
}
10661064

1067-
// Special case for selector matchLabels
1068-
if m, ok := v.(map[string]any); ok {
1069-
if matchLabels, exists := m["matchLabels"].(map[string]any); exists {
1070-
var labels []string
1071-
for k, v := range matchLabels {
1072-
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
1073-
}
1074-
sort.Strings(labels)
1075-
return fmt.Sprintf("{%s}", strings.Join(labels, ", "))
1065+
// getSingleTemplateStorage extracts storage value from a single template.
1066+
// Returns empty string if template is invalid or has no storage.
1067+
func getSingleTemplateStorage(t any) string {
1068+
template, ok := t.(map[string]any)
1069+
if !ok {
1070+
return ""
1071+
}
1072+
return getTemplateStorage(template)
1073+
}
1074+
1075+
// formatMultipleTemplates formats an array of templates into a string list
1076+
// of template names, optionally including storage information.
1077+
func formatMultipleTemplates(templates []any) string {
1078+
var names []string
1079+
for _, t := range templates {
1080+
if name := getTemplateName(t); name != "" {
1081+
names = append(names, name)
10761082
}
10771083
}
1078-
// Add quotes for string values
1079-
if str, ok := v.(string); ok {
1080-
return fmt.Sprintf("%q", str)
1084+
return fmt.Sprintf("[%s]", strings.Join(names, ", "))
1085+
}
1086+
1087+
// getTemplateName extracts and formats the name from a template.
1088+
// If template has storage, appends it to the name in parentheses.
1089+
func getTemplateName(t any) string {
1090+
template, ok := t.(map[string]any)
1091+
if !ok {
1092+
return ""
1093+
}
1094+
1095+
metadata, ok := template["metadata"].(map[string]any)
1096+
if !ok {
1097+
return ""
1098+
}
1099+
1100+
name, ok := metadata["name"].(string)
1101+
if !ok {
1102+
return ""
1103+
}
1104+
1105+
if storage := getTemplateStorage(template); storage != "" {
1106+
return fmt.Sprintf("%s(%s)", name, storage)
1107+
}
1108+
return name
1109+
}
1110+
1111+
// formatMap handles special case formatting for maps, particularly for matchLabels.
1112+
// Returns standard string representation for non-matchLabel maps.
1113+
func formatMap(m map[string]any) string {
1114+
if matchLabels, exists := m["matchLabels"].(map[string]any); exists {
1115+
return formatMatchLabels(matchLabels)
1116+
}
1117+
return fmt.Sprintf("%v", m)
1118+
}
1119+
1120+
// formatMatchLabels converts a matchLabels map into a sorted string list
1121+
// of key:value pairs enclosed in curly braces.
1122+
func formatMatchLabels(matchLabels map[string]any) string {
1123+
var labels []string
1124+
for k, v := range matchLabels {
1125+
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
10811126
}
1082-
// For other types, use standard formatting
1083-
return fmt.Sprintf("%v", v)
1127+
sort.Strings(labels)
1128+
return fmt.Sprintf("{%s}", strings.Join(labels, ", "))
10841129
}
10851130

10861131
// Get storage size from template

0 commit comments

Comments
 (0)