使用反射从深层嵌套结构中提取标签

问题描述 投票:0回答:2

我正在尝试从一些深层嵌套的结构中提取一些标签。这些结构是从protobuf消息生成的,并包含json标签。

我有一个指向结构的指针,该结构可能包含一个结构,该结构的字段可能需要我的标签。我可以使用类型进行迭代以获取结构的字段,但是当我遇到一个指针字段时,如何获取其值然后递归?

// Struct has hierarchy like this 
a := &somepb.UpdateRequest{
        Updates: []*somepb.UpdateRequest_Foo{
            &somepb.UpdateRequest_Foo{
                Id: 1,
                Foo: &somepb.FooInfo{
                    Metadata: &somepb.Metadata{
                        Name:        "Foo",
                        Description: "Bar",
                    },
                    Some:    "s",
                    Things:  "o",
                    Are:     "m",
                    Broken:  "e",
                    Here:    "p",
                },
            },
        },
 } 

// ParseStruct parses struct tags of given object
func ParseStruct(obj interface{}, tag string) {
    r := reflect.ValueOf(obj)
    if r.Kind() == reflect.Ptr {
        obj = r.Elem().Interface()
    }
    rv := reflect.TypeOf(obj)
    for i := 0; i < rv.NumField(); i++ {
        f := rv.Field(i)
        // This is to avoid getting tags for Metadata itself (from the struct above)
        // which is *Metadata, I want the tags for Name and Description
        // inside *Metadata instead
        if f.Type.Kind() == reflect.Ptr {
            value := f.Tag.Get(tag)
            if len(value) == 0 {
                continue
            }
            fmt.Println(value)
        }
    }
} 
go struct reflection tags
2个回答
1
投票

reflect.Typereflect.Value都具有Elem()方法。根据document

Elem的[Type方法。

// Elem returns a type's element type.
// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
Elem() Type

Value.Elem

func (v Value) Elem() Value

Elem returns the value that the interface v contains or that the pointer v points to. It panics if v's Kind is not Interface or Ptr. It returns the zero Value if v is nil. 

您可以使用Elem()方法获取指针的内容,然后使用该内容进行递归。但是,假设您的原始API为func(interface{},string),则需要使用Value.Elem().Interface()来获得有意义的interface{}。但是,相反,我建议您更改API以接受reflect.Type-因为这对于标记提取最为明显。

示例代码:

func ParseStruct(t reflect.Type, tag string) {
    if t.Kind() == reflect.Ptr {
        t = t.Elm()
    }
    if t.Kind() != reflect.Struct {
        return
    }

    for i := 0; i < t.NumField(); i++ {
        f := t.Field(i)
        value := f.Tag.Get(tag)
        ft := t.Type
        if ft.Kind() == reflect.Ptr {
            ft = ft.Elem()
        }

        // It seems that you don't want a tag from an struct field; only other fields' tags are needed
        if ft.Kind() != reflect.Struct {
            if len(value) != 0 {
                fmt.Println(value)
            }
            continue
        }

        ParseStruct(ft,tag)
    }
}

请注意,此代码非常简单-它不会处理切片或地图中的结构标签。


0
投票

当我遇到一个指针字段时,如何获取其值,然后递归?

当遇到指针时,在reflect.Valuereflect.Type上,您必须拿Elem()继续前进。

[reflect.ValueOf(new(string)).Elem()从值*string变为string

[reflect.TypeOf(new(string)).Elem()从类型*string变为string

© www.soinside.com 2019 - 2024. All rights reserved.