当前位置: > > > > 使用反射从深层嵌套结构中提取标签
来源:stackoverflow
2024-04-21 12:18:37
0浏览
收藏
一分耕耘,一分收获!既然都打开这篇《使用反射从深层嵌套结构中提取标签》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!
问题内容
我正在尝试从一些深层嵌套的结构中提取一些标签。这些结构是从 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) } } }
解决方案
reflect.type
和 reflect.value
都有 elem()
方法。根据 ,
type
的elem
方法。
// 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) } }
请注意,此代码非常简单 – 它不处理切片或映射中的结构标签。
当你在 reflect.value
或 reflect.type
上遇到指针时,你必须使用 elem()
继续前进。
reflect.valueof(new(string)).elem()
从值 *string
变为 string
reflect.typeof(new(string)).elem()
从类型 *string
变为 string
理论要掌握,实操不能落!以上关于《使用反射从深层嵌套结构中提取标签》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注公众号吧!