I am trying to recurse through the struct tags for embedded structs.
This is the initial code, which gives a shallow report of struct tags
package main
import (
"fmt"
"reflect"
)
type Child struct {
AA string `name:"alice"`
BB string `name:"bella"`
}
type Parent struct {
A string `name:"adrian"`
Child
B string `name:"bob"`
}
func report(defn any) {
t := reflect.TypeOf(defn)
fmt.Printf("\n-- Field <%s>\n", t.Name())
for i := range t.NumField() {
f := t.Field(i)
name := f.Tag.Get("name")
tname := f.Type.Name()
fmt.Printf("Field tag(name) '%s' type '%s'\n", name, tname)
}
}
func main() {
report(Parent{})
report(Child{})
}
When run this shows :
-- Field <Parent>
Field tag(name) 'adrian' type 'string'
Field tag(name) '' type 'Child'
Field tag(name) 'bob' type 'string'
-- Field <Child>
Field tag(name) 'alice' type 'string'
Field tag(name) 'bella' type 'string'
I want to report the Child struct tags from within the Parent struct…adding a recursive call (see further down or Go Playground - The Go Programming Language) shows
-- Field <Parent>
Field tag(name) 'adrian' type 'string'
Field tag(name) '' type 'Child'
-- Field <StructField>
Field tag(name) '' type 'string'
Field tag(name) '' type 'string'
Field tag(name) '' type 'Type'
Field tag(name) '' type 'StructTag'
Field tag(name) '' type 'uintptr'
Field tag(name) '' type ''
Field tag(name) '' type 'bool'
Field tag(name) 'bob' type 'string'
Does anyone know how I get the actual Child struct fields so I can iterate through them? Note this is for a more complex piece of code - I can only access the embedded struct through the parent (and I also have to change field values - not shown here).
Thank you - in advance - Andy
The amended code is below
package main
import (
"fmt"
"reflect"
)
type Child struct {
AA string `name:"alice"`
BB string `name:"bella"`
}
type Parent struct {
A string `name:"adrian"`
Child
B string `name:"bob"`
}
func report(defn any) {
t := reflect.TypeOf(defn)
fmt.Printf("\n-- Field <%s>\n", t.Name())
for i := range t.NumField() {
f := t.Field(i)
name := f.Tag.Get("name")
tname := f.Type.Name()
fmt.Printf("Field tag(name) '%s' type '%s'\n", name, tname)
if tname == "Child" {
report(f)
}
}
}
func main() {
report(Parent{})
}