Use json keys as field path tokens
This commit is contained in:
parent
9438d7bc19
commit
655a9e34df
9
field.go
9
field.go
|
@ -9,7 +9,7 @@ type field struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
Value interface{} `json:"val"`
|
Value interface{} `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractFields(st interface{}, path string) []field {
|
func extractFields(st interface{}, path string) []field {
|
||||||
|
@ -24,10 +24,11 @@ func extractFields(st interface{}, path string) []field {
|
||||||
for i := 0; i < val.NumField(); i++ {
|
for i := 0; i < val.NumField(); i++ {
|
||||||
ftyp := typ.Field(i)
|
ftyp := typ.Field(i)
|
||||||
fval := val.Field(i)
|
fval := val.Field(i)
|
||||||
|
tag := ftyp.Tag.Get("json")
|
||||||
|
|
||||||
switch kind := fval.Kind(); kind {
|
switch kind := fval.Kind(); kind {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
sub := extractFields(fval.Interface(), ftyp.Name+".")
|
sub := extractFields(fval.Interface(), path+tag+".")
|
||||||
res = append(res, sub...)
|
res = append(res, sub...)
|
||||||
case reflect.Bool,
|
case reflect.Bool,
|
||||||
reflect.Int,
|
reflect.Int,
|
||||||
|
@ -44,13 +45,13 @@ func extractFields(st interface{}, path string) []field {
|
||||||
reflect.Float64,
|
reflect.Float64,
|
||||||
reflect.String:
|
reflect.String:
|
||||||
res = append(res, field{
|
res = append(res, field{
|
||||||
Path: path + ftyp.Name,
|
Path: path + tag,
|
||||||
Name: ftyp.Name,
|
Name: ftyp.Name,
|
||||||
Kind: kind.String(),
|
Kind: kind.String(),
|
||||||
Value: fval.Interface(),
|
Value: fval.Interface(),
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
log.Printf("Field type %q not supported for field %q\n", kind, path+ftyp.Name)
|
log.Printf("Field type %q not supported for field %q\n", kind, path+tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue