Use struct tags if available

This commit is contained in:
Asim 2016-07-30 19:01:42 +01:00
parent 2d6c403992
commit 15f19b700f

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"reflect" "reflect"
"strings"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
@ -40,11 +41,23 @@ func extractValue(v reflect.Type, d int) *registry.Value {
switch v.Kind() { switch v.Kind() {
case reflect.Struct: case reflect.Struct:
for i := 0; i < v.NumField(); i++ { for i := 0; i < v.NumField(); i++ {
val := extractValue(v.Field(i).Type, d+1) f := v.Field(i)
val := extractValue(f.Type, d+1)
if val == nil { if val == nil {
continue continue
} }
val.Name = v.Field(i).Name
// if we can find a json tag use it
if tags := f.Tag.Get("json"); len(tags) > 0 {
parts := strings.Split(tags, ",")
val.Name = parts[0]
}
// if there's no name default it
if len(val.Name) == 0 {
val.Name = v.Field(i).Name
}
arg.Values = append(arg.Values, val) arg.Values = append(arg.Values, val)
} }
case reflect.Slice: case reflect.Slice: