首頁  >  文章  >  後端開發  >  使用反射遞歸迭代結構並設定字段

使用反射遞歸迭代結構並設定字段

WBOY
WBOY轉載
2024-02-10 20:45:10585瀏覽

使用反射遞歸迭代結構並設定字段

php小編西瓜在本文中將介紹如何使用反射遞歸迭代結構並設定欄位。反射是PHP中一種強大的特性,它允許我們在運行時獲取並操作類別、方法、屬性等資訊。遞歸迭代結構是一種常用的處理方式,它可以幫助我們遍歷和操作複雜的資料結構。透過結合反射和遞歸迭代結構,我們可以輕鬆地取得並設定欄位的值,從而實現更靈活、高效的程式設計。在接下來的內容中,我們將詳細介紹這個過程,幫助讀者更好地理解並應用這個技巧。

問題內容

我想建立使用反射設定結構體欄位的程式。我讓它適用於頂級字段,但我正在努力處理嵌套結構字段。如何迭代嵌套結構字段?

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))
        }
    }
}

解決方法

使用此功能:

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://www.php.cn/link/e4848ea6b69df2c66c87e2877e74726b

以上是使用反射遞歸迭代結構並設定字段的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除