reflect: add support for struct types

This commit is contained in:
Ayke van Laethem
2019-08-08 14:41:52 +02:00
committed by Ron Evans
parent 5012be337f
commit e2c8654237
9 changed files with 654 additions and 134 deletions
+25 -1
View File
@@ -11,6 +11,17 @@ type (
myslice2 []myint
mychan chan int
myptr *int
point struct {
X int16
Y int16
}
mystruct struct {
n int `foo:"bar"`
some point
zero struct{}
buf []byte
Buf []byte
}
)
func main() {
@@ -86,6 +97,12 @@ func main() {
// structs
struct{}{},
struct{ error }{},
struct {
a uint8
b int16
c int8
}{42, 321, 123},
mystruct{5, point{-5, 3}, struct{}{}, []byte{'G', 'o'}, []byte{'X'}},
} {
showValue(reflect.ValueOf(v), "")
}
@@ -291,7 +308,14 @@ func showValue(rv reflect.Value, indent string) {
showValue(rv.Index(i), indent+" ")
}
case reflect.Struct:
println(indent + " struct")
println(indent+" struct:", rt.NumField())
for i := 0; i < rv.NumField(); i++ {
field := rt.Field(i)
println(indent+" field:", i, field.Name)
println(indent+" tag:", field.Tag)
println(indent+" embedded:", field.Anonymous)
showValue(rv.Field(i), indent+" ")
}
default:
println(indent + " unknown type kind!")
}
+75 -2
View File
@@ -217,9 +217,82 @@ reflect type: map
map
nil: false
reflect type: struct
struct
struct: 0
reflect type: struct
struct
struct: 1
field: 0 error
tag:
embedded: true
reflect type: interface
interface
nil: true
reflect type: struct
struct: 3
field: 0 a
tag:
embedded: false
reflect type: uint8
uint: 42
field: 1 b
tag:
embedded: false
reflect type: int16
int: 321
field: 2 c
tag:
embedded: false
reflect type: int8
int: 123
reflect type: struct
struct: 5
field: 0 n
tag: foo:"bar"
embedded: false
reflect type: int
int: 5
field: 1 some
tag:
embedded: false
reflect type: struct
struct: 2
field: 0 X
tag:
embedded: false
reflect type: int16
int: -5
field: 1 Y
tag:
embedded: false
reflect type: int16
int: 3
field: 2 zero
tag:
embedded: false
reflect type: struct
struct: 0
field: 3 buf
tag:
embedded: false
reflect type: slice
slice: uint8 2 2
pointer: true
nil: false
indexing: 0
reflect type: uint8
uint: 71
indexing: 1
reflect type: uint8
uint: 111
field: 4 Buf
tag:
embedded: false
reflect type: slice
slice: uint8 1 1
pointer: true
nil: false
indexing: 0
reflect type: uint8 settable=true
uint: 88
sizes:
int8 1 8