Loop over a dynamic nested struct in golang

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

Loop over a dynamic nested struct in golang

问题

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

  1. type Partition struct {
  2. DiskName string `json:"disk_name"`
  3. Mountpoint interface{} `json:"mountpoint,omitempty"`
  4. Size string `json:"size"`
  5. Fstype string `json:"fstype,omitempty"`
  6. SubPartitions bool `json:"sub_partitions"`
  7. Partitions []Partition `json:"partitions,omitempty"`
  8. }
  9. type NasInfo struct {
  10. Blockdevices []struct {
  11. DiskName string `json:"disk_name"`
  12. Mountpoint interface{} `json:"mountpoint,omitempty"`
  13. Size string `json:"size"`
  14. Fstype string `json:"fstype,omitempty"`
  15. Partitions []Partition `json:"partitions,omitempty"`
  16. } `json:"blockdevices"`
  17. }

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

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

英文:

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

  1. type Partition struct {
  2. DiskName string `json:"disk_name"`
  3. Mountpoint interface{} `json:"mountpoint,omitempty"`
  4. Size string `json:"size"`
  5. Fstype string `json:"fstype,omitempty"`
  6. SubPartitions bool `json:"sub_partitions"`
  7. Partitions []Partition `json:"partitions,omitempty"`
  8. }
  9. type NasInfo struct {
  10. Blockdevices []struct {
  11. DiskName string `json:"disk_name"`
  12. Mountpoint interface{} `json:"mountpoint,omitempty"`
  13. Size string `json:"size"`
  14. Fstype string `json:"fstype,omitempty"`
  15. Partitions []Partition `json:"partitions,omitempty"`
  16. } `json:"blockdevices"`
  17. }

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 :

  1. totalPartitions := len(diskInfo.Blockdevices[0].Partitions)
  2. if totalPartitions > 0 {
  3. for i := 0; i < totalPartitions; i++ {
  4. if diskInfo.Blockdevices[0].Partitions[i].Partitions != nil {
  5. diskInfo.Blockdevices[0].Partitions[i].SubPartitions = true
  6. } else {
  7. diskInfo.Blockdevices[0].Partitions[i].SubPartitions = false
  8. }
  9. }
  10. }

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字段,并添加一个计算该值的方法:

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

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

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

答案2

得分: 1

写一个函数

  1. func markSubPartition(p *Partition) {
  2. if len(p.Partitions) > 0 {
  3. p.SubPartitions = true
  4. for _, part := range p.Partitions {
  5. markSubPartition(part)
  6. }
  7. } else {
  8. p.SubPartitions = false
  9. }
  10. }

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

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

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

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

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

英文:

Write a function

  1. func markSubPartition(p *Partition) {
  2. if len(p.Partitions) > 0 {
  3. p.SubPartitions = true
  4. for _, part := range p.Partitions {
  5. markSubPartition(part)
  6. }
  7. } else {
  8. p.SubPartitions = false
  9. }}

And loop over blockdevices like this

  1. for _, part := range diskInfo.Blockdevices[0].Partitions {
  2. 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:

确定