简体   繁体   中英

Dynamic nested struct in golang

I want to create a dynamic struct. I am using some commands to get some information in JSON format and want to unmarshal it into a struct. Json look like this:

{
"blockdevices": [
    {
        "disk_name": "sda",
        "mountpoint": null,
        "size": "50G",
        "fstype": "mpath_member",
        "partitions": [
            {
                "disk_name": "sda1",
                "mountpoint": null,
                "size": "20G",
                "fstype": "vfat"
            },
            {
                "name": "3600a09803830566e615d5171774a3837",
                "mountpoint": null,
                "size": "50G",
                "fstype": null,
                "partitions": [
                    {
                        "disk_name": "3600a09803830566e615d5171774a3837-part1",
                        "mountpoint": "/myData",
                        "size": "20G",
                        "fstype": "vfat",
                        "partitions": [
                            {
                                "disk_name": "3600a09803830566e615d5171774a3837-part2",
                                "mountpoint": "/myData2",
                                "size": "10G",
                                "fstype": "vfat"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]}

The issue is that there may b unknown sub partitions can be 1 or can be up to any number. I have created the following struct:

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

Its working fine for upto two sub partitions but i want a solution that can work up to no matter how many sub partitions we have. Is there any way to do so. The partition struct inside disk struct is same can we some how like write once but it works as loop?

Thanks is advance!

Define a Partition struct:

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

This can nest as much as you need using the partitions slice. Use this type inside BlockDevice .

Structs can be defined recursively . Define a separate struct for Partition (as a welcome side effect, this also makes your code easier to handle, instead of defining your entire JSON structure in one big nested type) and have that struct reference itself (note the Partitions attribute in the Partition type):

type Blockdevice struct {
    DiskName   string      `json:"disk_name"`
    Mountpoint interface{} `json:"mountpoint"`
    Size       string      `json:"size"`
    Fstype     string      `json:"fstype"`
    Partitions []Partition `json:"partitions"`
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM