-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sorted fields from values.schema.json #230
Conversation
Issue: cyclops-ui#228 1. Sorted the fields from values.schema.json into the alphabetical form when the order is not provided 2. When order is provided , the defined fields will come in order first and then the sorted fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @RohanMishra315 for the PR. I left a comment on the sorting logic
cyclops-ctrl/internal/mapper/helm.go
Outdated
// Custom order provided, sort fields based on the order | ||
ordersMap := make(map[string]int) | ||
|
||
// Map field names to their indices in the custom order | ||
for i, s := range order { | ||
ordersMap[s] = i | ||
} | ||
|
||
// Sort fields based on their indices in the custom order | ||
sort.Slice(fields, func(i, j int) bool { | ||
return ordersMap[fields[i].Name] < ordersMap[fields[j].Name] | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your solution works when the order is provided for all fields or not provided at all, but it doesn't work when 3/6 fields have the order defined. You can check the issue for further explanation of such a case.
I tried it with the following template, and I got the order from the picture.
repo: https://github.com/cyclops-ui/templates
path: demo-extended
version: field-alphabetical-sort
} |
Hey can you edit the comment again I think there's some mistake |
Sounds good to me. The only thing I wouldn't do is introduce a new struct, sort.Slice(fields, func(i, j int) bool {
return ordersMap[fields[i].Name] < ordersMap[fields[j].Name]
}) |
@RohanMishra315 feel free to update the PR with those changes |
Updated the sorting fields
cyclops-ctrl/internal/mapper/helm.go
Outdated
if len(order) == 0 { | ||
// Extract names of fields for sorting | ||
fieldNames := make([]string, len(fields)) | ||
for i, field := range fields { | ||
fieldNames[i] = field.Name | ||
} | ||
|
||
// Sort field names alphabetically | ||
sort.Strings(fieldNames) | ||
|
||
// Map field names to their indices in the sorted array | ||
fieldIndices := make(map[string]int) | ||
for i, name := range fieldNames { | ||
fieldIndices[name] = i | ||
} | ||
|
||
// Use the sorted indices to sort the fields | ||
sort.Slice(fields, func(i, j int) bool { | ||
return fieldIndices[fields[i].Name] < fieldIndices[fields[j].Name] | ||
}) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You dont need this part. The code in the else statement should handle it.
cyclops-ctrl/internal/mapper/helm.go
Outdated
} | ||
|
||
return fields | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You closed the function and returned after closing it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @RohanMishra315!
Issue: #228