Loop over a dynamic nested struct in golang

huangapple go评论168阅读模式
英文:

Loop over a dynamic nested struct in golang

问题

我已经创建了一个动态结构体来解析JSON。结构体如下:

type Partition struct {
    DiskName      string      `json:"disk_name"`
    Mountpoint    interface{} `json:"mountpoint,omitempty"`
    Size          string      `json:"size"`
    Fstype        string      `json:"fstype,omitempty"`
    SubPartitions bool        `json:"sub_partitions"`
    Partitions    []Partition `json:"partitions,omitempty"`
}

type NasInfo struct {
    Blockdevices []struct {
        DiskName   string      `json:"disk_name"`
        Mountpoint interface{} `json:"mountpoint,omitempty"`
        Size       string      `json:"size"`
        Fstype     string      `json:"fstype,omitempty"`
        Partitions []Partition `json:"partitions,omitempty"`
    } `json:"blockdevices"`
}

现在,一个块设备中可以有多个分区,而一个分区中又可以有更多的子分区。我想手动为分区结构体中的字段赋值。我该如何做到这一点?我该如何迭代每个分区和子分区,并为其赋值?有没有办法可以做到这一点?

目前我使用的代码只能处理一个分区和一个子分区。有没有办法可以迭代每个分区并为其赋值?

英文:

I have created a dynamic struct to unmarshal a JSON.
Struct looks like :

type Partition struct {
	DiskName      string      `json:"disk_name"`
	Mountpoint    interface{} `json:"mountpoint,omitempty"`
	Size          string      `json:"size"`
	Fstype        string      `json:"fstype,omitempty"`
	SubPartitions bool        `json:"sub_partitions"`
	Partitions    []Partition `json:"partitions,omitempty"`
}

type NasInfo struct {
	Blockdevices []struct {
		DiskName   string      `json:"disk_name"`
		Mountpoint interface{} `json:"mountpoint,omitempty"`
		Size       string      `json:"size"`
		Fstype     string      `json:"fstype,omitempty"`
		Partitions []Partition `json:"partitions,omitempty"`
	} `json:"blockdevices"`
}

Now there can be many partitions inside block devices, and a partition can have more sub partitions in it. I want to manually assign value to a field in partition struct. How can i do that.
How can i iterate over each partition and sub partitions and assign a manual value to it.
Is there any way to do this ?

Currently using this :

	totalPartitions := len(diskInfo.Blockdevices[0].Partitions)
	if totalPartitions > 0 {
		for i := 0; i < totalPartitions; i++ {
			if diskInfo.Blockdevices[0].Partitions[i].Partitions != nil {
				diskInfo.Blockdevices[0].Partitions[i].SubPartitions = true
			} else {
				diskInfo.Blockdevices[0].Partitions[i].SubPartitions = false
			}
		}
	}

But it can only handle 1 partition with one sub partition. Is there any way i can iterate over each and assign value to it?

答案1

得分: 1

由于SubPartitions标志与Partitions相关联,一种选择是删除SubPartitions字段,并添加一个计算该值的方法:

func (p *Partition) HasSubPartitions() bool {
    // 注意:要检查切片是否为空,建议查看`len(slice)`而不是`nil`(可能存在非nil指针指向长度为0的切片)
    return len(p.Partitions) > 0
}
英文:

Since that SubPartitions flag is coupled with Partitions, one option is to remove the SubPartitions field, and add a method that computes this value :

func (p *Partition) HasSubPartitions() bool {
    // note : to check if a slice is empty or not, it is advised to look at
    // 'len(slice)' rather than nil (it is possible to have a non-nil pointer
    // to a 0 length slice)
    return len(p.Partitions) > 0  
}

答案2

得分: 1

写一个函数

func markSubPartition(p *Partition) {
    if len(p.Partitions) > 0 {
        p.SubPartitions = true
        for _, part := range p.Partitions {
            markSubPartition(part)
        }
    } else {
        p.SubPartitions = false
    }
}

并按照以下方式循环遍历块设备:

for _, part := range diskInfo.Blockdevices[0].Partitions {
    markSubPartition(part)
}

还需要修改你的结构体,将partition字段更改为:

Partitions []*Partition `json:"partitions,omitempty"`

希望对你有所帮助,谢谢!

英文:

Write a function

func markSubPartition(p *Partition) {
if len(p.Partitions) > 0 {
	p.SubPartitions = true
	for _, part := range p.Partitions {
		markSubPartition(part)
	}
} else {
	p.SubPartitions = false
}}

And loop over blockdevices like this

for _, part := range diskInfo.Blockdevices[0].Partitions {
				markSubPartition(part)}

Also need to alter your struct change partition field to :
Partitions []*Partition json:"partitions,omitempty"
Hope it helps, Thanks!

huangapple
  • 本文由 发表于 2021年5月19日 05:18:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/67594087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定