递归地迭代结构体并使用go设置字段

nvbavucw  于 2023-06-27  发布在  Go
关注(0)|答案(1)|浏览(85)

我想建立的程序,设置字段的结构使用反射。我让它为顶级字段工作,但我正在努力与嵌套结构字段。如何遍历嵌套的结构体字段?

type Payload struct {
    Type    string   `json:"type"`
    SubItem *SubItem `json:"sub_item"`
}

type SubItem struct {
    Foo string `json:"foo"`
}

func main() {
    var payload Payload
    setValue(&payload, "type", "test1")
    setValue(&payload, "sub_item.foo", "test2")
}

func setValue(structPtr interface{}, key string, value string) {
    structValue := reflect.Indirect(reflect.ValueOf(structPtr))
    for i, subkey := range strings.Split(key, ".") {
        isLast := i == len(strings.Split(key, "."))-1
        var found bool
        // this line is crashing with "reflect: call of reflect.Value.NumField on zero Value"
        for i := 0; i < structValue.NumField(); i++ {
            field := structValue.Type().Field(i)
            jsonTags := strings.Split(field.Tag.Get("json"), ",")
            if jsonTags[0] == subkey {
                found = true
                if isLast {
                    if isLast {
                        // last element
                        // TODO set value
                        fmt.Printf("TODO set value %s to %v", value, structValue)
                        structValue = reflect.Indirect(reflect.ValueOf(structPtr))
                    }
                } else {
                    structValue = reflect.Indirect(reflect.ValueOf(structValue.Field(i).Interface()))
                }
                break
            }
        }
        if !found {
            panic(fmt.Errorf("failed to find field %s", key))
        }
    }
}
kx1ctssn

kx1ctssn1#

使用此功能:

func setValue(p interface{}, key string, value interface{}) {
    v := reflect.ValueOf(p)

    // Loop through the names in key to find the target field.
    for _, name := range strings.Split(key, ".") {

        // If the value is pointer, then
        // - allocate value if ptr is nil.
        // - indirect ptr
        for v.Kind() == reflect.Ptr {
            if v.IsNil() {
                v.Set(reflect.New(v.Type().Elem()))
            }
            v = v.Elem()
        }

        // We expect that the value is struct. Find the 
        // named field.
        v = findJSONField(v, name)
        if !v.IsValid() {
            panic(fmt.Sprintf("could not find field %s", key))
        }
    }
    
    // Set the field.
    v.Set(reflect.ValueOf(value))
}

函数findJSONField通过字段的JSON标记查找结构字段:

func findJSONField(v reflect.Value, name string) reflect.Value {
    t := v.Type()
    for i := 0; i < v.NumField(); i++ {
        if tag, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ","); tag == name {
            return v.Field(i)
        }
    }
    return reflect.Value{}
}

https://go.dev/play/p/DnSuydQgQt9

相关问题