2021-01-29 13:17:32 +03:00
|
|
|
package register
|
2020-10-09 16:15:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2020-12-13 13:51:09 +03:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2020-11-04 00:38:12 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-10-09 16:15:36 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// ExtractValue from reflect.Type from specified depth
|
2021-04-20 12:39:21 +03:00
|
|
|
func ExtractValue(v reflect.Type, d int) string {
|
2020-10-09 16:15:36 +03:00
|
|
|
if d == 3 {
|
2021-04-20 12:39:21 +03:00
|
|
|
return ""
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
if v == nil {
|
2021-04-20 12:39:21 +03:00
|
|
|
return ""
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:48:05 +03:00
|
|
|
// slices and maps don't have a defined name
|
2021-03-12 16:09:20 +03:00
|
|
|
if (v.Kind() == reflect.Slice || v.Kind() == reflect.Map) || len(v.Name()) == 0 {
|
2021-04-20 12:39:21 +03:00
|
|
|
return ""
|
2020-12-13 13:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the rune character
|
|
|
|
a, _ := utf8.DecodeRuneInString(string(v.Name()[0]))
|
|
|
|
|
|
|
|
// crude check for is unexported field
|
|
|
|
if unicode.IsLower(a) {
|
2021-04-20 12:39:21 +03:00
|
|
|
return ""
|
2020-12-13 13:51:09 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:39:21 +03:00
|
|
|
return v.Name()
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ExtractEndpoint extract *Endpoint from reflect.Method
|
2020-10-09 16:20:10 +03:00
|
|
|
func ExtractEndpoint(method reflect.Method) *Endpoint {
|
2020-10-09 16:15:36 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// are we dealing with a stream?
|
|
|
|
switch rspType.Kind() {
|
|
|
|
case reflect.Func, reflect.Interface:
|
|
|
|
stream = true
|
|
|
|
}
|
|
|
|
|
2020-10-09 16:20:10 +03:00
|
|
|
request := ExtractValue(reqType, 0)
|
|
|
|
response := ExtractValue(rspType, 0)
|
2021-04-20 12:39:21 +03:00
|
|
|
if request == "" || response == "" {
|
2020-12-13 13:51:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-09 16:15:36 +03:00
|
|
|
|
2020-10-09 16:20:10 +03:00
|
|
|
ep := &Endpoint{
|
2020-10-09 16:15:36 +03:00
|
|
|
Name: method.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
2020-11-04 00:38:12 +03:00
|
|
|
Metadata: metadata.New(0),
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if stream {
|
2020-11-18 16:50:41 +03:00
|
|
|
ep.Metadata.Set("stream", fmt.Sprintf("%v", stream))
|
2020-10-09 16:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ep
|
|
|
|
}
|
2020-10-09 16:21:47 +03:00
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ExtractSubValue exctact *Value from reflect.Type
|
2021-04-20 12:39:21 +03:00
|
|
|
func ExtractSubValue(typ reflect.Type) string {
|
2020-10-09 16:21:47 +03:00
|
|
|
var reqType reflect.Type
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
reqType = typ.In(1)
|
|
|
|
case 3:
|
|
|
|
reqType = typ.In(2)
|
|
|
|
default:
|
2021-04-20 12:39:21 +03:00
|
|
|
return ""
|
2020-10-09 16:21:47 +03:00
|
|
|
}
|
|
|
|
return ExtractValue(reqType, 0)
|
|
|
|
}
|