67 lines
1.0 KiB
Go
67 lines
1.0 KiB
Go
package server
|
|
|
|
import (
|
|
"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
|
|
// var stream bool
|
|
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
|
|
}
|
|
|
|
// if rspType.Kind() == reflect.Func {
|
|
// stream = true
|
|
// }
|
|
|
|
request := extractValue(reqType)
|
|
response := extractValue(rspType)
|
|
|
|
return ®istry.Endpoint{
|
|
Name: method.Name,
|
|
Request: request,
|
|
Response: response,
|
|
}
|
|
}
|