2015-06-03 03:25:37 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-06-12 21:52:27 +03:00
|
|
|
"fmt"
|
2015-06-03 03:25:37 +03:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/myodc/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
func extractValue(v reflect.Type) *registry.Value {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := ®istry.Value{
|
|
|
|
Name: v.Name(),
|
|
|
|
Type: v.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
val := extractValue(v.Field(i).Type)
|
|
|
|
val.Name = v.Field(i).Name
|
|
|
|
arg.Values = append(arg.Values, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractEndpoint(method reflect.Method) *registry.Endpoint {
|
|
|
|
if method.PkgPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rspType, reqType reflect.Type
|
2015-06-12 21:52:27 +03:00
|
|
|
var stream bool
|
2015-06-03 03:25:37 +03:00
|
|
|
mt := method.Type
|
|
|
|
|
|
|
|
switch mt.NumIn() {
|
|
|
|
case 3:
|
|
|
|
reqType = mt.In(1)
|
|
|
|
rspType = mt.In(2)
|
|
|
|
case 4:
|
|
|
|
reqType = mt.In(2)
|
|
|
|
rspType = mt.In(3)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 21:52:27 +03:00
|
|
|
if rspType.Kind() == reflect.Func {
|
|
|
|
stream = true
|
|
|
|
}
|
2015-06-03 03:25:37 +03:00
|
|
|
|
|
|
|
request := extractValue(reqType)
|
|
|
|
response := extractValue(rspType)
|
|
|
|
|
|
|
|
return ®istry.Endpoint{
|
|
|
|
Name: method.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
2015-06-12 21:52:27 +03:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"stream": fmt.Sprintf("%v", stream),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractSubValue(typ reflect.Type) *registry.Value {
|
|
|
|
var reqType reflect.Type
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
reqType = typ.In(1)
|
|
|
|
default:
|
|
|
|
return nil
|
2015-06-03 03:25:37 +03:00
|
|
|
}
|
2015-06-12 21:52:27 +03:00
|
|
|
return extractValue(reqType)
|
2015-06-03 03:25:37 +03:00
|
|
|
}
|