Skip to content
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

Refactor: mapping disks to API #302

Open
Tinyblargon opened this issue Jan 22, 2024 · 0 comments
Open

Refactor: mapping disks to API #302

Tinyblargon opened this issue Jan 22, 2024 · 0 comments
Assignees
Labels
type/enhancement An improvement of existing functionality

Comments

@Tinyblargon
Copy link
Collaborator

While working on #299 I realized the following code could easily be restructured to make it more readable.

func (storages QemuStorages) mapToApiValues(currentStorages QemuStorages, vmID, linkedVmId uint, params map[string]interface{}) (delete string) {
if storages.Ide != nil {
delete = storages.Ide.mapToApiValues(currentStorages.Ide, vmID, linkedVmId, params, delete)
}
if storages.Sata != nil {
delete = storages.Sata.mapToApiValues(currentStorages.Sata, vmID, linkedVmId, params, delete)
}
if storages.Scsi != nil {
delete = storages.Scsi.mapToApiValues(currentStorages.Scsi, vmID, linkedVmId, params, delete)
}
if storages.VirtIO != nil {
delete = storages.VirtIO.mapToApiValues(currentStorages.VirtIO, vmID, linkedVmId, params, delete)
}
return delete
}

This restructure would get rid of the following code:

tmpCurrentDisks := QemuIdeDisks{}
if currentDisks != nil {
tmpCurrentDisks = *currentDisks
}
diskMap := disks.mapToIntMap()
currentDiskMap := tmpCurrentDisks.mapToIntMap()
for i := range diskMap {
delete = diskMap[i].convertDataStructure().mapToApiValues(currentDiskMap[i].convertDataStructure(), vmID, LinkedVmId, QemuDiskId("ide"+strconv.Itoa(int(i))), params, delete)
}
return delete
}
func (disks QemuIdeDisks) mapToIntMap() map[uint8]*QemuIdeStorage {
return map[uint8]*QemuIdeStorage{
0: disks.Disk_0,
1: disks.Disk_1,
2: disks.Disk_2,
3: disks.Disk_3,
}
}

tmpCurrentDisks := QemuSataDisks{}
if currentDisks != nil {
tmpCurrentDisks = *currentDisks
}
diskMap := disks.mapToIntMap()
currentDiskMap := tmpCurrentDisks.mapToIntMap()
for i := range diskMap {
delete = diskMap[i].convertDataStructure().mapToApiValues(currentDiskMap[i].convertDataStructure(), vmID, LinkedVmId, QemuDiskId("sata"+strconv.Itoa(int(i))), params, delete)
}
return delete
}
func (disks QemuSataDisks) mapToIntMap() map[uint8]*QemuSataStorage {
return map[uint8]*QemuSataStorage{
0: disks.Disk_0,
1: disks.Disk_1,
2: disks.Disk_2,
3: disks.Disk_3,
4: disks.Disk_4,
5: disks.Disk_5,
}
}

tmpCurrentDisks := QemuScsiDisks{}
if currentDisks != nil {
tmpCurrentDisks = *currentDisks
}
diskMap := disks.mapToIntMap()
currentDiskMap := tmpCurrentDisks.mapToIntMap()
for i := range diskMap {
delete = diskMap[i].convertDataStructure().mapToApiValues(currentDiskMap[i].convertDataStructure(), vmID, linkedVmId, QemuDiskId("scsi"+strconv.Itoa(int(i))), params, delete)
}
return delete
}
func (disks QemuScsiDisks) mapToIntMap() map[uint8]*QemuScsiStorage {
return map[uint8]*QemuScsiStorage{
0: disks.Disk_0,
1: disks.Disk_1,
2: disks.Disk_2,
3: disks.Disk_3,
4: disks.Disk_4,
5: disks.Disk_5,
6: disks.Disk_6,
7: disks.Disk_7,
8: disks.Disk_8,
9: disks.Disk_9,
10: disks.Disk_10,
11: disks.Disk_11,
12: disks.Disk_12,
13: disks.Disk_13,
14: disks.Disk_14,
15: disks.Disk_15,
16: disks.Disk_16,
17: disks.Disk_17,
18: disks.Disk_18,
19: disks.Disk_19,
20: disks.Disk_20,
21: disks.Disk_21,
22: disks.Disk_22,
23: disks.Disk_23,
24: disks.Disk_24,
25: disks.Disk_25,
26: disks.Disk_26,
27: disks.Disk_27,
28: disks.Disk_28,
29: disks.Disk_29,
30: disks.Disk_30,
}
}

tmpCurrentDisks := QemuVirtIODisks{}
if currentDisks != nil {
tmpCurrentDisks = *currentDisks
}
diskMap := disks.mapToIntMap()
currentDiskMap := tmpCurrentDisks.mapToIntMap()
for i := range diskMap {
delete = diskMap[i].convertDataStructure().mapToApiValues(currentDiskMap[i].convertDataStructure(), vmID, linkedVmId, QemuDiskId("virtio"+strconv.Itoa(int(i))), params, delete)
}
return delete
}
func (disks QemuVirtIODisks) mapToIntMap() map[uint8]*QemuVirtIOStorage {
return map[uint8]*QemuVirtIOStorage{
0: disks.Disk_0,
1: disks.Disk_1,
2: disks.Disk_2,
3: disks.Disk_3,
4: disks.Disk_4,
5: disks.Disk_5,
6: disks.Disk_6,
7: disks.Disk_7,
8: disks.Disk_8,
9: disks.Disk_9,
10: disks.Disk_10,
11: disks.Disk_11,
12: disks.Disk_12,
13: disks.Disk_13,
14: disks.Disk_14,
15: disks.Disk_15,
}
}

@Tinyblargon Tinyblargon added the type/enhancement An improvement of existing functionality label Jan 22, 2024
@Tinyblargon Tinyblargon self-assigned this Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement An improvement of existing functionality
Projects
None yet
Development

No branches or pull requests

1 participant