Skip to content

Commit 8897bce

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

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
@@ -1101,6 +1101,7 @@ func (sc *syncContext) shouldUseServerSideApply(targetObj *unstructured.Unstruct
11011101
return sc.serverSideApply || resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
11021102
}
11031103

1104+
<<<<<<< HEAD
11041105
// needsClientSideApplyMigration checks if a resource has fields managed by the specified manager
11051106
// that need to be migrated to the server-side apply manager
11061107
func (sc *syncContext) needsClientSideApplyMigration(liveObj *unstructured.Unstructured, fieldManager string) bool {
@@ -1147,56 +1148,103 @@ func (sc *syncContext) performClientSideApplyMigration(targetObj *unstructured.U
11471148
return nil
11481149
}
11491150

1151+
=======
1152+
// formatValue converts any value to its string representation with special handling for
1153+
// templates, maps, and strings. Returns "<nil>" for nil values.
1154+
>>>>>>> 3df63b1 (refactor formatValue method to reduce complexity)
11501155
func formatValue(v any) string {
11511156
if v == nil {
11521157
return "<nil>"
11531158
}
11541159

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

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

12021250
// Get storage size from template

0 commit comments

Comments
 (0)