Merge pull request #114 from micro/extractor

Use struct tags if available
This commit is contained in:
Asim Aslam 2016-08-01 13:03:32 +01:00 committed by GitHub
commit 56aaeff042

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
} }
// 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 val.Name = v.Field(i).Name
}
arg.Values = append(arg.Values, val) arg.Values = append(arg.Values, val)
} }
case reflect.Slice: case reflect.Slice: