Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit c067393

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

File tree

1 file changed

+87
-39
lines changed

1 file changed

+87
-39
lines changed

pkg/sync/sync_context.go

Lines changed: 87 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ func (sc *syncContext) shouldUseServerSideApply(targetObj *unstructured.Unstruct
11101110
return sc.serverSideApply || resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
11111111
}
11121112

1113+
<<<<<<< HEAD
11131114
// needsClientSideApplyMigration checks if a resource has fields managed by the specified manager
11141115
// that need to be migrated to the server-side apply manager
11151116
func (sc *syncContext) needsClientSideApplyMigration(liveObj *unstructured.Unstructured, fieldManager string) bool {
@@ -1156,56 +1157,103 @@ func (sc *syncContext) performClientSideApplyMigration(targetObj *unstructured.U
11561157
return nil
11571158
}
11581159

1160+
=======
1161+
// formatValue converts any value to its string representation with special handling for
1162+
// templates, maps, and strings. Returns "<nil>" for nil values.
1163+
>>>>>>> 3df63b1 (refactor formatValue method to reduce complexity)
11591164
func formatValue(v any) string {
11601165
if v == nil {
11611166
return "<nil>"
11621167
}
11631168

1164-
// Special case for volumeClaimTemplates
1165-
if templates, ok := v.([]any); ok {
1166-
// For a single volumeClaimTemplate field change
1167-
if len(templates) == 1 {
1168-
if template, ok := templates[0].(map[string]any); ok {
1169-
if storage := getTemplateStorage(template); storage != "" {
1170-
return fmt.Sprintf("%q", storage)
1171-
}
1172-
}
1173-
}
1174-
// For multiple templates or other array types format
1175-
var names []string
1176-
for _, t := range templates {
1177-
if template, ok := t.(map[string]any); ok {
1178-
if metadata, ok := template["metadata"].(map[string]any); ok {
1179-
if name, ok := metadata["name"].(string); ok {
1180-
if storage := getTemplateStorage(template); storage != "" {
1181-
names = append(names, fmt.Sprintf("%s(%s)", name, storage))
1182-
continue
1183-
}
1184-
names = append(names, name)
1185-
}
1186-
}
1187-
}
1169+
switch val := v.(type) {
1170+
case []any:
1171+
return formatTemplates(val)
1172+
case map[string]any:
1173+
return formatMap(val)
1174+
case string:
1175+
return fmt.Sprintf("%q", val)
1176+
default:
1177+
return fmt.Sprintf("%v", val)
1178+
}
1179+
}
1180+
1181+
// formatTemplates handles the formatting of volumeClaimTemplates arrays.
1182+
// For single templates with storage, returns the storage value.
1183+
// For multiple templates, returns a formatted list of template names with storage.
1184+
func formatTemplates(templates []any) string {
1185+
if len(templates) == 1 {
1186+
if storage := getSingleTemplateStorage(templates[0]); storage != "" {
1187+
return fmt.Sprintf("%q", storage)
11881188
}
1189-
return fmt.Sprintf("[%s]", strings.Join(names, ", "))
11901189
}
1190+
return formatMultipleTemplates(templates)
1191+
}
11911192

1192-
// Special case for selector matchLabels
1193-
if m, ok := v.(map[string]any); ok {
1194-
if matchLabels, exists := m["matchLabels"].(map[string]any); exists {
1195-
var labels []string
1196-
for k, v := range matchLabels {
1197-
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
1198-
}
1199-
sort.Strings(labels)
1200-
return fmt.Sprintf("{%s}", strings.Join(labels, ", "))
1193+
// getSingleTemplateStorage extracts storage value from a single template.
1194+
// Returns empty string if template is invalid or has no storage.
1195+
func getSingleTemplateStorage(t any) string {
1196+
template, ok := t.(map[string]any)
1197+
if !ok {
1198+
return ""
1199+
}
1200+
return getTemplateStorage(template)
1201+
}
1202+
1203+
// formatMultipleTemplates formats an array of templates into a string list
1204+
// of template names, optionally including storage information.
1205+
func formatMultipleTemplates(templates []any) string {
1206+
var names []string
1207+
for _, t := range templates {
1208+
if name := getTemplateName(t); name != "" {
1209+
names = append(names, name)
12011210
}
12021211
}
1203-
// Add quotes for string values
1204-
if str, ok := v.(string); ok {
1205-
return fmt.Sprintf("%q", str)
1212+
return fmt.Sprintf("[%s]", strings.Join(names, ", "))
1213+
}
1214+
1215+
// getTemplateName extracts and formats the name from a template.
1216+
// If template has storage, appends it to the name in parentheses.
1217+
func getTemplateName(t any) string {
1218+
template, ok := t.(map[string]any)
1219+
if !ok {
1220+
return ""
1221+
}
1222+
1223+
metadata, ok := template["metadata"].(map[string]any)
1224+
if !ok {
1225+
return ""
1226+
}
1227+
1228+
name, ok := metadata["name"].(string)
1229+
if !ok {
1230+
return ""
1231+
}
1232+
1233+
if storage := getTemplateStorage(template); storage != "" {
1234+
return fmt.Sprintf("%s(%s)", name, storage)
1235+
}
1236+
return name
1237+
}
1238+
1239+
// formatMap handles special case formatting for maps, particularly for matchLabels.
1240+
// Returns standard string representation for non-matchLabel maps.
1241+
func formatMap(m map[string]any) string {
1242+
if matchLabels, exists := m["matchLabels"].(map[string]any); exists {
1243+
return formatMatchLabels(matchLabels)
1244+
}
1245+
return fmt.Sprintf("%v", m)
1246+
}
1247+
1248+
// formatMatchLabels converts a matchLabels map into a sorted string list
1249+
// of key:value pairs enclosed in curly braces.
1250+
func formatMatchLabels(matchLabels map[string]any) string {
1251+
var labels []string
1252+
for k, v := range matchLabels {
1253+
labels = append(labels, fmt.Sprintf("%s:%s", k, v))
12061254
}
1207-
// For other types, use standard formatting
1208-
return fmt.Sprintf("%v", v)
1255+
sort.Strings(labels)
1256+
return fmt.Sprintf("{%s}", strings.Join(labels, ", "))
12091257
}
12101258

12111259
// Get storage size from template

0 commit comments

Comments
 (0)