walk: rerefacto
This commit is contained in:
parent
61a0d0ff03
commit
10a93aeb27
23
walk/walk.go
23
walk/walk.go
@ -7,28 +7,27 @@ import (
|
||||
func Walk(x interface{}, fn func(string)) {
|
||||
val := getValue(x)
|
||||
|
||||
numberOfValues := 0
|
||||
var getField func(int) reflect.Value
|
||||
walkValue := func(value reflect.Value) {
|
||||
Walk(value.Interface(), fn)
|
||||
}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.String:
|
||||
fn(val.String())
|
||||
case reflect.Struct:
|
||||
numberOfValues = val.NumField()
|
||||
getField = val.Field
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
// XXX: Interface() to get interface
|
||||
walkValue(val.Field(i))
|
||||
}
|
||||
case reflect.Slice, reflect.Array:
|
||||
numberOfValues = val.Len()
|
||||
getField = val.Index
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
walkValue(val.Index(i))
|
||||
}
|
||||
case reflect.Map:
|
||||
for _, key := range val.MapKeys() {
|
||||
Walk(val.MapIndex(key).Interface(), fn)
|
||||
walkValue(val.MapIndex(key))
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < numberOfValues; i++ {
|
||||
// XXX: Interface() to get interface
|
||||
Walk(getField(i).Interface(), fn)
|
||||
}
|
||||
}
|
||||
|
||||
func getValue(x interface{}) reflect.Value {
|
||||
|
Loading…
Reference in New Issue
Block a user